public RigidBodyConstructionInfo(float mass, MotionState motionState, CollisionShape collisionShape)
 {
     _native = btRigidBody_btRigidBodyConstructionInfo_new(mass, (motionState != null) ? motionState._native : IntPtr.Zero,
         (collisionShape != null) ? collisionShape._native : IntPtr.Zero);
     _collisionShape = collisionShape;
     _motionState = motionState;
 }
 public RigidBodyConstructionInfo(float mass, MotionState motionState, CollisionShape collisionShape, Vector3 localInertia)
 {
     _native = btRigidBody_btRigidBodyConstructionInfo_new2(mass, (motionState != null) ? motionState._native : IntPtr.Zero,
         (collisionShape != null) ? collisionShape._native : IntPtr.Zero, ref localInertia);
     _collisionShape = collisionShape;
     _motionState = motionState;
 }
Esempio n. 3
0
 public PhysicalBody(RigidBody rigidBody, CollisionShape shape, TransformationManager manager)
 {
     Body = rigidBody;
     Shape = shape;
     Transformation = manager;
     Enabled = false;
 }
		public unsafe static void AddChildShape(this CompoundShape obj, ref OpenTK.Matrix4 localTransform, CollisionShape shape)
		{
			fixed (OpenTK.Matrix4* localTransformPtr = &localTransform)
			{
				obj.AddChildShape(ref *(BulletSharp.Math.Matrix*)localTransformPtr, shape);
			}
		}
Esempio n. 5
0
	void Awake ()
	{
		shape = createShape ();
		myTransform = transform;
		previousScale = myTransform.localScale;
		shape.LocalScaling = new BulletSharp.Vector3 (previousScale.x, previousScale.y, previousScale.z);
	}
Esempio n. 6
0
 public override void RemoveShape(CollisionShape shape)
 {
     if (shapes.ContainsKey(shape))
     {
         shapes[shape].Dispose();
         shapes.Remove(shape);
     }
 }
        public BulletPhysicObject(CollisionShape CollisionShape, Vector3 translation, Matrix rotation, Vector3 Scale, float mass, CollisionFilterGroups CollisionFilterGroup = CollisionFilterGroups.DefaultFilter,CollisionFilterGroups collisionFilterMask = CollisionFilterGroups.DefaultFilter)
        {
            shape  = CollisionShape;
            this.scale = Scale;
            this.mass = mass;
            shape.LocalScaling = Scale;
            obj = LocalCreateRigidBody(mass, Matrix.CreateTranslation(translation) * rotation, CollisionShape);            

        }
        Mesh CreateShape(CollisionShape shape)
        {
            uint[] indices;
            BulletSharp.Math.Vector3[] vertices = CreateShape(shape, out indices);

            int vertexCount = vertices.Length / 2;
            int indexCount = (indices != null) ? indices.Length : vertexCount;
            bool index32 = indexCount > 65536;

            Mesh mesh = new Mesh(device, indexCount / 3, vertexCount,
                MeshFlags.SystemMemory | (index32 ? MeshFlags.Use32Bit : 0), VertexFormat.Position | VertexFormat.Normal);

            DataStream vertexBuffer = mesh.LockVertexBuffer(LockFlags.Discard);
            vertexBuffer.WriteRange(vertices);
            mesh.UnlockVertexBuffer();

            DataStream indexBuffer = mesh.LockIndexBuffer(LockFlags.Discard);
            if (index32)
            {
                if (indices == null)
                {
                    indices = new uint[indexCount];
                    uint i = 0;
                    while (i < indexCount)
                    {
                        indices[i] = i;
                        i++;
                    }
                }
                indexBuffer.WriteRange(indices);
            }
            else
            {
                ushort[] indices_s;
                if (indices == null)
                {
                    indices_s = new ushort[indexCount];
                    ushort i = 0;
                    while (i < indexCount)
                    {
                        indices_s[i] = i;
                        i++;
                    }
                }
                else
                {
                    indices_s = CompactIndexBuffer(indices);
                }
                indexBuffer.WriteRange(indices_s);
            }
            mesh.UnlockIndexBuffer();

            shapes.Add(shape, mesh);
            return mesh;
        }
