Last active
August 11, 2016 14:44
-
-
Save fversnel/da3868f53fc4436dfaba90491cd9d43f to your computer and use it in GitHub Desktop.
Bullet physics in a separate thread on Unity
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Diagnostics; | |
using System.Threading; | |
using BulletSharp; | |
using BulletSharp.Math; | |
using BulletUnity; | |
using UnityEngine; | |
using System.Collections; | |
using Debug = UnityEngine.Debug; | |
public class MultithreadingTest : MonoBehaviour { | |
[SerializeField] private float _physicsTimeStep; | |
[SerializeField] private Transform _renderableBody; | |
private bool _isPhysicsRunning; | |
private Stopwatch _stopwatch; | |
private MotionState _bodyView; | |
// Use this for initialization | |
void Start () { | |
_bodyView = new DefaultMotionState(); | |
_stopwatch = new Stopwatch(); | |
_stopwatch.Start(); | |
_isPhysicsRunning = true; | |
new Thread(() => { | |
DiscreteDynamicsWorld physicsWorld; | |
{ | |
//List<CollisionShape> CollisionShapes = new List<CollisionShape>(); | |
DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration(); | |
CollisionDispatcher Dispatcher = new CollisionDispatcher(CollisionConf); | |
DbvtBroadphase Broadphase = new DbvtBroadphase(); | |
physicsWorld = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); | |
physicsWorld.Gravity = new BulletSharp.Math.Vector3(0, -1, 0); | |
} | |
{ | |
const float mass = 1.0f; | |
//Add a single cube | |
BoxShape shape = new BoxShape(1f, 1f, 1f); | |
BulletSharp.Math.Vector3 localInertia = BulletSharp.Math.Vector3.Zero; | |
shape.CalculateLocalInertia(mass, out localInertia); | |
RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, _bodyView, shape, localInertia); | |
var body = new RigidBody(rbInfo); | |
rbInfo.Dispose(); | |
Matrix st = Matrix.Translation(new BulletSharp.Math.Vector3(0f, 0f, 0f)); | |
body.WorldTransform = st; | |
physicsWorld.AddRigidBody(body); | |
} | |
float fixedTimePassed = 0; | |
while (_isPhysicsRunning) { | |
var currentTime = _stopwatch.ElapsedMilliseconds / 1000f; | |
var fixedDeltaTime = currentTime - fixedTimePassed; | |
var totalStepsPerformed = physicsWorld.StepSimulation(fixedDeltaTime, 5, _physicsTimeStep); | |
fixedTimePassed += _physicsTimeStep * totalStepsPerformed; | |
//Debug.Log("physics tick at " + (deltaTime)); | |
var sleepIntervalInMs = ((int) (_physicsTimeStep * 1000)) / 4; | |
Thread.Sleep(sleepIntervalInMs); | |
} | |
}).Start(); | |
} | |
void OnDestroy() { | |
_isPhysicsRunning = false; | |
} | |
// Update is called once per frame | |
void Update () { | |
Matrix trans; | |
_bodyView.GetWorldTransform(out trans); | |
_renderableBody.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref trans); | |
_renderableBody.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref trans); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment