public bool Build( FeatureUpdateContext updateContext, [ParentModel] Point[] Points, double Size) { //-- Upkeep //-- if( physics == null ) { physics = new Sutd.Physics( ); } else { Regenerate( ); physics.Reset( ); } //-- Set Gravity //-- physics.world.Gravity = new Vec3D( 0, 0, -10 ); //-- Create Ground //-- var body = new Sutd.Physics.Body( ); { //-- Define Infinite Plane //-- var shape = new StaticPlaneShape( new Vec3D( 0, 0, 1 ), 0 ); physics.shapes.Add( shape ); //-- Set Physics State / Bullet //-- Fixed bodies have zero mass and inertia //-- var param = new RigidBodyConstructionInfo( mass: 0.0f, motionState: new DefaultMotionState( Mat4D.Identity ), collisionShape: shape, localInertia: Vec3D.Zero ); body.rigid = new RigidBody( param ); param.Dispose( ); physics.world.AddRigidBody( body.rigid ); body.matrix = body.rigid.WorldTransform; //-- Set Visual State / Rhino //-- Create a very thin but wide finite box //-- var transform = DTransform3d.Identity; AddBox( transform, new DVector3d( 50, 50, 0.01 ) ); body.solid = geometry[geometry.Count - 1]; } physics.bodies.Add( body ); //-- Create 3D Grid of Boxes //-- float half = (float)( Size * 0.5 ); foreach( var point in Points ) { body = new Sutd.Physics.Body( ); { //-- Collision Shape //-- var shape = new BoxShape( half, half, half ); physics.shapes.Add( shape ); //-- Mass Properties //-- var inertia = Vec3D.Zero; shape.CalculateLocalInertia( mass: 1.0f, inertia: out inertia ); //-- Physics State //-- var param = new RigidBodyConstructionInfo( mass: 1.0f, motionState: new DefaultMotionState( Mat4D.Identity ), collisionShape: shape, localInertia: inertia ); body.rigid = new RigidBody( param ); param.Dispose( ); physics.world.AddRigidBody( body.rigid ); body.rigid.Translate( new Vec3D( (float)point.X, (float)point.Y, (float)point.Z ) ); body.matrix = body.rigid.WorldTransform; //-- Visual State //-- var transform = DTransform3d.Identity; transform.Translation = point.DPoint3d; AddBox( transform, new DVector3d( Size, Size, Size ) ); body.solid = geometry[geometry.Count - 1]; } physics.bodies.Add( body ); } return true; }
Mesh CreateStaticPlaneShape(StaticPlaneShape shape) { // Load shader if (planeShader == null) { Assembly assembly = Assembly.GetExecutingAssembly(); Stream shaderStream = assembly.GetManifestResourceStream("DemoFramework.SlimDX.checker_shader.fx"); planeShader = Effect.FromStream(device, shaderStream, ShaderFlags.None); } Mesh mesh = CreateShape(shape); shapes.Remove(shape); planeShapes.Add(shape, mesh); return mesh; }
public ContinuousConvexCollision(ConvexShape shapeA, StaticPlaneShape plane) : base(btContinuousConvexCollision_new2(shapeA._native, plane._native)) { }
public static void CreateStaticPlane(StaticPlaneShape shape, Mesh m) { UnityEngine.Vector3 planeOrigin = shape.PlaneNormal.ToUnity() * shape.PlaneConstant; UnityEngine.Vector3 vec0, vec1; PlaneSpace1(shape.PlaneNormal.ToUnity(), out vec0, out vec1); const float size = 1000f; int[] indices = new int[] { 0, 2, 1, 0, 1, 3 }; UnityEngine.Vector3[] verts = new UnityEngine.Vector3[] { planeOrigin + vec0*size, planeOrigin - vec0*size, planeOrigin + vec1*size, planeOrigin - vec1*size, }; m.Clear(); m.vertices = verts; m.triangles = indices; m.RecalculateBounds(); m.RecalculateNormals(); }
protected override void OnInitializePhysics() { // collision configuration contains default setup for memory, collision setup CollisionConf = new DefaultCollisionConfiguration(); Dispatcher = new CollisionDispatcher(CollisionConf); //Broadphase = new SimpleBroadphase(); Broadphase = new AxisSweep3_32Bit(new Vector3(-10000, -10000, -10000), new Vector3(10000, 10000, 10000), 1024); Solver = new SequentialImpulseConstraintSolver(); World = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf); World.Gravity = new Vector3(0, -10, 0); // create trimesh model and shape InitGImpactCollision(); // Create Scene float mass = 0.0f; CollisionShape staticboxShape1 = new BoxShape(200, 1, 200);//floor CollisionShapes.Add(staticboxShape1); LocalCreateRigidBody(mass, Matrix.Translation(0, -10, 0), staticboxShape1); CollisionShape staticboxShape2 = new BoxShape(1, 50, 200);//left wall CollisionShapes.Add(staticboxShape2); LocalCreateRigidBody(mass, Matrix.Translation(-200, 15, 0), staticboxShape2); CollisionShape staticboxShape3 = new BoxShape(1, 50, 200);//right wall CollisionShapes.Add(staticboxShape3); LocalCreateRigidBody(mass, Matrix.Translation(200, 15, 0), staticboxShape3); CollisionShape staticboxShape4 = new BoxShape(200, 50, 1);//front wall CollisionShapes.Add(staticboxShape4); LocalCreateRigidBody(mass, Matrix.Translation(0, 15, 200), staticboxShape4); CollisionShape staticboxShape5 = new BoxShape(200, 50, 1);//back wall CollisionShapes.Add(staticboxShape5); LocalCreateRigidBody(mass, Matrix.Translation(0, 15, -200), staticboxShape5); //static plane Vector3 normal = new Vector3(-0.5f, 0.5f, 0.0f); normal.Normalize(); CollisionShape staticplaneShape6 = new StaticPlaneShape(normal, 0.5f);// A plane CollisionShapes.Add(staticplaneShape6); /*RigidBody staticBody2 =*/ LocalCreateRigidBody(mass, Matrix.Translation(0, -9, 0), staticplaneShape6); //another static plane normal = new Vector3(0.5f, 0.7f, 0.0f); //normal.Normalize(); CollisionShape staticplaneShape7 = new StaticPlaneShape(normal, 0.0f);// A plane CollisionShapes.Add(staticplaneShape7); /*staticBody2 =*/ LocalCreateRigidBody(mass, Matrix.Translation(0, -10, 0), staticplaneShape7); // Create Static Torus float height = 28; const float step = 2.5f; const float massT = 1.0f; Matrix startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll((float)Math.PI * 0.5f, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); #if BULLET_GIMPACT kinematicTorus = LocalCreateRigidBody(0, startTransform, trimeshShape); #else //kinematicTorus = LocalCreateRigidBody(0, startTransform, CreateTorusShape()); #endif //kinematicTorus.CollisionFlags = kinematicTorus.CollisionFlags | CollisionFlags.StaticObject; //kinematicTorus.ActivationState = ActivationState.IslandSleeping; kinematicTorus.CollisionFlags = kinematicTorus.CollisionFlags | CollisionFlags.KinematicObject; kinematicTorus.ActivationState = ActivationState.DisableDeactivation; // Kinematic //kinTorusTran = new Vector3(-0.1f, 0, 0); //kinTorusRot = Quaternion.RotationYawPitchRoll(0, (float)Math.PI * 0.01f, 0); #if TEST_GIMPACT_TORUS #if BULLET_GIMPACT // Create dynamic Torus for (int i = 0; i < 6; i++) { height -= step; startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll(0, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); /*RigidBody bodyA =*/ LocalCreateRigidBody(massT, startTransform, trimeshShape); height -= step; startTransform = Matrix.RotationQuaternion(Quaternion.RotationYawPitchRoll((float)Math.PI * 0.5f, 0, (float)Math.PI * 0.5f)) * Matrix.Translation(0, height, -5); /*RigidBody bodyB =*/ LocalCreateRigidBody(massT, startTransform, trimeshShape); } #else /* // Create dynamic Torus for (int i = 0; i < 6; i++) { height -= step; startTransform.setOrigin(btVector3(0, height, -5)); startTransform.setRotation(btQuaternion(0, 0, 3.14159265 * 0.5)); btRigidBody* bodyA = localCreateRigidBody(massT, startTransform, createTorusShape()); height -= step; startTransform.setOrigin(btVector3(0, height, -5)); startTransform.setRotation(btQuaternion(3.14159265 * 0.5, 0, 3.14159265 * 0.5)); btRigidBody* bodyB = localCreateRigidBody(massT, startTransform, createTorusShape()); } */ #endif #endif // Create Dynamic Boxes for (int i = 0; i < 8; i++) { CollisionShape boxShape = new BoxShape(new Vector3(1, 1, 1)); CollisionShapes.Add(boxShape); LocalCreateRigidBody(1, Matrix.Translation(2 * i - 5, 2, -3), boxShape); } }
public static UnityEngine.Vector3[] CreateStaticPlane(StaticPlaneShape shape, out int[] indices) { UnityEngine.Vector3 planeOrigin = shape.PlaneNormal.ToUnity() * shape.PlaneConstant; Vector3 vec0, vec1; PlaneSpace1(shape.PlaneNormal, out vec0, out vec1); const float size = 1000; indices = new int[] { 0, 2, 1, 0, 1, 3 }; return new UnityEngine.Vector3[] { planeOrigin + vec0.ToUnity()*size, shape.PlaneNormal.ToUnity(), planeOrigin - vec0.ToUnity()*size, shape.PlaneNormal.ToUnity(), planeOrigin + vec1.ToUnity()*size, shape.PlaneNormal.ToUnity(), planeOrigin - vec1.ToUnity()*size, shape.PlaneNormal.ToUnity() }; }
public CollisionShape CreatePlaneShape(ref Vector3 planeNormal, float planeConstant) { StaticPlaneShape shape = new StaticPlaneShape(planeNormal, planeConstant); _allocatedCollisionShapes.Add(shape); return shape; }
public static Vector3[] CreateStaticPlane(StaticPlaneShape shape, out uint[] indices) { Vector3 planeOrigin = shape.PlaneNormal * shape.PlaneConstant; Vector3 vec0, vec1; PlaneSpace1(shape.PlaneNormal, out vec0, out vec1); const float size = 1000; indices = new uint[] { 1, 2, 0, 1, 3, 0 }; return new Vector3[] { planeOrigin + vec0*size, shape.PlaneNormal, planeOrigin - vec0*size, shape.PlaneNormal, planeOrigin + vec1*size, shape.PlaneNormal, planeOrigin - vec1*size, shape.PlaneNormal }; }
/// <summary> /// Create a static ground plane facing upwards. /// </summary> /// <param name="yposition">The Y position that the plane is located at.</param> void CreateGroundPlane(float yposition) { // make an infinite plane so we don't fall forever. TODO: hook up an event so when we collide with this, we respawn back on the track Matrix4 matrix = new Matrix4(); matrix.MakeTransform(new Vector3(0, yposition, 0), Vector3.UNIT_SCALE, new Quaternion(0, 0, 0, 1)); CollisionShape groundShape = new StaticPlaneShape(Vector3.NEGATIVE_UNIT_Y, 1); var groundInfo = new RigidBodyConstructionInfo(0, new DefaultMotionState(matrix), groundShape, Vector3.ZERO); var groundBody = new RigidBody(groundInfo); groundBody.UserObject = new CollisionObjectDataHolder(groundBody, PonykartCollisionGroups.Environment, "ground"); groundBody.CollisionFlags = CollisionFlags.StaticObject | CollisionFlags.DisableVisualizeObject; world.AddRigidBody(groundBody, PonykartCollisionGroups.Environment, PonykartCollidesWithGroups.Environment); }
Mesh CreateStaticPlaneShape(StaticPlaneShape shape) { // Load shader if (planeShader == null) { Assembly assembly = Assembly.GetExecutingAssembly(); Stream shaderStream = assembly.GetManifestResourceStream("DemoFramework.checker_shader.fx"); planeShader = Effect.FromStream(device, shaderStream, ShaderFlags.None); } Vector3[] vertices = new Vector3[4 * 2]; Mesh mesh = new Mesh(device, 2, 4, MeshFlags.SystemMemory, VertexFormat.Position | VertexFormat.Normal); Vector3 planeOrigin = shape.PlaneNormal * shape.PlaneConstant; Vector3 vec0, vec1; PlaneSpace1(shape.PlaneNormal, out vec0, out vec1); float size = 1000; Vector3[] verts = new Vector3[4] { planeOrigin + vec0*size, planeOrigin - vec0*size, planeOrigin + vec1*size, planeOrigin - vec1*size }; SlimDX.DataStream vertexBuffer = mesh.LockVertexBuffer(LockFlags.Discard); vertexBuffer.Write(verts[0]); vertexBuffer.Position += 12; vertexBuffer.Write(verts[1]); vertexBuffer.Position += 12; vertexBuffer.Write(verts[2]); vertexBuffer.Position += 12; vertexBuffer.Write(verts[3]); vertexBuffer.Position += 12; mesh.UnlockVertexBuffer(); SlimDX.DataStream indexBuffer = mesh.LockIndexBuffer(LockFlags.Discard); indexBuffer.Write((short)1); indexBuffer.Write((short)2); indexBuffer.Write((short)0); indexBuffer.Write((short)1); indexBuffer.Write((short)3); indexBuffer.Write((short)0); mesh.UnlockIndexBuffer(); mesh.ComputeNormals(); complexShapes.Add(shape, mesh); return mesh; }