Esempio n. 9
0
        public override RigidBody CreateRigidBody(bool isDynamic, float mass, Matrix startTransform, CollisionShape shape, string bodyName)
        {
            RigidBody body = base.CreateRigidBody(isDynamic, mass, startTransform, shape, bodyName);

            if (bodyName != null && bodyName.Equals("GroundName"))
                body.UserObject = "Ground";

            if (shape.ShapeType == BroadphaseNativeType.StaticPlaneShape)
                body.UserObject = "Ground";

            return body;
        }
Esempio n. 10
0
        public Prop(CollisionShape shape, float mass, Matrix4 start)
        {
            Physics physics = Game.Instance.Physics;

            Shape = shape;
            IsDynamic = (mass != 0.0f);
            physics.Props.Add(this);
            Vector3 inertia = Vector3.Zero;
            if (IsDynamic) shape.CalculateLocalInertia(mass, out inertia);
            DefaultMotionState motionState = new DefaultMotionState(start);
            RigidBody.RigidBodyConstructionInfo info = new RigidBody.RigidBodyConstructionInfo(mass, motionState, shape, inertia);
            Body = new RigidBody(info);
            physics.World.AddRigidBody(Body);
        }
Esempio n. 11
0
 RigidBody CreateBody(float mass, CollisionShape shape, Vector3 offset)
 {
     using (var info = new RigidBodyConstructionInfo(mass, new DefaultMotionState(), shape, Vector3.Zero))
     {
         if (mass != 0.0f)
         {
             info.LocalInertia = info.CollisionShape.CalculateLocalInertia(mass);
         }
         var collisionObject = new RigidBody(info);
         collisionObject.Translate(offset);
         world.AddRigidBody(collisionObject);
         return collisionObject;
     }
 }
        protected RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;
            if (isDynamic)
                shape.CalculateLocalInertia(mass, out localInertia);

            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            body = new RigidBody(rbInfo);
            
            return body;
        }
Esempio n. 13
0
        static RigidBody CreateBody(float mass, CollisionShape shape, Vector3 offset)
        {
            var constInfo = new RigidBodyConstructionInfo(mass, new DefaultMotionState(), shape, Vector3.Zero);
            if (mass != 0.0f)
            {
                constInfo.LocalInertia = constInfo.CollisionShape.CalculateLocalInertia(mass);
            }
            var collisionObject = new RigidBody(constInfo);
            collisionObject.Translate(offset);
            world.AddRigidBody(collisionObject);

            AddToDisposeQueue(constInfo);
            AddToDisposeQueue(constInfo.MotionState);
            AddToDisposeQueue(collisionObject);
            AddToDisposeQueue(shape);

            return collisionObject;
        }
Esempio n. 14
0
        public virtual RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;
            if (isDynamic)
                shape.CalculateLocalInertia(mass, out localInertia);

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
            DefaultMotionState myMotionState = new DefaultMotionState(startTransform);

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);

            World.AddRigidBody(body);

            return body;
        }
        public static void CreateCube(CollisionShape cs, Mesh mesh)
        {
            BulletSharp.Math.Vector3 ext = ((BoxShape)cs).HalfExtentsWithMargin;
            float length = ext.X * 2f;
            float width = ext.Y * 2f;
            float height = ext.Z * 2f;

            UnityEngine.Vector3 p0 = new UnityEngine.Vector3(-length * .5f, -width * .5f, height * .5f);
            UnityEngine.Vector3 p1 = new UnityEngine.Vector3(length * .5f, -width * .5f, height * .5f);
            UnityEngine.Vector3 p2 = new UnityEngine.Vector3(length * .5f, -width * .5f, -height * .5f);
            UnityEngine.Vector3 p3 = new UnityEngine.Vector3(-length * .5f, -width * .5f, -height * .5f);

            UnityEngine.Vector3 p4 = new UnityEngine.Vector3(-length * .5f, width * .5f, height * .5f);
            UnityEngine.Vector3 p5 = new UnityEngine.Vector3(length * .5f, width * .5f, height * .5f);
            UnityEngine.Vector3 p6 = new UnityEngine.Vector3(length * .5f, width * .5f, -height * .5f);
            UnityEngine.Vector3 p7 = new UnityEngine.Vector3(-length * .5f, width * .5f, -height * .5f);

            MakeUnityCubeMesh(p0, p1, p2, p3, p4, p5, p6, p7, mesh);
        }
