Example #1
0
        /// <summary>
        /// Create the physical entity.
        /// </summary>
        /// <param name="shape">Collision shape that define this body.</param>
        /// <param name="mass">Body mass (in kg), or 0 for static.</param>
        /// <param name="inertia">Body inertia, or 0 for static.</param>
        /// <param name="transformations">Starting transformations.</param>
        public RigidBody(CollisionShapes.ICollisionShape shape, float mass = 10f, float inertia = 1f, Matrix?transformations = null)
        {
            // store collision shape
            _shape = shape;

            // set default transformations
            transformations = transformations ?? Matrix.Identity;

            // create starting state
            _state = new DefaultMotionState(ToBullet.Matrix((Matrix)(transformations)));

            // create the rigid body construction info
            RigidBodyConstructionInfo info = new RigidBodyConstructionInfo(
                mass,
                _state,
                shape.BulletCollisionShape,
                shape.BulletCollisionShape.CalculateLocalInertia(mass) * inertia);

            // create the rigid body itself and attach self to UserObject
            BulletRigidBody            = new BulletSharp.RigidBody(info);
            BulletRigidBody.UserObject = this;

            // set default group and mask
            CollisionGroup = CollisionGroups.DynamicObjects;
            CollisionMask  = CollisionMasks.Targets;

            // set some defaults
            InvokeCollisionEvents = true;
            IsEthereal            = false;
        }
Example #2
0
        /// <summary>
        /// Perform a raycast test and return colliding results.
        /// </summary>
        /// <param name="start">Start ray vector.</param>
        /// <param name="end">End ray vector.</param>
        /// <param name="resultsCallback">BulletSharp results callback.</param>
        internal RaycastResults Raycast(Vector3 start, Vector3 end, BulletSharp.RayResultCallback resultsCallback)
        {
            // convert start and end vectors to bullet vectors
            BulletSharp.Math.Vector3 bStart = ToBullet.Vector(start);
            BulletSharp.Math.Vector3 bEnd   = ToBullet.Vector(end);

            // perform the ray test
            return(Raycast(bStart, bEnd, resultsCallback));
        }
Example #3
0
        /// <summary>
        /// Convert an array of vectors from MonoGame to Bullet3d.
        /// </summary>
        /// <param name="vecs">Vectors array to convert.</param>
        /// <returns>Array of Bullet vectors.</returns>
        public static BulletSharp.Math.Vector3[] Vectors(Vector3[] vecs)
        {
            BulletSharp.Math.Vector3[] bvectors = new BulletSharp.Math.Vector3[vecs.Length];
            int i = 0;

            foreach (var vector in vecs)
            {
                bvectors[i++] = ToBullet.Vector(vector);
            }
            return(bvectors);
        }
Example #4
0
 /// <summary>
 /// Apply torque force on the body.
 /// </summary>
 /// <param name="torque">Torque force to apply.</param>
 /// <param name="asImpulse">If true, will apply torque as an impulse.</param>
 public void ApplyTorque(Vector3 torque, bool asImpulse = false)
 {
     if (asImpulse)
     {
         BulletRigidBody.ApplyTorqueImpulse(ToBullet.Vector(torque));
     }
     else
     {
         BulletRigidBody.ApplyTorque(ToBullet.Vector(torque));
     }
     BulletRigidBody.Activate();
 }
Example #5
0
        /// <summary>
        /// Perform a raycast test and return colliding results, while ignoring 'self' object.
        /// </summary>
        /// <param name="start">Start ray vector.</param>
        /// <param name="end">End ray vector.</param>
        /// <param name="self">Physical body to ignore.</param>
        public RaycastResults Raycast(Vector3 start, Vector3 end, ECS.Components.Physics.BasePhysicsComponent self)
        {
            // convert start and end vectors to bullet vectors
            BulletSharp.Math.Vector3 bStart = ToBullet.Vector(start);
            BulletSharp.Math.Vector3 bEnd   = ToBullet.Vector(end);

            // create class to hold results
            BulletSharp.RayResultCallback resultsCallback =
                new BulletSharp.KinematicClosestNotMeRayResultCallback(self._PhysicalBody._BulletEntity);

            // perform ray cast
            return(Raycast(bStart, bEnd, resultsCallback));
        }
