Esempio n. 1
0
        /// <summary>
        /// Applies the impulse.
        /// </summary>
        /// <param name="impulse">The impulse.</param>
        /// <param name="relPos">The relative position.</param>
        public void ApplyImpulse(float3 impulse, float3 relPos)
        {
            var o = (RigidBodyImp)_rbi.UserObject;

            // impulse *= 10;
            o._rbi.ApplyImpulse(Translator.Float3ToBtVector3(impulse) * 10, Translator.Float3ToBtVector3(relPos));
        }
Esempio n. 2
0
        /// <summary>
        /// Applies the torque impulse.
        /// </summary>
        /// <param name="torqueImpulse">The torque impulse.</param>
        public void ApplyTorqueImpulse(float3 torqueImpulse)
        {
            var o = (RigidBodyImp)_rbi.UserObject;

            o._rbi.ApplyTorqueImpulse(Translator.Float3ToBtVector3(torqueImpulse));
            // _torqueImpulse = value*10;
        }
Esempio n. 3
0
        /// <summary>
        /// Calculates the principal axis transform.
        /// </summary>
        /// <param name="masses">The masses.</param>
        /// <param name="principal">The principal.</param>
        /// <param name="inertia">The inertia.</param>
        public void CalculatePrincipalAxisTransform(float[] masses, float4x4 principal, float3 inertia)
        {
            var btPrincipal = Translator.Float4X4ToBtMatrix(principal);
            var btInertia   = Translator.Float3ToBtVector3(inertia);

            BtCompoundShape.CalculatePrincipalAxisTransform(masses, ref btPrincipal, out btInertia);
        }
Esempio n. 4
0
        /// <summary>
        /// Adds the child shape.
        /// </summary>
        /// <param name="localTransform">The local transform.</param>
        /// <param name="shape">The shape.</param>
        public void AddChildShape(float4x4 localTransform, ICylinderShapeImp shape)
        {
            var btHalfExtents    = Translator.Float3ToBtVector3(shape.HalfExtents);
            var btChildShape     = new CylinderShape(btHalfExtents);
            var btLocalTransform = Translator.Float4X4ToBtMatrix(localTransform);

            BtCompoundShape.AddChildShape(btLocalTransform, btChildShape);
        }
Esempio n. 5
0
        /// <summary>
        /// Adds a child shape.
        /// </summary>
        /// <param name="localTransform">The local transform.</param>
        /// <param name="shape">The shape.</param>
        public void AddChildShape(float4x4 localTransform, IBoxShapeImp shape)
        {
            Debug.WriteLine("AddBox");
            var btHalfExtents    = Translator.Float3ToBtVector3(shape.HalfExtents);
            var btChildShape     = new BoxShape(btHalfExtents);
            var btLocalTransform = Translator.Float4X4ToBtMatrix(localTransform);

            BtCompoundShape.AddChildShape(btLocalTransform, btChildShape);
        }
Esempio n. 6
0
        /// <summary>
        /// Adds the cylinder shape.
        /// </summary>
        /// <param name="halfExtents">The half extents.</param>
        /// <returns></returns>
        public ICylinderShapeImp AddCylinderShape(float3 halfExtents)
        {
            var btCylinderShape = new CylinderShape(Translator.Float3ToBtVector3(halfExtents));

            BtCollisionShapes.Add(btCylinderShape);

            var retval = new CylinderShapeImp();

            retval.BtCylinderShape     = btCylinderShape;
            btCylinderShape.UserObject = retval;
            return(retval);
        }
Esempio n. 7
0
        /// <summary>
        /// Adds the box shape.
        /// </summary>
        /// <param name="boxHalfExtents">The box half extents.</param>
        /// <returns></returns>
        public IBoxShapeImp AddBoxShape(float3 boxHalfExtents)
        {
            var btBoxShape = new BoxShape(Translator.Float3ToBtVector3(boxHalfExtents));

            BtCollisionShapes.Add(btBoxShape);

            var retval = new BoxShapeImp();

            retval.BtBoxShape     = btBoxShape;
            btBoxShape.UserObject = retval;
            return(retval);
        }