Esempio n. 16
0
        void CreateStack(CollisionShape boxShape, int size, float zPos)
        {
            Matrix trans;
            float mass = 1.0f;

            for (int i = 0; i < size; i++)
            {
                // This constructs a row, from left to right
                int rowSize = size - i;
                for (int j = 0; j < rowSize; j++)
                {
                    trans = Matrix.Translation(
                        -rowSize * CubeHalfExtents + CubeHalfExtents + j * 2.0f * CubeHalfExtents,
                        CubeHalfExtents + i * CubeHalfExtents * 2.0f,
                        zPos);

                    RigidBody body = LocalCreateRigidBody(mass, trans, boxShape);
                    body.ActivationState = ActivationState.IslandSleeping;
                }
            }
        }
        public override RigidBody LocalCreateRigidBody(float mass, Matrix startTransform, CollisionShape shape)
        {
            //rigidbody is dynamic if and only if mass is non zero, otherwise static
            bool isDynamic = (mass != 0.0f);

            Vector3 localInertia = Vector3.Zero;
            if (isDynamic)
                shape.CalculateLocalInertia(mass, out localInertia);

            //using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects

            RigidBodyConstructionInfo rbInfo = new RigidBodyConstructionInfo(mass, null, shape, localInertia);
            RigidBody body = new RigidBody(rbInfo);
            rbInfo.Dispose();
            body.ContactProcessingThreshold = defaultContactProcessingThreshold;
            body.WorldTransform = startTransform;

            World.AddRigidBody(body);

            return body;
        }
 public static Vector3[] CreateShape(CollisionShape shape, out uint[] indices)
 {
     switch (shape.ShapeType)
     {
         case BroadphaseNativeType.BoxShape:
             indices = null;
             return CreateBox(shape as BoxShape);
         case BroadphaseNativeType.Box2DShape:
             indices = null;
             return CreateBox2DShape(shape as Box2DShape);
         case BroadphaseNativeType.CapsuleShape:
             return CreateCapsule(shape as CapsuleShape, out indices);
         case BroadphaseNativeType.Convex2DShape:
             return CreateShape((shape as Convex2DShape).ChildShape, out indices);
         case BroadphaseNativeType.ConvexHullShape:
             indices = null;
             return CreateConvexHull(shape as ConvexHullShape);
         case BroadphaseNativeType.ConeShape:
             return CreateCone(shape as ConeShape, out indices);
         case BroadphaseNativeType.CylinderShape:
             return CreateCylinder(shape as CylinderShape, out indices);
         case BroadphaseNativeType.GImpactShape:
             indices = null;
             return CreateTriangleMesh((shape as GImpactMeshShape).MeshInterface);
         case BroadphaseNativeType.MultiSphereShape:
             return CreateMultiSphere(shape as MultiSphereShape, out indices);
         case BroadphaseNativeType.SphereShape:
             return CreateSphere(shape as SphereShape, out indices);
         case BroadphaseNativeType.StaticPlaneShape:
             return CreateStaticPlane(shape as StaticPlaneShape, out indices);
         case BroadphaseNativeType.TriangleMeshShape:
             indices = null;
             return CreateTriangleMesh((shape as TriangleMeshShape).MeshInterface);
     }
     if (shape is PolyhedralConvexShape)
     {
         return CreatePolyhedralConvexShape((shape as PolyhedralConvexShape), out indices);
     }
     throw new NotImplementedException();
 }
