/// <summary> /// リソースを開放する /// </summary> public void Dispose() { for (int i = dynamicsWorld.NumConstraints - 1; i >= 0; --i) { var constraint = dynamicsWorld.GetConstraint(i); dynamicsWorld.RemoveConstraint(constraint); constraint.Dispose(); } }
public void ExitPhysics() { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } //delete collision shapes foreach (CollisionShape shape in collisionShapes) { shape.Dispose(); } collisionShapes.Clear(); World.Dispose(); broadphase.Dispose(); if (dispatcher != null) { dispatcher.Dispose(); } collisionConf.Dispose(); }
public void Drop() { if (simThread != null) { simThread.Join(); } for (int i = bulletWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint c = bulletWorld.GetConstraint(i); bulletWorld.RemoveConstraint(c); c.Dispose(); } for (int i = bulletWorld.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject o = bulletWorld.CollisionObjectArray[i]; RigidBody b = o as RigidBody; if (b != null && b.MotionState != null) { b.MotionState.Dispose(); } bulletWorld.RemoveCollisionObject(o); o.Dispose(); } foreach (CollisionShape s in bulletShapes) { s.Dispose(); } bulletShapes.Clear(); bulletWorld.Dispose(); bulletBroadphase.Dispose(); bulletCollisionDispatcher.Dispose(); bulletCollisionConfiguration.Dispose(); }
protected void Dispose(bool disposing) { if (debugType >= BDebug.DebugType.Debug) { Debug.Log("BDynamicsWorld Disposing physics."); } if (PhysicsWorldHelper != null) { PhysicsWorldHelper.m_ddWorld = null; PhysicsWorldHelper.m_world = null; } if (m_world != null) { //remove/dispose constraints int i; if (_ddWorld != null) { if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removing Constraints {0}", _ddWorld.NumConstraints); } for (i = _ddWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = _ddWorld.GetConstraint(i); _ddWorld.RemoveConstraint(constraint); if (constraint.Userobject is BTypedConstraint) { ((BTypedConstraint)constraint.Userobject).m_isInWorld = false; } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removed Constaint {0}", constraint.Userobject); } constraint.Dispose(); } } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removing Collision Objects {0}", m_world.NumCollisionObjects); } //remove the rigidbodies from the dynamics world and delete them for (i = m_world.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = m_world.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints"); body.MotionState.Dispose(); } m_world.RemoveCollisionObject(obj); if (obj.UserObject is BCollisionObject) { ((BCollisionObject)obj.UserObject).isInWorld = false; } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject); } obj.Dispose(); } MultiBodyDynamicsWorld _mbdWorld = m_world as MultiBodyDynamicsWorld; if (_mbdWorld != null) { if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removing MultiBody Constraints {0}", _mbdWorld.NumMultiBodyConstraints); } for (i = _mbdWorld.NumMultiBodyConstraints - 1; i >= 0; i--) { MultiBodyConstraint constraint = _mbdWorld.GetMultiBodyConstraint(i); _mbdWorld.RemoveMultiBodyConstraint(constraint); /* if (constraint.Userobject is BTypedConstraint) ((BTypedConstraint)constraint.Userobject).m_isInWorld = false; * if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removed Constaint {0}", constraint.Userobject);*/ constraint.Dispose(); } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removing Multibodies {0}", _mbdWorld.NumMultibodies); } //remove the rigidbodies from the dynamics world and delete them for (i = _mbdWorld.NumMultibodies - 1; i >= 0; i--) { MultiBody mb = _mbdWorld.GetMultiBody(i); _mbdWorld.RemoveMultiBody(mb); if (mb.UserObject is BMultiBody bmb) { bmb.isInWorld = false; } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removed CollisionObject {0}", mb.UserObject); } mb.Dispose(); } } if (m_world.DebugDrawer != null) { if (m_world.DebugDrawer is IDisposable) { IDisposable dis = (IDisposable)m_world.DebugDrawer; dis.Dispose(); } } m_world.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); _ddWorld = null; m_world = null; } if (Broadphase != null) { Broadphase.Dispose(); Broadphase = null; } if (Dispatcher != null) { Dispatcher.Dispose(); Dispatcher = null; } if (CollisionConf != null) { CollisionConf.Dispose(); CollisionConf = null; } if (constraintSolver != null) { constraintSolver.Dispose(); constraintSolver = null; } if (softBodyWorldInfo != null) { softBodyWorldInfo.Dispose(); softBodyWorldInfo = null; } _isDisposed = true; singleton = null; }
protected void Dispose(bool disposing) { if (debugType >= BDebug.DebugType.Debug) { Debug.Log("BDynamicsWorld Disposing physics."); } /* NU * if (lateUpdateHelper != null) * { * lateUpdateHelper.m_ddWorld = null; * lateUpdateHelper.m_world = null; * } */ if (m_world != null) { //remove/dispose constraints int i; if (_ddWorld != null) { if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removing Constraints {0}", _ddWorld.NumConstraints); } for (i = _ddWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = _ddWorld.GetConstraint(i); _ddWorld.RemoveConstraint(constraint); if (constraint.Userobject is BTypedConstraint) { ((BTypedConstraint)constraint.Userobject).m_isInWorld = false; } if (debugType >= BDebug.DebugType.Debug) { Debug.LogFormat("Removed Constaint {0}", constraint.Userobject); } constraint.Dispose(); } } /* gRally * if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removing Collision Objects {0}", _ddWorld.NumCollisionObjects); * //remove the rigidbodies from the dynamics world and delete them * for (i = m_world.NumCollisionObjects - 1; i >= 0; i--) * { * CollisionObject obj = m_world.CollisionObjectArray[i]; * RigidBody body = obj as RigidBody; * if (body != null && body.MotionState != null) * { * Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints"); * body.MotionState.Dispose(); * } * m_world.RemoveCollisionObject(obj); * if (obj.UserObject is BCollisionObject) ((BCollisionObject)obj.UserObject).isInWorld = false; * if (debugType >= BDebug.DebugType.Debug) Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject); * obj.Dispose(); * } * */ if (m_world.DebugDrawer != null) { if (m_world.DebugDrawer is IDisposable) { IDisposable dis = (IDisposable)m_world.DebugDrawer; dis.Dispose(); } } m_world.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); _ddWorld = null; m_world = null; } if (Broadphase != null) { Broadphase.Dispose(); Broadphase = null; } if (Dispatcher != null) { Dispatcher.Dispose(); Dispatcher = null; } if (CollisionConf != null) { CollisionConf.Dispose(); CollisionConf = null; } if (Solver != null) { Solver.Dispose(); Solver = null; } if (softBodyWorldInfo != null) { softBodyWorldInfo.Dispose(); softBodyWorldInfo = null; } _isDisposed = true; singleton = null; }
/*This Funcion is called at: * public static void Main() * { * var app = new BulletTest(); * app.Run(); * _physic.World.Dispose(); * } * definetly the wrong place!!!!!!!! * TODO: call it at the right place */ public void Dispose() { if (BtWorld != null) { /* for (int d = 0; d < BtWorld.Dispatcher.NumManifolds; d++) * { * var m = BtWorld.Dispatcher.GetManifoldByIndexInternal(d); * BtWorld.Dispatcher.ReleaseManifold(m); * ; * }*/ //remove/dispose constraints int i; for (i = BtWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = BtWorld.GetConstraint(i); BtWorld.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = BtWorld.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = BtWorld.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } BtWorld.RemoveCollisionObject(obj); obj.Dispose(); } //delete collision shapes foreach (CollisionShape shape in BtCollisionShapes) { shape.Dispose(); } BtCollisionShapes.Clear(); BtWorld.Dispose(); BtBroadphase.Dispose(); BtDispatcher.Dispose(); BtCollisionConf.Dispose(); } if (BtBroadphase != null) { BtBroadphase.Dispose(); } if (BtDispatcher != null) { BtDispatcher.Dispose(); } if (BtCollisionConf != null) { BtCollisionConf.Dispose(); } }
//IMPORTANT Time.fixedTime must match the timestep being used here. public static List <UnityEngine.Vector3> SimulateBall(BRigidBody ballRb, UnityEngine.Vector3 ballThrowForce, int numberOfSimulationSteps, bool reverseOrder) { var ballPositions = new List <UnityEngine.Vector3>(numberOfSimulationSteps); //Create a World Debug.Log("Initialize physics"); CollisionConfiguration CollisionConf; CollisionDispatcher Dispatcher; BroadphaseInterface Broadphase; CollisionWorld cw; ConstraintSolver Solver; BulletSharp.SoftBody.SoftBodyWorldInfo softBodyWorldInfo; //This should create a copy of the BPhysicsWorld with the same settings BPhysicsWorld bw = BPhysicsWorld.Get(); bw.CreatePhysicsWorld(out cw, out CollisionConf, out Dispatcher, out Broadphase, out Solver, out softBodyWorldInfo); World = (DiscreteDynamicsWorld)cw; //Copy all existing rigidbodies in scene // IMPORTANT rigidbodies must be added to the offline world in the same order that they are in the source world // this is because collisions must be resolved in the same order for the sim to be deterministic DiscreteDynamicsWorld sourceWorld = (DiscreteDynamicsWorld)bw.world; BulletSharp.RigidBody bulletBallRb = null; BulletSharp.Math.Matrix mm = BulletSharp.Math.Matrix.Identity; for (int i = 0; i < sourceWorld.NumCollisionObjects; i++) { CollisionObject co = sourceWorld.CollisionObjectArray[i]; if (co != null && co.UserObject is BRigidBody) { BRigidBody rb = (BRigidBody)co.UserObject; float mass = rb.isDynamic() ? rb.mass : 0f; BCollisionShape existingShape = rb.GetComponent <BCollisionShape>(); CollisionShape shape = null; if (existingShape is BSphereShape) { shape = ((BSphereShape)existingShape).CopyCollisionShape(); } else if (existingShape is BBoxShape) { shape = ((BBoxShape)existingShape).CopyCollisionShape(); } RigidBody bulletRB = null; BulletSharp.Math.Vector3 localInertia = new BulletSharp.Math.Vector3(); rb.CreateOrConfigureRigidBody(ref bulletRB, ref localInertia, shape, null); BulletSharp.Math.Vector3 pos = rb.GetCollisionObject().WorldTransform.Origin; BulletSharp.Math.Quaternion rot = rb.GetCollisionObject().WorldTransform.GetOrientation(); BulletSharp.Math.Matrix.AffineTransformation(1f, ref rot, ref pos, out mm); bulletRB.WorldTransform = mm; World.AddRigidBody(bulletRB, rb.groupsIBelongTo, rb.collisionMask); if (rb == ballRb) { bulletBallRb = bulletRB; bulletRB.ApplyCentralImpulse(ballThrowForce.ToBullet()); } } } //Step the simulation numberOfSimulationSteps times for (int i = 0; i < numberOfSimulationSteps; i++) { int numSteps = World.StepSimulation(1f / 60f, 10, 1f / 60f); ballPositions.Add(bulletBallRb.WorldTransform.Origin.ToUnity()); } UnityEngine.Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } return(ballPositions); }
private void Start() { //Create a World Debug.Log("Initialize physics"); List <CollisionShape> CollisionShapes = new List <CollisionShape>(); DefaultCollisionConfiguration CollisionConf = new DefaultCollisionConfiguration(); CollisionDispatcher Dispatcher = new CollisionDispatcher(CollisionConf); DbvtBroadphase Broadphase = new DbvtBroadphase(); DiscreteDynamicsWorld World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf) { Gravity = new BulletSharp.Math.Vector3(0, -9.80665f, 0) }; // create a few dynamic rigidbodies const float mass = 1.0f; //Add a single cube RigidBody fallRigidBody; 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, null, shape, localInertia); fallRigidBody = new RigidBody(rbInfo); rbInfo.Dispose(); Matrix st = Matrix.Translation(new BulletSharp.Math.Vector3(0f, 10f, 0f)); fallRigidBody.WorldTransform = st; World.AddRigidBody(fallRigidBody); Matrix trans; //Step the simulation 300 steps for (int i = 0; i < 300; i++) { World.StepSimulation(1f / 60f, 10); fallRigidBody.GetWorldTransform(out trans); Debug.Log("box height: " + trans.Origin); } //Clean up. World.RemoveRigidBody(fallRigidBody); fallRigidBody.Dispose(); Debug.Log("ExitPhysics"); if (World != null) { //remove/dispose constraints int i; for (i = World.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = World.GetConstraint(i); World.RemoveConstraint(constraint); constraint.Dispose(); } //remove the rigidbodies from the dynamics world and delete them for (i = World.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = World.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } World.RemoveCollisionObject(obj); obj.Dispose(); } //delete collision shapes foreach (CollisionShape ss in CollisionShapes) { ss.Dispose(); } CollisionShapes.Clear(); World.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); } if (Broadphase != null) { Broadphase.Dispose(); } if (Dispatcher != null) { Dispatcher.Dispose(); } if (CollisionConf != null) { CollisionConf.Dispose(); } }
protected void Dispose(bool disposing) { BDebug.Log(debugType, "BDynamicsWorld Disposing physics."); if (m_world != null) { //remove/dispose constraints int i; if (_ddWorld != null) { for (i = _ddWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = _ddWorld.GetConstraint(i); _ddWorld.RemoveConstraint(constraint); if (constraint.Userobject is BTypedConstraint) { ((BTypedConstraint)constraint.Userobject).isInWorld = false; } if ((debugType & BDebug.DebugType.Debug) == BDebug.DebugType.Debug) { Debug.LogFormat("Removed Constaint {0}", constraint.Userobject); } constraint.Dispose(); } } //remove the rigidbodies from the dynamics world and delete them for (i = m_world.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = m_world.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } m_world.RemoveCollisionObject(obj); if (obj.UserObject is BCollisionObject) { ((BCollisionObject)obj.UserObject).isInWorld = false; } if ((debugType & BDebug.DebugType.Debug) == BDebug.DebugType.Debug) { Debug.LogFormat("Removed CollisionObject {0}", obj.UserObject); } obj.Dispose(); } if (m_world.DebugDrawer != null) { if (m_world.DebugDrawer is IDisposable) { IDisposable dis = (IDisposable)m_world.DebugDrawer; dis.Dispose(); } } m_world.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); _ddWorld = null; m_world = null; } if (Broadphase != null) { Broadphase.Dispose(); Broadphase = null; } if (Dispatcher != null) { Dispatcher.Dispose(); Dispatcher = null; } if (CollisionConf != null) { CollisionConf.Dispose(); CollisionConf = null; } if (Solver != null) { Solver.Dispose(); Solver = null; } if (softBodyWorldInfo != null) { softBodyWorldInfo.Dispose(); softBodyWorldInfo = null; } _isDisposed = true; singleton = null; }
protected void Dispose(bool disposing) { if (lateUpdateHelper != null) { lateUpdateHelper.m_ddWorld = null; lateUpdateHelper.m_world = null; } if (m_world != null) { //remove/dispose constraints int i; if (_ddWorld != null) { for (i = _ddWorld.NumConstraints - 1; i >= 0; i--) { TypedConstraint constraint = _ddWorld.GetConstraint(i); _ddWorld.RemoveConstraint(constraint); if (constraint.Userobject is BTypedConstraint) { ((BTypedConstraint)constraint.Userobject).m_isInWorld = false; } constraint.Dispose(); } } //remove the rigidbodies from the dynamics world and delete them for (i = m_world.NumCollisionObjects - 1; i >= 0; i--) { CollisionObject obj = m_world.CollisionObjectArray[i]; RigidBody body = obj as RigidBody; if (body != null && body.MotionState != null) { Debug.Assert(body.NumConstraintRefs == 0, "Rigid body still had constraints"); body.MotionState.Dispose(); } m_world.RemoveCollisionObject(obj); if (obj.UserObject is BCollisionObject) { ((BCollisionObject)obj.UserObject).isInWorld = false; } obj.Dispose(); } if (m_world.DebugDrawer != null) { if (m_world.DebugDrawer is IDisposable) { IDisposable dis = (IDisposable)m_world.DebugDrawer; dis.Dispose(); } } m_world.Dispose(); Broadphase.Dispose(); Dispatcher.Dispose(); CollisionConf.Dispose(); _ddWorld = null; m_world = null; } if (Broadphase != null) { Broadphase.Dispose(); Broadphase = null; } if (Dispatcher != null) { Dispatcher.Dispose(); Dispatcher = null; } if (CollisionConf != null) { CollisionConf.Dispose(); CollisionConf = null; } if (constraintSolver != null) { constraintSolver.Dispose(); constraintSolver = null; } if (softBodyWorldInfo != null) { softBodyWorldInfo.Dispose(); softBodyWorldInfo = null; } _isDisposed = true; singleton = null; }