Last active
August 11, 2016 14:44
Revisions
-
Frank Versnel revised this gist
Aug 11, 2016 . 1 changed file with 27 additions and 31 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,83 +1,79 @@ 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); } } -
Frank Versnel created this gist
Aug 11, 2016 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ 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); } }