Esempio n. 19
0
 public void AddChildShape(Matrix localTransform, CollisionShape shape)
 {
     _childShapes.Add(shape);
     btGImpactCompoundShape_addChildShape(Native, ref localTransform, shape.Native);
 }
 bool MyCompoundChildShapeCallback(CollisionShape shape0, CollisionShape shape1)
 {
     return true;
 }
Esempio n. 21
0
 public void AddChildShape(CollisionShape shape)
 {
     _childShapes.Add(shape);
     btGImpactCompoundShape_addChildShape2(Native, shape.Native);
 }
Esempio n. 22
0
        public void SetUpBulletPhysicsBody(float mass, BulletSharp.MotionState motionState, BulletSharp.CollisionShape collisionShape, Vector3 localInertia)
        {
            BulletSharpPhysics.RigidBodyConstructionInfo rbInfo =
                new BulletSharpPhysics.RigidBodyConstructionInfo(mass, motionState, collisionShape, localInertia);
            RigidBody = new BulletSharpPhysics.RigidBody(rbInfo);

            bulletPhysics.World.AddRigidBody(RigidBody, GetCollisionFlags(), GetCollisionMask());
        }
Esempio n. 23
0
        void InitSoftBodyInstance(SoftBody softBody, CollisionShape shape)
        {
            var shapeData = InitShapeData(shape);
            shapeData.Instances.Add(new InstanceData()
            {
                WorldTransform = Matrix.Identity,
                Color = softBodyColor
            });

            UpdateSoftBody(softBody, shapeData);
        }
Esempio n. 24
0
        ShapeData InitShapeData(CollisionShape shape)
        {
            ShapeData shapeData;

            if (shapes.TryGetValue(shape, out shapeData) == false)
            {
                if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape)
                {
                    shapeData = new ShapeData();
                }
                else
                {
                    shapeData = CreateShape(shape);
                }

                // Create an initial instance data buffer for a single instance
                instanceDataDesc.SizeInBytes = InstanceData.SizeInBytes;
                shapeData.InstanceDataBuffer = new Buffer(device, instanceDataDesc);
                shapeData.BufferBindings[1] = new VertexBufferBinding(shapeData.InstanceDataBuffer, instanceDataDesc.SizeInBytes, 0);

                shapes.Add(shape, shapeData);
            }

            return shapeData;
        }
 public void GImpactVsShape(CollisionObjectWrapper body0Wrap, CollisionObjectWrapper body1Wrap,
                            GImpactShapeInterface shape0, CollisionShape shape1, bool swapped)
 {
     btGImpactCollisionAlgorithm_gimpact_vs_shape(Native, body0Wrap.Native,
                                                  body1Wrap.Native, shape0.Native, shape1.Native, swapped);
 }
Esempio n. 26
0
        public void Render(CollisionShape shape)
        {
            Mesh mesh;

            if (shapes.TryGetValue(shape, out mesh))
            {
                mesh.DrawSubset(0);
                return;
            }

            if (planeShapes.TryGetValue(shape, out mesh))
            {
                RenderStaticPlaneShape(mesh);
                return;
            }

            // Create the graphics mesh or go to child shapes.
            switch (shape.ShapeType)
            {
                case BroadphaseNativeType.ConeShape:
                    mesh = CreateConeShape(shape as ConeShape);
                    break;
                case BroadphaseNativeType.Convex2DShape:
                    Render((shape as Convex2DShape).ChildShape);
                    return;
                case BroadphaseNativeType.StaticPlane:
                    mesh = CreateStaticPlaneShape(shape as StaticPlaneShape);
                    RenderStaticPlaneShape(mesh);
                    return;
                default:
                    mesh = CreateShape(shape);
                    break;
            }

            mesh.DrawSubset(0);
        }
