public override CollisionShape CopyCollisionShape()
        {
            CylinderShape cs = new CylinderShapeZ(halfExtent.ToBullet());

            cs.LocalScaling = m_localScaling.ToBullet();
            return(cs);
        }
 public override CollisionShape GetCollisionShape()
 {
     if (collisionShapePtr == null)
     {
         collisionShapePtr = new CylinderShapeZ(halfExtent.ToBullet());
         ((CylinderShapeZ)collisionShapePtr).LocalScaling = m_localScaling.ToBullet();
     }
     return(collisionShapePtr);
 }
Exemple #3
0
        protected override CollisionShape CreateShape()
        {
            //Cylinder are around Z axis in vvvv
            //If we need Y/X axis, we can rotate, so i use Z
            CollisionShape shape = new CylinderShapeZ(this.hw, this.hh, this.hd);

            //BvhTriangleMeshShape bvh ;//= new BvhTriangleMeshShape(new StridingMeshInterface(),
            //bvh.
            return(shape);
        }
Exemple #4
0
        private void Create2dBodies()
        {
            // Re-using the same collision is better for memory usage and performance
            float u = 0.96f;

            Vector3[] points      = { new Vector3(0, u, 0), new Vector3(-u, -u, 0), new Vector3(u, -u, 0) };
            var       childShape0 = new BoxShape(1, 1, Depth);
            var       colShape    = new Convex2DShape(childShape0);
            var       childShape1 = new ConvexHullShape(points);
            var       colShape2   = new Convex2DShape(childShape1);
            var       childShape2 = new CylinderShapeZ(1, 1, Depth);
            var       colShape3   = new Convex2DShape(childShape2);

            CollisionShapes.Add(colShape);
            CollisionShapes.Add(colShape2);
            CollisionShapes.Add(colShape3);

            CollisionShapes.Add(childShape0);
            CollisionShapes.Add(childShape1);
            CollisionShapes.Add(childShape2);

            colShape.Margin = 0.03f;

            float   mass         = 1.0f;
            Vector3 localInertia = colShape.CalculateLocalInertia(mass);

            var rbInfo = new RigidBodyConstructionInfo(mass, null, colShape, localInertia);

            Vector3 x      = new Vector3(-ArraySizeX, 8, -20);
            Vector3 y      = Vector3.Zero;
            Vector3 deltaX = new Vector3(1, 2, 0);
            Vector3 deltaY = new Vector3(2, 0, 0);

            for (int i = 0; i < ArraySizeY; i++)
            {
                y = x;
                for (int j = 0; j < ArraySizeX; j++)
                {
                    Matrix startTransform = Matrix.Translation(y - new Vector3(-10, 0, 0));

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

                    switch (j % 3)
                    {
                    case 0:
                        rbInfo.CollisionShape = colShape;
                        break;

                    case 1:
                        rbInfo.CollisionShape = colShape3;
                        break;

                    default:
                        rbInfo.CollisionShape = colShape2;
                        break;
                    }
                    var body = new RigidBody(rbInfo)
                    {
                        //ActivationState = ActivationState.IslandSleeping,
                        LinearFactor  = new Vector3(1, 1, 0),
                        AngularFactor = new Vector3(0, 0, 1)
                    };

                    World.AddRigidBody(body);

                    y += deltaY;
                }
                x += deltaX;
            }

            rbInfo.Dispose();
        }
