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.Diagnostics; | |
using System.Threading; | |
using BulletSharp; | |
using BulletSharp.Math; | |
using BulletUnity; | |
using UnityEngine; | |
using Vector3 = BulletSharp.Math.Vector3; | |
public class MultithreadingTest : MonoBehaviour { | |
private MotionState _bodyView; | |
private bool _isPhysicsRunning; | |
[SerializeField] private float _physicsTimeStep; | |
[SerializeField] private Transform _renderableBody; | |
private Stopwatch _stopwatch; | |
// Use this for initialization | |
private void Start() { | |
_bodyView = new DefaultMotionState(); | |
_stopwatch = new Stopwatch(); | |
_stopwatch.Start(); | |
_isPhysicsRunning = true; | |
new Thread(() => { | |
DiscreteDynamicsWorld physicsWorld; | |
{ | |
//List<CollisionShape> CollisionShapes = new List<CollisionShape>(); | |
var CollisionConf = new DefaultCollisionConfiguration(); | |
var Dispatcher = new CollisionDispatcher(CollisionConf); | |
var Broadphase = new DbvtBroadphase(); | |
physicsWorld = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); | |
physicsWorld.Gravity = new Vector3(0, -1, 0); | |
} | |
{ | |
const float mass = 1.0f; | |
//Add a single cube | |
var shape = new BoxShape(1f, 1f, 1f); | |
Vector3 localInertia = Vector3.Zero; | |
shape.CalculateLocalInertia(mass, out localInertia); | |
var rbInfo = new RigidBodyConstructionInfo(mass, _bodyView, shape, localInertia); | |
var body = new RigidBody(rbInfo); | |
rbInfo.Dispose(); | |
Matrix st = Matrix.Translation(new Vector3(0f, 0f, 0f)); | |
body.WorldTransform = st; | |
physicsWorld.AddRigidBody(body); | |
} | |
float fixedTimePassed = 0; | |
while (_isPhysicsRunning) { | |
float currentTime = _stopwatch.ElapsedMilliseconds / 1000f; | |
float fixedDeltaTime = currentTime - fixedTimePassed; | |
int totalStepsPerformed = physicsWorld.StepSimulation(fixedDeltaTime, 5, _physicsTimeStep); | |
fixedTimePassed += _physicsTimeStep * totalStepsPerformed; | |
//Debug.Log("physics tick at " + (deltaTime)); | |
int sleepIntervalInMs = ((int) (_physicsTimeStep * 1000)) / 4; | |
Thread.Sleep(sleepIntervalInMs); | |
} | |
}).Start(); | |
} | |
private void OnDestroy() { | |
_isPhysicsRunning = false; | |
} | |
// Update is called once per frame | |
private 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