Esempio n. 27
0
 public void AddChildShapeRef(ref Matrix localTransform, CollisionShape shape)
 {
     _childList.AddChildShape(ref localTransform, shape);
 }
Esempio n. 28
0
 internal CompoundShapeChild(IntPtr native, CollisionShape childShape)
 {
     Native      = native;
     _childShape = childShape;
 }
Esempio n. 29
0
        void InitInstanceData(CollisionObject colObj, CollisionShape shape, ref Matrix transform)
        {
            if (shape.ShapeType == BroadphaseNativeType.CompoundShape)
            {
                foreach (CompoundShapeChild child in (shape as CompoundShape).ChildList)
                {
                    Matrix childTransform = child.Transform * transform;
                    InitInstanceData(colObj, child.ChildShape, ref childTransform);
                }
            }
            else if (shape.ShapeType == BroadphaseNativeType.SoftBodyShape)
            {
                ShapeData shapeData = InitShapeData(shape);
                UpdateSoftBody(colObj as SoftBody, shapeData);

                shapeData.InstanceDataList.Add(new InstanceData()
                {
                    WorldTransform = transform,
                    Color = softBodyColor
                });
            }
            else
            {
                InitShapeData(shape).InstanceDataList.Add(new InstanceData()
                {
                    WorldTransform = transform,
                    Color = "Ground".Equals(colObj.UserObject) ? groundColor :
                        colObj.ActivationState == ActivationState.ActiveTag ? activeColor : passiveColor
                });
            }
        }
Esempio n. 30
0
 public void AddChildShape(Matrix localTransform, CollisionShape shape)
 {
     _childShapes.Add(shape);
     btGImpactCompoundShape_addChildShape(_native, ref localTransform, shape._native);
 }
Esempio n. 31
0
 public void AddChildShape(CollisionShape shape)
 {
     _childShapes.Add(shape);
     btGImpactCompoundShape_addChildShape2(_native, shape._native);
 }
Esempio n. 32
0
        void Create_RbUpStack(int count)
        {
            const float mass = 10.0f;

            CompoundShape cylinderCompound = new CompoundShape();
            CollisionShape cylinderShape = new CylinderShapeX(4, 1, 1);
            CollisionShape boxShape = new BoxShape(4, 1, 1);
            cylinderCompound.AddChildShape(Matrix.Identity, boxShape);
            Quaternion orn = Quaternion.RotationYawPitchRoll((float)Math.PI / 2.0f, 0.0f, 0.0f);
            Matrix localTransform = Matrix.RotationQuaternion(orn);
            //localTransform *= Matrix.Translation(new Vector3(1,1,1));
            cylinderCompound.AddChildShape(localTransform, cylinderShape);

            CollisionShape[] shape = new CollisionShape[]{cylinderCompound,
                new BoxShape(new Vector3(1,1,1)),
                new SphereShape(1.5f)};

            for (int i = 0; i < count; ++i)
                LocalCreateRigidBody(mass, Matrix.Translation(0, 2 + 6 * i, 0), shape[i % shape.Length]);
        }
Esempio n. 33
0
 void InitRigidBodyInstance(CollisionObject colObj, CollisionShape shape, ref Matrix transform)
 {
     if (shape.ShapeType == BroadphaseNativeType.CompoundShape)
     {
         foreach (var child in (shape as CompoundShape).ChildList)
         {
             Matrix childTransform = child.Transform * transform;
             InitRigidBodyInstance(colObj, child.ChildShape, ref childTransform);
         }
     }
     else
     {
         var shapeData = InitShapeData(shape);
         shapeData.Instances.Add(new InstanceData()
         {
             WorldTransform = transform,
             Color = "Ground".Equals(colObj.UserObject) ? groundColor :
                 colObj.ActivationState == ActivationState.ActiveTag ? activeColor : passiveColor
         });
     }
 }