Esempio n. 8
0
        /// <summary>
        /// Adds the static plane shape.
        /// </summary>
        /// <param name="planeNormal">The plane normal.</param>
        /// <param name="planeConstant">The plane constant.</param>
        /// <returns></returns>
        public IStaticPlaneShapeImp AddStaticPlaneShape(float3 planeNormal, float planeConstant)
        {
            var btPlaneNormal      = Translator.Float3ToBtVector3(planeNormal);
            var btStaticPlaneShape = new StaticPlaneShape(btPlaneNormal, planeConstant);

            btStaticPlaneShape.Margin = 0.04f;
            BtCollisionShapes.Add(btStaticPlaneShape);
            Debug.WriteLine("btStaticPlaneShape.Margin" + btStaticPlaneShape.Margin);
            var retval = new StaticPlaneShapeImp();

            retval.BtStaticPlaneShape     = btStaticPlaneShape;
            btStaticPlaneShape.UserObject = retval;
            return(retval);
        }
Esempio n. 9
0
        /// <summary>
        /// Adds the child shape.
        /// </summary>
        /// <param name="localTransform">The local transform.</param>
        /// <param name="shape">The shape.</param>
        public void AddChildShape(float4x4 localTransform, IMultiSphereShapeImp shape)
        {
            var btPositions = new Vector3[shape.SphereCount];
            var btRadi      = new float[shape.SphereCount];

            for (int i = 0; i < shape.SphereCount; i++)
            {
                var pos = Translator.Float3ToBtVector3(shape.GetSpherePosition(i));
                btPositions[i] = pos;
                btRadi[i]      = shape.GetSphereRadius(i);
            }
            var btChildShape     = new MultiSphereShape(btPositions, btRadi);
            var btLocalTransform = Translator.Float4X4ToBtMatrix(localTransform);

            BtCompoundShape.AddChildShape(btLocalTransform, btChildShape);
        }
Esempio n. 10
0
        //GearConstraint
        /// <summary>
        /// Adds the gear constraint.
        /// </summary>
        /// <param name="rigidBodyA">The rigid body a.</param>
        /// <param name="rigidBodyB">The rigid body b.</param>
        /// <param name="axisInA">The axis in a.</param>
        /// <param name="axisInB">The axis in b.</param>
        /// <param name="ratio">The ratio.</param>
        /// <returns></returns>
        public IGearConstraintImp AddGearConstraint(IRigidBodyImp rigidBodyA, IRigidBodyImp rigidBodyB, float3 axisInA, float3 axisInB, float ratio)
        {
            var rigidBodyAImp = (RigidBodyImp)rigidBodyA;
            var btRigidBodyA  = rigidBodyAImp._rbi;

            var rigidBodyBImp = (RigidBodyImp)rigidBodyB;
            var btRigidBodyB  = rigidBodyBImp._rbi;

            var btAxisInA = Translator.Float3ToBtVector3(axisInA);
            var btAxisInB = Translator.Float3ToBtVector3(axisInB);

            var btGearConstraint = new GearConstraint(btRigidBodyA, btRigidBodyB, btAxisInA, btAxisInB, ratio);

            BtWorld.AddConstraint(btGearConstraint);

            var retval = new GearConstraintImp();

            retval._gci = btGearConstraint;
            btGearConstraint.UserObject = retval;
            return(retval);
        }
Esempio n. 11
0
        /// <summary>
        /// Adds the convex hull shape.
        /// </summary>
        /// <param name="points">The points.</param>
        /// <param name="optimized">if set to <c>true</c> [optimized].</param>
        /// <returns></returns>
        public IConvexHullShapeImp AddConvexHullShape(float3[] points, bool optimized)
        {
            var btPoints = new Vector3[points.Count()];

            for (int i = 0; i < btPoints.Count(); i++)
            {
                var point = Translator.Float3ToBtVector3(points[i]);
                btPoints[i] = point;
            }


            var btConvexHullShape = new ConvexHullShape(btPoints);

            //btConvexHullShape.LocalScaling = new Vector3(3, 3, 3);
            if (optimized == true)
            {
                var btShapeHull = new ShapeHull(btConvexHullShape);
                var margin      = btConvexHullShape.Margin;
                btShapeHull.BuildHull(margin);
                ConvexHullShape simplifiedConvexShape = new ConvexHullShape(btShapeHull.Vertices);

                BtCollisionShapes.Add(simplifiedConvexShape);

                var retval = new ConvexHullShapeImp();
                retval.BtConvexHullShape         = simplifiedConvexShape;
                simplifiedConvexShape.UserObject = retval;
                return(retval);
            }
            else
            {
                BtCollisionShapes.Add(btConvexHullShape);

                var retval = new ConvexHullShapeImp();
                retval.BtConvexHullShape     = btConvexHullShape;
                btConvexHullShape.UserObject = retval;
                return(retval);
            }
        }