Exemple #5
0
        protected override void OnInitializePhysics()
        {
            // collision configuration contains default setup for memory, collision setup
            CollisionConf = new DefaultCollisionConfiguration();

            // Use the default collision dispatcher. For parallel processing you can use a diffent dispatcher.
            Dispatcher = new CollisionDispatcher(CollisionConf);

            VoronoiSimplexSolver            simplex  = new VoronoiSimplexSolver();
            MinkowskiPenetrationDepthSolver pdSolver = new MinkowskiPenetrationDepthSolver();

            Convex2DConvex2DAlgorithm.CreateFunc convexAlgo2d = new Convex2DConvex2DAlgorithm.CreateFunc(simplex, pdSolver);

            Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Convex2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2d);
            Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Box2DShape, BroadphaseNativeType.Convex2DShape, convexAlgo2d);
            Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Convex2DShape, BroadphaseNativeType.Box2DShape, convexAlgo2d);
            Dispatcher.RegisterCollisionCreateFunc(BroadphaseNativeType.Box2DShape, BroadphaseNativeType.Box2DShape, new Box2DBox2DCollisionAlgorithm.CreateFunc());

            Broadphase = new DbvtBroadphase();

            // the default constraint solver.
            Solver = new SequentialImpulseConstraintSolver();

            World         = new DiscreteDynamicsWorld(Dispatcher, Broadphase, Solver, CollisionConf);
            World.Gravity = new Vector3(0, -10, 0);

            // create a few basic rigid bodies
            CollisionShape groundShape = new BoxShape(150, 7, 150);

            CollisionShapes.Add(groundShape);
            RigidBody ground = LocalCreateRigidBody(0, Matrix.Identity, groundShape);

            ground.UserObject = "Ground";

            // create a few dynamic rigidbodies
            // Re-using the same collision is better for memory usage and performance
            float u = 0.96f;

            Vector3[]   points      = { new Vector3(0, u, 0), new Vector3(-u, -u, 0), new Vector3(u, -u, 0) };
            ConvexShape childShape0 = new BoxShape(1, 1, Depth);
            ConvexShape colShape    = new Convex2DShape(childShape0);
            ConvexShape childShape1 = new ConvexHullShape(points);
            ConvexShape colShape2   = new Convex2DShape(childShape1);
            ConvexShape childShape2 = new CylinderShapeZ(1, 1, Depth);
            ConvexShape colShape3   = new Convex2DShape(childShape2);

            CollisionShapes.Add(colShape);
            CollisionShapes.Add(colShape2);
            CollisionShapes.Add(colShape3);

            CollisionShapes.Add(childShape0);
            CollisionShapes.Add(childShape1);
            CollisionShapes.Add(childShape2);

            colShape.Margin = 0.03f;

            float   mass         = 1.0f;
            Vector3 localInertia = colShape.CalculateLocalInertia(mass);

            Matrix startTransform;

            Vector3 x      = new Vector3(-ArraySizeX, 8, -20);
            Vector3 y      = Vector3.Zero;
            Vector3 deltaX = new Vector3(1, 2, 0);
            Vector3 deltaY = new Vector3(2, 0, 0);

            int i, j;

            for (i = 0; i < ArraySizeY; i++)
            {
                y = x;
                for (j = 0; j < ArraySizeX; j++)
                {
                    startTransform = Matrix.Translation(y - new Vector3(-10, 0, 0));

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

                    RigidBodyConstructionInfo rbInfo;
                    switch (j % 3)
                    {
                    case 0:
                        rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape, localInertia);
                        break;

                    case 1:
                        rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape3, localInertia);
                        break;

                    default:
                        rbInfo = new RigidBodyConstructionInfo(mass, myMotionState, colShape2, localInertia);
                        break;
                    }
                    RigidBody body = new RigidBody(rbInfo);
                    rbInfo.Dispose();
                    //body.ActivationState = ActivationState.IslandSleeping;
                    body.LinearFactor  = new Vector3(1, 1, 0);
                    body.AngularFactor = new Vector3(0, 0, 1);

                    World.AddRigidBody(body);

                    y += deltaY;
                }
                x += deltaX;
            }
        }
Exemple #6
0
        /// <summary>
        /// Creates a Rigid Body from a .bxda file
        /// </summary>
        /// <param name="FilePath"></param>
        public void CreateRigidBody(string FilePath)
        {
            CollisionShape     shape;
            WheelDriverMeta    wheel = null;
            DefaultMotionState motion;
            BXDAMesh           mesh = new BXDAMesh();

            mesh.ReadFromFile(FilePath);
            Vector3    loc;
            Quaternion rot = Quaternion.Identity;

            //Is it a wheel?
            if ((wheel = GetSkeletalJoint()?.cDriver?.GetInfo <WheelDriverMeta>()) != null && true)
            {
                //Align the cylinders
                Vector3 min, max;
                GetShape(mesh).GetAabb(Matrix4.Identity, out min, out max);
                Vector3 extents = max - min;

                //Find the thinnest dimension, that is probably wheat the cylinder should be aligned to
                if (extents.X < extents.Y) //X or Z
                {
                    if (extents.X < extents.Z)
                    {
                        shape = new CylinderShapeX(wheel.width, wheel.radius, wheel.radius); //X
                    }
                    else
                    {
                        shape = new CylinderShapeZ(wheel.radius, wheel.radius, wheel.width); //Z
                    }
                }
                else //Y or Z
                {
                    if (extents.Y < extents.Z)
                    {
                        shape = new CylinderShape(wheel.radius, wheel.width, wheel.radius); //Y
                    }
                    else
                    {
                        shape = new CylinderShapeZ(wheel.radius, wheel.radius, wheel.width); //Z
                    }
                }

                loc = MeshUtilities.MeshCenter(mesh);
            }
            //Rigid Body Construction
            else
            {
                shape = GetShape(mesh);
                loc   = MeshUtilities.MeshCenter(mesh);
            }

            if (debug)
            {
                Console.WriteLine("Rotation is " + rot);
            }

            motion = new DefaultMotionState(Matrix4.CreateTranslation(loc + new Vector3(0, 100, 0)));
            RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(mesh.physics.mass, motion, shape, shape.CalculateLocalInertia(mesh.physics.mass));

            //Temp?
            info.Friction        = 100;
            info.RollingFriction = 100;
            //info.AngularDamping = 0f;
            //info.LinearDamping = 0.5f;

            BulletObject = new RigidBody(info);
        }