public static void AddPaddle(Entity boxObject, OpenTK.Vector3 position, double width, double height, double paddleAngle) { Box2DShape boxShape = new Box2DShape((float)(width / 2), (float)(height / 2), (0.0f)); RigidBodyConstructionInfo boxConstructionInfo = new RigidBodyConstructionInfo(10000f, new DefaultMotionState( Matrix.Translation(position.X, position.Y, 0) * Matrix.RotationZ((float)paddleAngle) ), boxShape); RigidBody box = new RigidBody(boxConstructionInfo); box.LinearFactor = new BulletSharp.Math.Vector3(1, 1, 0); box.AngularFactor = new BulletSharp.Math.Vector3(0, 0, 1); box.SetDamping(0, 0); box.Restitution = 1f; box.Friction = 0.0f; box.UserObject = boxObject; foreach (var box2 in boxes) { box.SetIgnoreCollisionCheck(box2.Box, true); } boxes.Add(new BoxInfo { BoxObject = boxObject, BoxShape = boxShape, BoxConstructionInfo = boxConstructionInfo, Box = box, DefaultPosition = position }); //Add box to the world colWorld.AddCollisionObject(box); }
public void AddShape(Mesh mesh, Transform tran) { // Cube, Plane은 잘 된다. 구는 잘 안 되는데 // C++로 불릿 내부를 본 뒤에 다시 보도록 한다. // TriangleMesh는 정적 노드를 기준으로 한다. // 변환해서 넣어야 됨. WorldTransform 반영 안 함 var triMesh = new TriangleMesh(true, true); for (int i = 0; i < mesh.Indices.Count; i += 3) { var v0 = ToBulletVector3(OpenTK.Vector3.TransformPosition(mesh.Vertices[mesh.Indices[i + 0]].Position, tran.Matrix)); var v1 = ToBulletVector3(OpenTK.Vector3.TransformPosition(mesh.Vertices[mesh.Indices[i + 1]].Position, tran.Matrix)); var v2 = ToBulletVector3(OpenTK.Vector3.TransformPosition(mesh.Vertices[mesh.Indices[i + 2]].Position, tran.Matrix)); triMesh.AddTriangle(v0, v1, v2, false); } var triShape = new BvhTriangleMeshShape(triMesh, true, true); // 아래는 생략해도 피킹 잘 됨 // triShape.BuildOptimizedBvh(); var colObj = new CollisionObject(); colObj.CollisionShape = triShape; colObj.WorldTransform = ToBulletMatrix(OpenTK.Matrix4.Identity); world.AddCollisionObject(colObj); world.StepSimulation(0.1f); }
internal static CollisionObject CreateCollisionObject( CollisionShape shape, CollisionFilterGroups group = CollisionFilterGroups.DefaultFilter, CollisionFilterGroups mask = CollisionFilterGroups.AllFilter) { if (shape == null) { return(null); } var o = new CollisionObject(shape); collisionObjects.Add(o); world.AddCollisionObject(o.BulletCollisionObject, group, mask); return(o); }
public override void InitializeDemo() { // Setup the basic world SetTexturing(true); SetShadows(true); SetCameraDistance(5.0f); m_collisionConfiguration = new DefaultCollisionConfiguration(); m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); IndexedVector3 worldAabbMin = new IndexedVector3(-10000, -10000, -10000); IndexedVector3 worldAabbMax = new IndexedVector3(10000, 10000, 10000); m_broadphase = new AxisSweep3Internal(ref worldAabbMin, ref worldAabbMax, 0xfffe, 0xffff, 16384, null, true); //m_broadphase = new SimpleBroadphase(1000, null); m_constraintSolver = new SequentialImpulseConstraintSolver(); m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); //m_dynamicsWorld.getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld.getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; // Setup a big ground box { CollisionShape groundShape = new BoxShape(new IndexedVector3(200.0f, 10.0f, 200.0f)); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(0, -10, 0); CollisionObject fixedGround = new CollisionObject(); fixedGround.SetCollisionShape(groundShape); fixedGround.SetWorldTransform(ref groundTransform); fixedGround.SetUserPointer("Ground"); m_dynamicsWorld.AddCollisionObject(fixedGround); } // Spawn one ragdoll IndexedVector3 startOffset = new IndexedVector3(1, 0.5f, 0); //string filename = @"c:\users\man\bullet\xna-ragdoll-constraints-output.txt"; //FileStream filestream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read); //BulletGlobals.g_streamWriter = new StreamWriter(filestream); SpawnRagdoll(ref startOffset, BulletGlobals.g_streamWriter); //startOffset = new IndexedVector3(-1,0.5f,0); //spawnRagdoll(ref startOffset); ClientResetScene(); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConf); World.Gravity = new Vector3(0, -10, 0); renderCallback = new DrawingResult(World); BoxShape boxA = new BoxShape(new Vector3(1, 1, 1)); boxA.Margin = 0; BoxShape boxB = new BoxShape(new Vector3(0.5f, 0.5f, 0.5f)); boxB.Margin = 0; CollisionShapes.Add(boxA); CollisionShapes.Add(boxB); objects[0] = new CollisionObject(); objects[1] = new CollisionObject(); objects[0].CollisionShape = boxA; objects[1].CollisionShape = boxB; World.AddCollisionObject(objects[0]); World.AddCollisionObject(objects[1]); Quaternion rotA = new Quaternion(0.739f, -0.204f, 0.587f, 0.257f); rotA.Normalize(); objects[0].WorldTransform = Matrix.RotationQuaternion(rotA) * Matrix.Translation(0, 3, 0); objects[1].WorldTransform = Matrix.Translation(0, 4.248f, 0); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.DispatchInfo.AllowedCcdPenetration = 0.0001f; //World.Gravity = Freelook.Up * -10.0f; Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f); ghostObject = new PairCachingGhostObject(); ghostObject.WorldTransform = startTransform; Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback()); const float characterHeight = 1.75f; const float characterWidth = 1.75f; ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight); ghostObject.CollisionShape = capsule; ghostObject.CollisionFlags = CollisionFlags.CharacterObject; const float stepHeight = 0.35f; character = new KinematicCharacterController(ghostObject, capsule, stepHeight); BspLoader bspLoader = new BspLoader(); //string filename = UnityEngine.Application.dataPath + "/BulletUnity/Examples/Scripts/BulletSharpDemos/CharacterDemo/data/BspDemo.bsp"; UnityEngine.TextAsset bytes = (UnityEngine.TextAsset)UnityEngine.Resources.Load("BspDemo"); System.IO.Stream byteStream = new System.IO.MemoryStream(bytes.bytes); bspLoader.LoadBspFile(byteStream); BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); World.AddAction(character); Vector3 v1 = new Vector3(0f, 0f, 0f); Vector3 v2 = new Vector3(0f, 0f, 0f); convexResultCallback = new ClosestConvexResultCallback(ref v1, ref v2); convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter; cameraSphere = new SphereShape(0.2f); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); Broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.DispatchInfo.AllowedCcdPenetration = 0.0001f; //World.Gravity = Freelook.Up * -10.0f; Matrix startTransform = Matrix.Translation(10.210098f, -1.6433364f, 16.453260f); ghostObject = new PairCachingGhostObject(); ghostObject.WorldTransform = startTransform; Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new GhostPairCallback()); const float characterHeight = 1.75f; const float characterWidth = 1.75f; ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight); ghostObject.CollisionShape = capsule; ghostObject.CollisionFlags = CollisionFlags.CharacterObject; const float stepHeight = 0.35f; character = new KinematicCharacterController(ghostObject, capsule, stepHeight); BspLoader bspLoader = new BspLoader(); bspLoader.LoadBspFile("data/BspDemo.bsp"); BspConverter bsp2Bullet = new BspToBulletConverter(this); bsp2Bullet.ConvertBsp(bspLoader, 0.1f); World.AddCollisionObject(ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); World.AddAction(character); Vector3 zero = Vector3.Zero; convexResultCallback = new ClosestConvexResultCallback(ref zero, ref zero); convexResultCallback.CollisionFilterMask = (short)CollisionFilterGroups.StaticFilter; cameraSphere = new SphereShape(0.2f); }
private void CreatePlayerBox() { const float stepHeight = 0.35f; CapsuleShape shape = new CapsuleShape(0.4f, charHeighth); _ghostObject = new PairCachingGhostObject() { CollisionShape = shape, CollisionFlags = CollisionFlags.CharacterObject, WorldTransform = Matrix.Translation(Node.GetTransform.Position.Convert() + Vector3.UnitY), Friction = 0.7f }; //world.AddCollisionObject(_ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); world.AddCollisionObject(_ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); _charController = new KinematicCharacterController(_ghostObject, shape, stepHeight); world.AddAction(_charController); }
public MachinegunBullet(DiscreteDynamicsWorld world, Character origin) : base(3f, origin) { var boxRadius = new TGCVector3(0.093f, 0.093f, 0.836f); var tgcBox = TGCBox.fromSize(boxRadius, Color.FromArgb(255, 255, 249, 56)); mesh = tgcBox.ToMesh("MachinegunBullet"); tgcBox.Dispose(); var boxShape = new BoxShape(boxRadius.ToBsVector * 0.5f); var boxTransform = TGCMatrix.RotationYawPitchRoll(0, 0, 0).ToBsMatrix; boxTransform.Origin = new TGCVector3(0, -100, 0).ToBsVector; var boxMotionState = new DefaultMotionState(boxTransform); var boxLocalInertia = boxShape.CalculateLocalInertia(0.5f); var boxInfo = new RigidBodyConstructionInfo(0.5f, boxMotionState, boxShape, boxLocalInertia); rigidBody = new RigidBody(boxInfo); rigidBody.ForceActivationState(ActivationState.DisableDeactivation); rigidBody.Flags = RigidBodyFlags.DisableWorldGravity; rigidBody.CollisionFlags = CollisionFlags.NoContactResponse; world.AddCollisionObject(rigidBody); }
public CollisionInterfaceDemoSimulation() { CollisionConfiguration = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConfiguration); Broadphase = new AxisSweep3( new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, null, CollisionConfiguration); RenderCallback = new DrawingResult(World); _movingBox = new BoxShape(1.0f) { Margin = 0 }; var rotation = Quaternion.RotationYawPitchRoll((float)Math.PI * 0.6f, (float)Math.PI * 0.2f, 0); MovingObject = new CollisionObject { CollisionShape = _movingBox, WorldTransform = Matrix.RotationQuaternion(rotation) * Matrix.Translation(0, 3, 0) }; _staticShape = new BoxShape(0.5f) { Margin = 0 }; _staticObject = new CollisionObject { CollisionShape = _staticShape, WorldTransform = Matrix.Translation(0, 4.248f, 0) }; World.AddCollisionObject(_staticObject); }
public PowerMissile(DiscreteDynamicsWorld world, Character origin) : base(15f, origin) { var loader = new TgcSceneLoader(); var xmlPath = Game.Default.MediaDirectory + Game.Default.ItemsDirectory + "power-missile-TgcScene.xml"; mesh = loader.loadSceneFromFile(xmlPath).Meshes[0]; mesh.AutoTransform = false; var meshAxisRadius = mesh.BoundingBox.calculateAxisRadius().ToBsVector; var boxShape = new BoxShape(meshAxisRadius); var boxTransform = TGCMatrix.RotationYawPitchRoll(0, 0, 0).ToBsMatrix; boxTransform.Origin = new TGCVector3(0, -100, 0).ToBsVector; var boxMotionState = new DefaultMotionState(boxTransform); var boxLocalInertia = boxShape.CalculateLocalInertia(0.5f); var boxInfo = new RigidBodyConstructionInfo(0.5f, boxMotionState, boxShape, boxLocalInertia); rigidBody = new RigidBody(boxInfo); rigidBody.ForceActivationState(ActivationState.DisableDeactivation); rigidBody.Flags = RigidBodyFlags.DisableWorldGravity; rigidBody.CollisionFlags = CollisionFlags.NoContactResponse; world.AddCollisionObject(rigidBody); }
public override void InitializeDemo() { base.InitializeDemo(); SetCameraDistance(50f); //string filename = @"E:\users\man\bullet\xna-basic-output-1.txt"; //FileStream filestream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read); //BulletGlobals.g_streamWriter = new StreamWriter(filestream); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new DefaultCollisionConfiguration(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); IndexedVector3 worldMin = new IndexedVector3 (-1000,-1000,-1000); IndexedVector3 worldMax = -worldMin; m_broadphase = new AxisSweep3Internal(ref worldMin, ref worldMax, 0xfffe, 0xffff, 16384, null, false); //pairCache = new SortedOverlappingPairCache(); //m_broadphase = new SimpleBroadphase(1000, pairCache); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) SequentialImpulseConstraintSolver sol = new SequentialImpulseConstraintSolver(); m_constraintSolver = sol; m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); m_dynamicsWorld.GetDispatchInfo().SetAllowedCcdPenetration(0.0001f); IndexedVector3 gravity = new IndexedVector3(0, -10, 0); m_dynamicsWorld.SetGravity(ref gravity); ///create a few basic rigid bodies IndexedVector3 halfExtents = new IndexedVector3(50, 50, 50); //IndexedVector3 halfExtents = new IndexedVector3(10, 10, 10); CollisionShape groundShape = new BoxShape(ref halfExtents); //CollisionShape groundShape = new StaticPlaneShape(new IndexedVector3(0,1,0), 50); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(new IndexedVector3(0, -50, 0)); //IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(new IndexedVector3(0,-10,0)); float mass = 0f; LocalCreateRigidBody(mass, ref groundTransform, groundShape); #region CharacterController IndexedMatrix startTransform = IndexedMatrix.Identity; //startTransform.setOrigin (btVector3(0.0, 4.0, 0.0)); startTransform._origin = new IndexedVector3(10.210098f,-1.6433364f,16.453260f); m_ghostObject = new PairCachingGhostObject(); m_ghostObject.SetWorldTransform(startTransform); m_broadphase.GetOverlappingPairCache().SetInternalGhostPairCallback(new GhostPairCallback()); float characterHeight=1.75f; float characterWidth =1.75f; ConvexShape capsule = new CapsuleShape(characterWidth,characterHeight); m_ghostObject.SetCollisionShape (capsule); m_ghostObject.SetCollisionFlags (CollisionFlags.CF_CHARACTER_OBJECT); float stepHeight = 0.35f; int upAxis = 1; m_character = new KinematicCharacterController (m_ghostObject,capsule,stepHeight,upAxis); m_dynamicsWorld.AddCollisionObject(m_ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); m_dynamicsWorld.AddAction(m_character); #endregion }
public void AlignedObjectArray_World() { // World's object array has a backing list var objects = world.CollisionObjectArray; Assert.AreSame(objects[0], body1); Assert.AreSame(objects[1], body2); // Swap objects.Swap(0, 1); Assert.AreSame(objects[0], body2); Assert.AreSame(objects[1], body1); objects.Swap(0, 1); Assert.AreSame(objects[0], body1); Assert.AreSame(objects[1], body2); Assert.AreEqual(2, objects.Count); Assert.AreEqual(2, world.NumCollisionObjects); // Remove objects.Remove(body1); Assert.AreEqual(1, objects.Count); Assert.AreSame(body2, objects[0]); // List modifications should propagate to the related world Assert.AreEqual(1, world.NumCollisionObjects); Assert.IsNull(body1.BroadphaseHandle); // Add (body1 & body2 order flipped) objects.Add(body1); Assert.AreSame(body2, objects[0]); Assert.AreSame(body1, objects[1]); Assert.AreEqual(2, world.NumCollisionObjects); Assert.IsNotNull(body1.BroadphaseHandle); Assert.AreSame(body1, body1.BroadphaseHandle.ClientObject); // Contains Assert.True(objects.Contains(body1)); Assert.True(objects.Contains(body2)); // CopyTo CollisionObject[] objectsArray = new CollisionObject[2]; objects.CopyTo(objectsArray, 0); Assert.AreSame(body2, objectsArray[0]); Assert.AreSame(body1, objectsArray[1]); // IndexOf Assert.AreEqual(0, objects.IndexOf(body2)); Assert.AreEqual(1, objects.IndexOf(body1)); Assert.AreEqual(-1, objects.IndexOf(null)); // RemoveAt objects.RemoveAt(0); Assert.AreEqual(1, objects.Count); Assert.AreEqual(1, world.NumCollisionObjects); Assert.AreSame(body1, objects[0]); world.AddCollisionObject(body2); // PopBack objects.PopBack(); Assert.AreEqual(1, objects.Count); Assert.AreEqual(1, world.NumCollisionObjects); Assert.AreSame(body1, objects[0]); world.AddCollisionObject(body2); // Clear objects.Clear(); Assert.AreEqual(0, objects.Count); Assert.AreEqual(0, world.NumCollisionObjects); Assert.IsNull(body1.BroadphaseHandle); Assert.IsNull(body2.BroadphaseHandle); world.AddCollisionObject(body1, CollisionFilterGroups.DefaultFilter, CollisionFilterGroups.AllFilter); world.AddCollisionObject(body2, CollisionFilterGroups.DefaultFilter, CollisionFilterGroups.AllFilter); }
protected override void Initialize() { base.Initialize(); SetCameraDistance(50.0f); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new DefaultCollisionConfiguration(); //m_collisionConfiguration.setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); m_broadphase = new DbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) m_constraintSolver = new SequentialImpulseConstraintSolver(); m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); IndexedVector3 gravity = new IndexedVector3(0, -10, 0); m_dynamicsWorld.SetGravity(ref gravity); // NEW => btGhostPairCallback ================================= m_ghostPairCallback = new GhostPairCallback(); m_dynamicsWorld.GetBroadphase().GetOverlappingPairCache().SetInternalGhostPairCallback(m_ghostPairCallback); // Needed once to enable ghost objects inside Bullet // NEW => btGhostObject ======================================= m_ghostObject = new GhostObject(); CollisionShape shape = new BoxShape(new IndexedVector3(5f)); // As far as I know only the world aabb of the shape will be used (i.e. a box always parallel to the x,y,z axis of variable size) m_collisionShapes.Add(shape); m_ghostObject.SetCollisionShape(shape); m_ghostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE); // We can choose to make it "solid" if we want... m_dynamicsWorld.AddCollisionObject(m_ghostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger); //m_ghostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,-15))); IndexedMatrix im = IndexedMatrix.CreateFromQuaternion(quatDeg45Y); im._origin = new IndexedVector3(0, 5, -15); m_ghostObject.SetWorldTransform(im); // NEW => btPairCachingGhostObject ============================ m_pairCachingGhostObject = new PairCachingGhostObject(); shape = new ConeShape(7.0f, 14.0f); m_collisionShapes.Add(shape); m_pairCachingGhostObject.SetCollisionShape(shape); m_pairCachingGhostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE); // We can choose to make it "solid" if we want... m_dynamicsWorld.AddCollisionObject(m_pairCachingGhostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger); //m_pairCachingGhostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,15))); im._origin = new IndexedVector3(0, 7, 15); m_pairCachingGhostObject.SetWorldTransform(im); //============================================================= ///create a few basic rigid bodies CollisionShape groundShape = new BoxShape(new IndexedVector3(50)); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.Identity; groundTransform._origin = new IndexedVector3(0, -50, 0); float mass = 0.0f; LocalCreateRigidBody(mass, groundTransform, groundShape); // spawn some cubes (code pasted from appBasicDemo...) if (true) { //create a few dynamic rigidbodies CollisionShape colShape = new BoxShape(new IndexedVector3(SCALING, SCALING, SCALING)); //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); //CollisionShape colShape = new CylinderShape(new IndexedVector3(1f, 1, 1f)); m_collisionShapes.Add(colShape); /// Create Dynamic Objects IndexedMatrix startTransform = IndexedMatrix.Identity; mass = 1f; //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = mass != 0f; IndexedVector3 localInertia = IndexedVector3.Zero; if (isDynamic) { colShape.CalculateLocalInertia(mass, out localInertia); } float start_x = START_POS_X - ARRAY_SIZE_X / 2; float start_y = START_POS_Y; float start_z = START_POS_Z - ARRAY_SIZE_Z / 2; for (int k = 0; k < ARRAY_SIZE_Y; k++) { for (int i = 0; i < ARRAY_SIZE_X; i++) { for (int j = 0; j < ARRAY_SIZE_Z; j++) { startTransform._origin = (new IndexedVector3(2.0f * i + start_x, 20 + 2.0f * k + start_y, 2.0f * j + start_z) * SCALING); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform, IndexedMatrix.Identity); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); RigidBody body = new RigidBody(rbInfo); //body.setContactProcessingThreshold(colShape.getContactBreakingThreshold()); body.SetActivationState(ActivationState.ISLAND_SLEEPING); m_dynamicsWorld.AddRigidBody(body); body.SetActivationState(ActivationState.ISLAND_SLEEPING); body.SetUserPointer(String.Format("Box X{0} Y{1} Z{2}", k, i, j)); } } } } ClientResetScene(); }
// ========================================================================== Add PhyBody to physics world === private void _AddPhyBody(PhyBody phyBody) { World.AddCollisionObject(phyBody.body, (CollisionFilterGroups)(1 << phyBody.phyType), (CollisionFilterGroups)_collisionsMap[phyBody.phyType]); }
public override void AddCollision(CollisionProxy colProxy, int layer = 1, int mask = -1) { world.AddCollisionObject((CollisionObject)colProxy.CollisionObject, (short)layer, (short)mask); }
public override void InitializeDemo() { base.InitializeDemo(); SetCameraDistance(50f); //string filename = @"E:\users\man\bullet\xna-basic-output-1.txt"; //FileStream filestream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read); //BulletGlobals.g_streamWriter = new StreamWriter(filestream); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new DefaultCollisionConfiguration(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); IndexedVector3 worldMin = new IndexedVector3(-1000, -1000, -1000); IndexedVector3 worldMax = -worldMin; m_broadphase = new AxisSweep3Internal(ref worldMin, ref worldMax, 0xfffe, 0xffff, 16384, null, false); //pairCache = new SortedOverlappingPairCache(); //m_broadphase = new SimpleBroadphase(1000, pairCache); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) SequentialImpulseConstraintSolver sol = new SequentialImpulseConstraintSolver(); m_constraintSolver = sol; m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); m_dynamicsWorld.GetDispatchInfo().SetAllowedCcdPenetration(0.0001f); IndexedVector3 gravity = new IndexedVector3(0, -10, 0); m_dynamicsWorld.SetGravity(ref gravity); ///create a few basic rigid bodies IndexedVector3 halfExtents = new IndexedVector3(50, 50, 50); //IndexedVector3 halfExtents = new IndexedVector3(10, 10, 10); CollisionShape groundShape = new BoxShape(ref halfExtents); //CollisionShape groundShape = new StaticPlaneShape(new IndexedVector3(0,1,0), 50); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(new IndexedVector3(0, -50, 0)); //IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(new IndexedVector3(0,-10,0)); float mass = 0f; LocalCreateRigidBody(mass, ref groundTransform, groundShape); #region CharacterController IndexedMatrix startTransform = IndexedMatrix.Identity; //startTransform.setOrigin (btVector3(0.0, 4.0, 0.0)); startTransform._origin = new IndexedVector3(10.210098f, -1.6433364f, 16.453260f); m_ghostObject = new PairCachingGhostObject(); m_ghostObject.SetWorldTransform(startTransform); m_broadphase.GetOverlappingPairCache().SetInternalGhostPairCallback(new GhostPairCallback()); float characterHeight = 1.75f; float characterWidth = 1.75f; ConvexShape capsule = new CapsuleShape(characterWidth, characterHeight); m_ghostObject.SetCollisionShape(capsule); m_ghostObject.SetCollisionFlags(CollisionFlags.CF_CHARACTER_OBJECT); float stepHeight = 0.35f; int upAxis = 1; m_character = new KinematicCharacterController(m_ghostObject, capsule, stepHeight, upAxis); m_dynamicsWorld.AddCollisionObject(m_ghostObject, CollisionFilterGroups.CharacterFilter, CollisionFilterGroups.StaticFilter | CollisionFilterGroups.DefaultFilter); m_dynamicsWorld.AddAction(m_character); #endregion }
public override void InitializeDemo() { // Setup the basic world SetTexturing(true); SetShadows(true); SetCameraDistance(5.0f); m_collisionConfiguration = new DefaultCollisionConfiguration(); m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); IndexedVector3 worldAabbMin = new IndexedVector3(-10000,-10000,-10000); IndexedVector3 worldAabbMax = new IndexedVector3(10000,10000,10000); //m_broadphase = new AxisSweep3Internal(ref worldAabbMin, ref worldAabbMax, 0xfffe, 0xffff, 16384, null, true); m_broadphase = new SimpleBroadphase(1000, null); m_constraintSolver = new SequentialImpulseConstraintSolver(); m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); //m_dynamicsWorld.getDispatchInfo().m_useConvexConservativeDistanceUtil = true; //m_dynamicsWorld.getDispatchInfo().m_convexConservativeDistanceThreshold = 0.01f; // Setup a big ground box { CollisionShape groundShape = new BoxShape(new IndexedVector3(200.0f,10.0f,200.0f)); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.CreateTranslation(0,-10,0); CollisionObject fixedGround = new CollisionObject(); fixedGround.SetCollisionShape(groundShape); fixedGround.SetWorldTransform(ref groundTransform); fixedGround.SetUserPointer("Ground"); m_dynamicsWorld.AddCollisionObject(fixedGround); } // Spawn one ragdoll IndexedVector3 startOffset = new IndexedVector3(1,0.5f,0); //string filename = @"c:\users\man\bullet\xna-ragdoll-constraints-output.txt"; //FileStream filestream = File.Open(filename, FileMode.Create, FileAccess.Write, FileShare.Read); //BulletGlobals.g_streamWriter = new StreamWriter(filestream); SpawnRagdoll(ref startOffset, BulletGlobals.g_streamWriter); //startOffset = new IndexedVector3(-1,0.5f,0); //spawnRagdoll(ref startOffset); ClientResetScene(); }
static void TestGCCollection() { var conf = new DefaultCollisionConfiguration(); var dispatcher = new CollisionDispatcher(conf); var broadphase = new DbvtBroadphase(); //var broadphase = new AxisSweep3(new Vector3(-1000, -1000, -1000), new Vector3(1000, 1000, 1000)); world = new DiscreteDynamicsWorld(dispatcher, broadphase, null, conf); world.Gravity = new Vector3(0, -10, 0); dispatcher.NearCallback = DispatcherNearCallback; CreateBody(0.0f, new BoxShape(50, 1, 50), Vector3.Zero); var dynamicObject = CreateBody(10.0f, new SphereShape(1.0f), new Vector3(2, 2, 0)); var dynamicObject2 = CreateBody(1.0f, new SphereShape(1.0f), new Vector3(0, 2, 0)); var ghostPairCallback = new GhostPairCallback(); broadphase.OverlappingPairCache.SetInternalGhostPairCallback(ghostPairCallback); AddToDisposeQueue(ghostPairCallback); ghostPairCallback = null; var ghostObject = new PairCachingGhostObject(); ghostObject.CollisionShape = new BoxShape(2); ghostObject.WorldTransform = Matrix.Translation(2, 2, 0); world.AddCollisionObject(ghostObject); var trimesh = new TriangleMesh(); Vector3 v0 = new Vector3(0, 0, 0); Vector3 v1 = new Vector3(1, 0, 0); Vector3 v2 = new Vector3(0, 1, 0); Vector3 v3 = new Vector3(1, 1, 0); trimesh.AddTriangle(v0, v1, v2); trimesh.AddTriangle(v1, v3, v2); var triangleMeshShape = new BvhTriangleMeshShape(trimesh, false); var triMeshObject = CreateBody(0, triangleMeshShape, new Vector3(20, 0, 20)); AddToDisposeQueue(triangleMeshShape); AddToDisposeQueue(trimesh); AddToDisposeQueue(triMeshObject); triangleMeshShape = null; trimesh = null; AddToDisposeQueue(conf); AddToDisposeQueue(dispatcher); AddToDisposeQueue(broadphase); AddToDisposeQueue(world); //conf.Dispose(); conf = null; //dispatcher.Dispose(); dispatcher = null; //broadphase.Dispose(); broadphase = null; world.DebugDrawer = new DebugDrawTest(); AddToDisposeQueue(world.DebugDrawer); world.SetInternalTickCallback(WorldPreTickCallback); for (int i = 0; i < 600; i++) { world.StepSimulation(1.0f / 60.0f); } world.DispatchInfo.DebugDraw = new DebugDrawTest2(); AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DispatchInfo.DebugDraw = world.DispatchInfo.DebugDraw; AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DispatchInfo.DebugDraw = null; world.DebugDrawer = null; world.DebugDrawer = new DebugDrawTest2(); world.StepSimulation(1.0f / 60.0f); world.DebugDrawWorld(); AddToDisposeQueue(world.DispatchInfo.DebugDraw); world.DebugDrawer = new DebugDrawTest(); world.DebugDrawWorld(); AddToDisposeQueue(world.DebugDrawer); world.DebugDrawer = null; TestContactTest(dynamicObject, dynamicObject2); TestGhostObjectPairs(ghostObject); TestRayCast(dynamicObject); TestTriangleMeshRayCast(triMeshObject); dynamicObject = null; dynamicObject2 = null; triMeshObject = null; //world.SetInternalTickCallback(null); world.Dispose(); world = null; GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced); GC.WaitForPendingFinalizers(); TestWeakRefs(); disposeQueue.Clear(); }
protected override void Initialize() { base.Initialize(); SetCameraDistance(50.0f); ///collision configuration contains default setup for memory, collision setup m_collisionConfiguration = new DefaultCollisionConfiguration(); //m_collisionConfiguration.setConvexConvexMultipointIterations(); ///use the default collision dispatcher. For parallel processing you can use a diffent dispatcher (see Extras/BulletMultiThreaded) m_dispatcher = new CollisionDispatcher(m_collisionConfiguration); m_broadphase = new DbvtBroadphase(); ///the default constraint solver. For parallel processing you can use a different solver (see Extras/BulletMultiThreaded) m_constraintSolver = new SequentialImpulseConstraintSolver(); m_dynamicsWorld = new DiscreteDynamicsWorld(m_dispatcher, m_broadphase, m_constraintSolver, m_collisionConfiguration); IndexedVector3 gravity = new IndexedVector3(0, -10, 0); m_dynamicsWorld.SetGravity(ref gravity); // NEW => btGhostPairCallback ================================= m_ghostPairCallback = new GhostPairCallback(); m_dynamicsWorld.GetBroadphase().GetOverlappingPairCache().SetInternalGhostPairCallback(m_ghostPairCallback); // Needed once to enable ghost objects inside Bullet // NEW => btGhostObject ======================================= m_ghostObject = new GhostObject(); CollisionShape shape = new BoxShape(new IndexedVector3(5f)); // As far as I know only the world aabb of the shape will be used (i.e. a box always parallel to the x,y,z axis of variable size) m_collisionShapes.Add(shape); m_ghostObject.SetCollisionShape(shape); m_ghostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE); // We can choose to make it "solid" if we want... m_dynamicsWorld.AddCollisionObject(m_ghostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger); //m_ghostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,-15))); IndexedMatrix im = IndexedMatrix.CreateFromQuaternion(quatDeg45Y); im._origin = new IndexedVector3(0, 5, -15); m_ghostObject.SetWorldTransform(im); // NEW => btPairCachingGhostObject ============================ m_pairCachingGhostObject = new PairCachingGhostObject(); shape = new ConeShape(7.0f, 14.0f); m_collisionShapes.Add(shape); m_pairCachingGhostObject.SetCollisionShape(shape); m_pairCachingGhostObject.SetCollisionFlags(CollisionFlags.CF_NO_CONTACT_RESPONSE); // We can choose to make it "solid" if we want... m_dynamicsWorld.AddCollisionObject(m_pairCachingGhostObject, CollisionFilterGroups.SensorTrigger, CollisionFilterGroups.AllFilter & ~CollisionFilterGroups.SensorTrigger); //m_pairCachingGhostObject.setWorldTransform(btTransform(btQuaternion::getIdentity(),btVector3(0,5,15))); im._origin = new IndexedVector3(0, 7, 15); m_pairCachingGhostObject.SetWorldTransform(im); //============================================================= ///create a few basic rigid bodies CollisionShape groundShape = new BoxShape(new IndexedVector3(50)); m_collisionShapes.Add(groundShape); IndexedMatrix groundTransform = IndexedMatrix.Identity; groundTransform._origin = new IndexedVector3(0, -50, 0); float mass = 0.0f; LocalCreateRigidBody(mass, groundTransform, groundShape); // spawn some cubes (code pasted from appBasicDemo...) if(true) { //create a few dynamic rigidbodies CollisionShape colShape = new BoxShape(new IndexedVector3(SCALING, SCALING, SCALING)); //btCollisionShape* colShape = new btSphereShape(btScalar(1.)); //CollisionShape colShape = new CylinderShape(new IndexedVector3(1f, 1, 1f)); m_collisionShapes.Add(colShape); /// Create Dynamic Objects IndexedMatrix startTransform = IndexedMatrix.Identity; mass = 1f; //rigidbody is dynamic if and only if mass is non zero, otherwise static bool isDynamic = mass != 0f; IndexedVector3 localInertia = IndexedVector3.Zero; if (isDynamic) { colShape.CalculateLocalInertia(mass, out localInertia); } float start_x = START_POS_X - ARRAY_SIZE_X / 2; float start_y = START_POS_Y; float start_z = START_POS_Z - ARRAY_SIZE_Z / 2; for (int k = 0; k < ARRAY_SIZE_Y; k++) { for (int i = 0; i < ARRAY_SIZE_X; i++) { for (int j = 0; j < ARRAY_SIZE_Z; j++) { startTransform._origin = (new IndexedVector3(2.0f * i + start_x, 20 + 2.0f * k + start_y, 2.0f * j + start_z) * SCALING); //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects DefaultMotionState myMotionState = new DefaultMotionState(startTransform, IndexedMatrix.Identity); RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia); RigidBody body = new RigidBody(rbInfo); //body.setContactProcessingThreshold(colShape.getContactBreakingThreshold()); body.SetActivationState(ActivationState.ISLAND_SLEEPING); m_dynamicsWorld.AddRigidBody(body); body.SetActivationState(ActivationState.ISLAND_SLEEPING); body.SetUserPointer(String.Format("Box X{0} Y{1} Z{2}", k, i, j)); } } } } ClientResetScene(); }