Esempio n. 12
0
        /// <summary>
        /// Applies the torque.
        /// </summary>
        /// <param name="torque">The torque.</param>
        public void ApplyTorque(float3 torque)
        {
            var o = (RigidBodyImp)_rbi.UserObject;

            o._rbi.ApplyTorque(Translator.Float3ToBtVector3(torque));
        }
Esempio n. 13
0
        /// <summary>
        /// Applies the force.
        /// </summary>
        /// <param name="force">The force.</param>
        /// <param name="relPos">The relative position.</param>
        public void ApplyForce(float3 force, float3 relPos)
        {
            var o = (RigidBodyImp)_rbi.UserObject;

            o._rbi.ApplyForce(Translator.Float3ToBtVector3(force), Translator.Float3ToBtVector3(relPos));
        }
 /// <summary>
 /// Sets the axis.
 /// </summary>
 /// <param name="axis1">The axis1.</param>
 /// <param name="axis2">The axis2.</param>
 public void SetAxis(float3 axis1, float3 axis2)
 {
     _g6dofci.SetAxis(Translator.Float3ToBtVector3(axis1), Translator.Float3ToBtVector3(axis2));
 }
Esempio n. 15
0
        /// <summary>
        /// Adds the point.
        /// </summary>
        /// <param name="point">The point.</param>
        public void AddPoint(float3 point)
        {
            var btPoint = Translator.Float3ToBtVector3(point);

            BtConvexHullShape.AddPoint(btPoint, true);
        }