Esempio n. 34
0
        ShapeData InitShapeData(CollisionShape shape)
        {
            ShapeData shapeData;

            if (shapes.TryGetValue(shape, out shapeData) == false)
            {
                switch (shape.ShapeType)
                {
                    case BroadphaseNativeType.SoftBodyShape:
                        shapeData = new ShapeData();
                        break;
                    case BroadphaseNativeType.BoxShape:
                        shapeData = CreateBoxShape(shape as BoxShape);
                        break;
                    case BroadphaseNativeType.CapsuleShape:
                        shapeData = CreateCapsule(shape as CapsuleShape);
                        break;
                    case BroadphaseNativeType.CylinderShape:
                        shapeData = CreateCylinderShape(shape as CylinderShape);
                        break;
                    case BroadphaseNativeType.ConvexHullShape:
                        shapeData = CreateConvexHullShape(shape as ConvexHullShape);
                        break;
                    case BroadphaseNativeType.GImpactShape:
                        shapeData = CreateTriangleMeshShape((shape as GImpactMeshShape).MeshInterface);
                        break;
                    case BroadphaseNativeType.SphereShape:
                        shapeData = CreateSphereShape(shape as SphereShape);
                        break;
                    case BroadphaseNativeType.TriangleMeshShape:
                        shapeData = CreateTriangleMeshShape((shape as TriangleMeshShape).MeshInterface);
                        break;
                    default:
                        throw new NotImplementedException();
                }

                // Create an initial instance data buffer for a single instance
                instanceDataDesc.SizeInBytes = InstanceData.SizeInBytes;
                shapeData.InstanceDataBuffer = new Buffer(device, instanceDataDesc);
                shapeData.BufferBindings[1] = new VertexBufferBinding(shapeData.InstanceDataBuffer, instanceDataDesc.SizeInBytes, 0);

                shapes.Add(shape, shapeData);
            }

            return shapeData;
        }
Esempio n. 35
0
        ShapeData CreateShape(CollisionShape shape)
        {
            ShapeData shapeData = new ShapeData();
            uint[] indices;
            Vector3[] vertices = CreateShape(shape, out indices);
            shapeData.VertexCount = vertices.Length / 2;
            shapeData.SetVertexBuffer(device, vertices);

            if (indices != null)
            {
                shapeData.IndexCount = indices.Length;
                ushort[] indices_s = CompactIndexBuffer(indices);
                if (indices_s != null)
                {
                    shapeData.SetIndexBuffer(device, indices_s);
                }
                else
                {
                    shapeData.SetIndexBuffer(device, indices);
                }
            }

            return shapeData;
        }
Esempio n. 36
0
 public BulletSharpObject(Game game, string modelAssetName, float mass, BulletSharp.MotionState motionState, BulletSharp.CollisionShape collisionShape, Vector3 localInertia)
     : base(game, modelAssetName)
 {
     SetUpBulletPhysicsBody(mass, motionState, collisionShape, localInertia);
 }
Esempio n. 37
0
        ShapeData InitShapeData(CollisionShape shape)
        {
            ShapeData shapeData;

            if (shapes.TryGetValue(shape, out shapeData) == false)
            {
                switch (shape.ShapeType)
                {
                    case BroadphaseNativeType.SoftBodyShape:
                        shapeData = CreateSoftBody();
                        break;
                    case BroadphaseNativeType.Convex2DShape:
                        return InitShapeData((shape as Convex2DShape).ChildShape);
                    default:
                        shapeData = CreateShape(shape);
                        break;
                }

                // Create an initial instance data buffer for a single instance
                instanceDataDesc.SizeInBytes = InstanceData.SizeInBytes;
                shapeData.InstanceDataBuffer = new Buffer(device, instanceDataDesc);
                shapeData.BufferBindings[1] = new VertexBufferBinding(shapeData.InstanceDataBuffer, instanceDataDesc.SizeInBytes, 0);

                shapes.Add(shape, shapeData);
            }

            return shapeData;
        }
 internal CompoundShapeChild(IntPtr native, CollisionShape childShape)
 {
     Initialize(native);
     _childShape = childShape;
 }