internal static void RemoveCollisionObject(CollisionObject col) { if (col.IsInWorld) { world.RemoveCollisionObject(col.BulletCollisionObject); } }
/// <summary> /// Removes and destroys (!) the object /// </summary> /// <param name="obj">Physics Object to remove.</param> public void RemoveObject(PhysicsObject obj) { if (obj.RigidBody.MotionState != null) { obj.RigidBody.MotionState.Dispose(); } btWorld.RemoveCollisionObject(obj.RigidBody); obj.RigidBody.Dispose(); objects.Remove(obj); if (!obj.Static) { dynamicObjects.Remove(obj); } }
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(); }
internal override void Unload() { foreach (var joint in joints) { World.RemoveConstraint(joint.Constraint); joint.Constraint.Dispose(); } foreach (var rigid in rigitBodies) { World.RemoveCollisionObject(rigid.Body); rigid.Body.MotionState.Dispose(); rigid.Body.RemoveCustomDebugColor(); rigid.Body.Dispose(); } base.Unload(); }
public void AlignedObjectArray_GhostObject() { var pairs = ghostObject.OverlappingPairs; Assert.AreEqual(0, pairs.Count); // Ghost object's object array does not have a backing list world.AddCollisionObject(ghostObject); Assert.AreEqual(3, world.NumCollisionObjects); world.StepSimulation(1 / 60.0f); Assert.AreEqual(1, pairs.Count); Assert.AreSame(pairs[0].CollisionShape, body2.CollisionShape); world.RemoveCollisionObject(ghostObject); Assert.AreEqual(2, world.NumCollisionObjects); }
/// <summary> /// リソースを開放する /// </summary> public void Dispose() { for (int i = dynamicsWorld.NumCollisionObjects - 1; i >= 0; --i) { CollisionObject obj = dynamicsWorld.CollisionObjectArray[i]; RigidBody body = RigidBody.Upcast(obj); if (body != null && body.MotionState != null) { body.MotionState.Dispose(); } dynamicsWorld.RemoveCollisionObject(obj); obj.Dispose(); } for (int i = 0; i < collisionShapes.Count; ++i) { CollisionShape collisionShape = collisionShapes[i]; collisionShapes[i] = null; collisionShape.Dispose(); } collisionShapes.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(); }
/*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(); } }
public override void RemoveCollision(CollisionProxy colProxy) { world.RemoveCollisionObject((CollisionObject)colProxy.CollisionObject); }