Esempio n. 16
0
        /*private void MyTickCallBack(ManifoldPoint cp, CollisionObjectWrapper colobj0wrap, int partid0, int index0, CollisionObjectWrapper colobj1wrap, int partid1, int index1)
         * {
         *  Debug.WriteLine("MyTickCallBack");
         *  int numManifolds = BtWorld.Dispatcher.NumManifolds;
         *  RigidBodyImp myRb;
         *  //Debug.WriteLine("numManifolds: " + numManifolds);
         *  for (int i = 0; i < numManifolds; i++)
         *  {
         *      PersistentManifold contactManifold = BtWorld.Dispatcher.GetManifoldByIndexInternal(i);
         *      int numContacts = contactManifold.NumContacts;
         *      if (numContacts > 0)
         *      {
         *          CollisionObject obA = (CollisionObject) contactManifold.Body0;
         *          CollisionObject obB = (CollisionObject) contactManifold.Body1;
         *
         *         // Debug.WriteLine(numContacts);
         *          var pnA = obA.UserObject;
         *
         *          for (int j = 0; j < numContacts; j++)
         *          {
         *              ManifoldPoint pt = contactManifold.GetContactPoint(j);
         *
         *          }
         *      }
         *  }
         * }*/


        /// <summary>
        /// Adds the rigid body.
        /// </summary>
        /// <param name="mass">The mass.</param>
        /// <param name="worldTransform">The world transform.</param>
        /// <param name="orientation">The orientation.</param>
        /// <param name="colShape">The col shape.</param>
        /// <returns></returns>
        public IRigidBodyImp AddRigidBody(float mass, float3 worldTransform, float3 orientation, ICollisionShapeImp colShape /*, float3 intertia*/)
        {
            // Use bullet to do what needs to be done:


            var btMatrix = Matrix.RotationX(orientation.x)
                           * Matrix.RotationY(orientation.y)
                           * Matrix.RotationZ(orientation.z)
                           * Matrix.Translation(worldTransform.x, worldTransform.y, worldTransform.z);

            var btMotionState = new DefaultMotionState(btMatrix);


            var shapeType = colShape.GetType().ToString();

            CollisionShape btColShape;

            // var isStatic = false;
            switch (shapeType)
            {
            //Primitives
            case "Fusee.Engine.BoxShapeImp":
                var box = (BoxShapeImp)colShape;
                var btBoxHalfExtents = Translator.Float3ToBtVector3(box.HalfExtents);
                btColShape = new BoxShape(btBoxHalfExtents);
                break;

            case "Fusee.Engine.CapsuleShapeImp":
                var capsule = (CapsuleShapeImp)colShape;
                btColShape = new CapsuleShape(capsule.Radius, capsule.HalfHeight);
                break;

            case "Fusee.Engine.ConeShapeImp":
                var cone = (ConeShapeImp)colShape;
                btColShape = new ConeShape(cone.Radius, cone.Height);
                break;

            case "Fusee.Engine.CylinderShapeImp":
                var cylinider             = (CylinderShapeImp)colShape;
                var btCylinderHalfExtents = Translator.Float3ToBtVector3(cylinider.HalfExtents);
                btColShape = new CylinderShape(btCylinderHalfExtents);
                break;

            case "Fusee.Engine.MultiSphereShapeImp":
                var multiSphere = (MultiSphereShapeImp)colShape;
                var btPositions = new Vector3[multiSphere.SphereCount];
                var btRadi      = new float[multiSphere.SphereCount];
                for (int i = 0; i < multiSphere.SphereCount; i++)
                {
                    var pos = Translator.Float3ToBtVector3(multiSphere.GetSpherePosition(i));
                    btPositions[i] = pos;
                    btRadi[i]      = multiSphere.GetSphereRadius(i);
                }
                btColShape = new MultiSphereShape(btPositions, btRadi);
                break;

            case "Fusee.Engine.SphereShapeImp":
                var sphere   = (SphereShapeImp)colShape;
                var btRadius = sphere.Radius;
                btColShape = new SphereShape(btRadius);
                break;

            //Misc
            case "Fusee.Engine.CompoundShapeImp":
                var compShape = (CompoundShapeImp)colShape;
                btColShape = new CompoundShape(true);
                btColShape = compShape.BtCompoundShape;
                break;

            case "Fusee.Engine.EmptyShapeImp":
                btColShape = new EmptyShape();
                break;

            //Meshes
            case "Fusee.Engine.ConvexHullShapeImp":
                var convHull = (ConvexHullShapeImp)colShape;
                var btPoints = new Vector3[convHull.GetNumPoints()];
                for (int i = 0; i < convHull.GetNumPoints(); i++)
                {
                    var point = convHull.GetScaledPoint(i);
                    btPoints[i] = Translator.Float3ToBtVector3(point);
                }
                btColShape = new ConvexHullShape(btPoints);
                //btColShape.LocalScaling = new Vector3(3,3,3);
                break;

            case "Fusee.Engine.StaticPlaneShapeImp":
                var staticPlane = (StaticPlaneShapeImp)colShape;
                Debug.WriteLine("staticplane: " + staticPlane.Margin);
                var btNormal = Translator.Float3ToBtVector3(staticPlane.PlaneNormal);
                btColShape = new StaticPlaneShape(btNormal, staticPlane.PlaneConstant);
                // isStatic = true;
                //btColShape.Margin = 0.04f;
                //Debug.WriteLine("btColshape" + btColShape.Margin);
                break;

            case "Fusee.Engine.GImpactMeshShapeImp":
                var gImpMesh = (GImpactMeshShapeImp)colShape;
                gImpMesh.BtGImpactMeshShape.UpdateBound();
                var btGimp = new GImpactMeshShape(gImpMesh.BtGImpactMeshShape.MeshInterface);

                btGimp.UpdateBound();
                btColShape = btGimp;

                break;

            //Default
            default:
                Debug.WriteLine("defaultImp");
                btColShape = new EmptyShape();
                break;
            }

            var btLocalInertia = btColShape.CalculateLocalInertia(mass);
            // btLocalInertia *= (10.0f*10);
            RigidBodyConstructionInfo btRbcInfo = new RigidBodyConstructionInfo(mass, btMotionState, btColShape,
                                                                                btLocalInertia);

            var btRigidBody = new RigidBody(btRbcInfo);

            btRigidBody.Restitution    = 0.2f;
            btRigidBody.Friction       = 0.2f;
            btRigidBody.CollisionFlags = CollisionFlags.CustomMaterialCallback;

            BtWorld.AddRigidBody(btRigidBody);
            btRbcInfo.Dispose();
            var retval = new RigidBodyImp();

            retval._rbi            = btRigidBody;
            btRigidBody.UserObject = retval;
            return(retval);
        }
Esempio n. 17
0
        /// <summary>
        /// Applies the central impulse.
        /// </summary>
        /// <param name="centralImpulse">The central impulse.</param>
        public void ApplyCentralImpulse(float3 centralImpulse)
        {
            var o = (RigidBodyImp)_rbi.UserObject;

            o._rbi.ApplyCentralImpulse(Translator.Float3ToBtVector3(centralImpulse));
        }
Esempio n. 18
0
        /// <summary>
        /// Sets the axis.
        /// </summary>
        /// <param name="axisInA">The axis in a.</param>
        public void SetAxis(float3 axisInA)
        {
            var o = (HingeConstraintImp)_hci.UserObject;

            _hci.SetAxis(Translator.Float3ToBtVector3(axisInA));
        }