Skip to content

Instantly share code, notes, and snippets.

@fversnel
Last active August 11, 2016 14:44

Revisions

  1. Frank Versnel revised this gist Aug 11, 2016. 1 changed file with 27 additions and 31 deletions.
    58 changes: 27 additions & 31 deletions BulletThreadingUnity.cs
    Original file line number Diff line number Diff line change
    @@ -1,83 +1,79 @@
    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;
    using Vector3 = BulletSharp.Math.Vector3;

    public class MultithreadingTest : MonoBehaviour {

    private MotionState _bodyView;
    private bool _isPhysicsRunning;
    [SerializeField] private float _physicsTimeStep;
    [SerializeField] private Transform _renderableBody;

    private bool _isPhysicsRunning;
    private Stopwatch _stopwatch;
    private MotionState _bodyView;

    // Use this for initialization
    void Start () {
    // Use this for initialization
    private void Start() {
    _bodyView = new DefaultMotionState();

    _stopwatch = new Stopwatch();
    _stopwatch.Start();

    _isPhysicsRunning = true;
    _isPhysicsRunning = true;
    new Thread(() => {
    DiscreteDynamicsWorld physicsWorld;
    {
    //List<CollisionShape> CollisionShapes = new List<CollisionShape>();

    DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration();
    CollisionDispatcher Dispatcher = new CollisionDispatcher(CollisionConf);
    var CollisionConf = new DefaultCollisionConfiguration();
    var Dispatcher = new CollisionDispatcher(CollisionConf);

    DbvtBroadphase Broadphase = new DbvtBroadphase();
    var Broadphase = new DbvtBroadphase();

    physicsWorld = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf);
    physicsWorld.Gravity = new BulletSharp.Math.Vector3(0, -1, 0);
    physicsWorld.Gravity = new 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;
    var shape = new BoxShape(1f, 1f, 1f);
    Vector3 localInertia = Vector3.Zero;
    shape.CalculateLocalInertia(mass, out localInertia);
    RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, _bodyView, shape, localInertia);
    var 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));
    Matrix st = Matrix.Translation(new Vector3(0f, 0f, 0f));
    body.WorldTransform = st;
    physicsWorld.AddRigidBody(body);
    }

    float fixedTimePassed = 0;
    while (_isPhysicsRunning) {
    var currentTime = _stopwatch.ElapsedMilliseconds / 1000f;
    var fixedDeltaTime = currentTime - fixedTimePassed;
    float currentTime = _stopwatch.ElapsedMilliseconds / 1000f;
    float fixedDeltaTime = currentTime - fixedTimePassed;

    var totalStepsPerformed = physicsWorld.StepSimulation(fixedDeltaTime, 5, _physicsTimeStep);
    int totalStepsPerformed = physicsWorld.StepSimulation(fixedDeltaTime, 5, _physicsTimeStep);
    fixedTimePassed += _physicsTimeStep * totalStepsPerformed;

    //Debug.Log("physics tick at " + (deltaTime));
    var sleepIntervalInMs = ((int) (_physicsTimeStep * 1000)) / 4;
    int sleepIntervalInMs = ((int) (_physicsTimeStep * 1000)) / 4;
    Thread.Sleep(sleepIntervalInMs);
    }
    }).Start();
    }
    }

    void OnDestroy() {
    private void OnDestroy() {
    _isPhysicsRunning = false;
    }
    // Update is called once per frame
    void Update () {
    Matrix trans;

    // Update is called once per frame
    private void Update() {
    Matrix trans;
    _bodyView.GetWorldTransform(out trans);
    _renderableBody.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref trans);
    _renderableBody.position = BSExtensionMethods2.ExtractTranslationFromMatrix(ref trans);
    _renderableBody.rotation = BSExtensionMethods2.ExtractRotationFromMatrix(ref trans);
    }
    }
    }
    }
  2. Frank Versnel created this gist Aug 11, 2016.
    83 changes: 83 additions & 0 deletions BulletThreadingUnity.cs
    Original 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);
    }
    }