Example #6
0
        /// <summary>
        /// Perform a raycast test and return colliding results.
        /// </summary>
        /// <param name="start">Start ray vector.</param>
        /// <param name="end">End ray vector.</param>
        /// <param name="returnNearest">If true, will only return the nearest object collided.</param>
        public RaycastResults Raycast(Vector3 start, Vector3 end, bool returnNearest = true)
        {
            // convert start and end vectors to bullet vectors
            BulletSharp.Math.Vector3 bStart = ToBullet.Vector(start);
            BulletSharp.Math.Vector3 bEnd   = ToBullet.Vector(end);

            // create class to hold results
            BulletSharp.RayResultCallback resultsCallback = returnNearest ?
                                                            new BulletSharp.ClosestRayResultCallback(ref bStart, ref bEnd) as BulletSharp.RayResultCallback :
                                                            new BulletSharp.AllHitsRayResultCallback(bStart, bEnd);

            // perform ray cast
            return(Raycast(bStart, bEnd, resultsCallback));
        }
Example #7
0
        /// <summary>
        /// Create the static collision object from shape.
        /// </summary>
        /// <param name="shape">Collision shape that define this body.</param>
        /// <param name="transformations">Starting transformations.</param>
        public KinematicBody(CollisionShapes.ICollisionShape shape, Matrix?transformations = null)
        {
            // create the collision object
            _shape = shape;
            BulletCollisionObject = new CollisionObject();
            BulletCollisionObject.CollisionShape = shape.BulletCollisionShape;

            // turn off simulation
            base.EnableSimulation = false;

            // set default group and mask
            CollisionGroup = CollisionGroups.DynamicObjects;
            CollisionMask  = CollisionMasks.Targets;

            // if provided, set transformations
            if (transformations != null)
            {
                BulletCollisionObject.WorldTransform = ToBullet.Matrix(transformations.Value);
            }
        }
Example #8
0
 /// <summary>
 /// Set gravity vector.
 /// </summary>
 /// <param name="gravity"></param>
 public void SetGravity(Vector3 gravity)
 {
     _gravity = ToBullet.Vector(gravity);
     _world.SetGravity(ref _gravity);
 }
Example #9
0
 /// <summary>
 /// Apply impulse on the body, from its center.
 /// </summary>
 /// <param name="impulse">Impulse to apply.</param>
 public void ApplyImpulse(Vector3 impulse)
 {
     BulletRigidBody.ApplyCentralImpulse(ToBullet.Vector(impulse));
     BulletRigidBody.Activate();
 }
Example #10
0
 /// <summary>
 /// Apply force on the body, from a given position.
 /// </summary>
 /// <param name="force">Force to apply.</param>
 /// <param name="from">Force source position.</param>
 public void ApplyForce(Vector3 force, Vector3 from)
 {
     BulletRigidBody.ApplyForce(ToBullet.Vector(force), ToBullet.Vector(from));
     BulletRigidBody.Activate();
 }
Example #11
0
 /// <summary>
 /// Apply force on the body, from its center.
 /// </summary>
 /// <param name="force">Force to apply.</param>
 public void ApplyForce(Vector3 force)
 {
     BulletRigidBody.ApplyCentralForce(ToBullet.Vector(force));
     BulletRigidBody.Activate();
 }
Example #12
0
 /// <summary>
 /// Apply impulse on the body, from a given position.
 /// </summary>
 /// <param name="impulse">Impulse to apply.</param>
 /// <param name="from">Impulse source position.</param>
 public void ApplyImpulse(Vector3 impulse, Vector3 from)
 {
     BulletRigidBody.ApplyImpulse(ToBullet.Vector(impulse), ToBullet.Vector(from));
     BulletRigidBody.Activate();
 }