Example #1
0
        protected override void OnAttach()
        {
            NativeCollisionObject = new BulletSharp.PairCachingGhostObject
            {
                CollisionShape = ColliderShape.InternalShape,
                UserObject     = this
            };

            NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject;

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;

            KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)NativeCollisionObject, (BulletSharp.ConvexShape)ColliderShape.InternalShape, StepHeight);

            base.OnAttach();

            FallSpeed = fallSpeed;
            MaxSlope  = maxSlope;
            JumpSpeed = jumpSpeed;
            Gravity   = gravity;

            UpdatePhysicsTransformation(); //this will set position and rotation of the collider

            Simulation.AddCharacter(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
        }
Example #2
0
        protected override void OnDetach()
        {
            Simulation.RemoveCharacter(this);

            if (KinematicCharacter == null)
            {
                return;
            }
            KinematicCharacter.Dispose();
            KinematicCharacter = null;

            base.OnDetach();
        }
Example #3
0
        protected override void OnAttach()
        {
            BulletSharp.ConvexShape cshape = ColliderShape.InternalShape as BulletSharp.ConvexShape;

            if (cshape == null)
            {
                throw new ArgumentException(Entity.Name + " needs a convex shape for its CharacterComponent!");
            }

            NativeCollisionObject = new BulletSharp.PairCachingGhostObject
            {
                CollisionShape = ColliderShape.InternalShape,
                UserObject     = this,
            };

            NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject;

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;

            BulletSharp.Math.Vector3 unitY = new BulletSharp.Math.Vector3(0f, 1f, 0f);
            KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)NativeCollisionObject, cshape, StepHeight, ref unitY);

            base.OnAttach();

            FallSpeed = fallSpeed;
            MaxSlope  = maxSlope;
            JumpSpeed = jumpSpeed;
            Gravity   = gravity;

            UpdatePhysicsTransformation(); //this will set position and rotation of the collider

            Simulation.AddCharacter(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
        }
Example #4
0
        protected override void OnDetach()
        {
            if (KinematicCharacter == null) return;

            Simulation.RemoveCharacter(this);

            KinematicCharacter.Dispose();
            KinematicCharacter = null;

            base.OnDetach();
        }
Example #5
0
        protected override void OnAttach()
        {
            NativeCollisionObject = new BulletSharp.PairCachingGhostObject
            {
                CollisionShape = ColliderShape.InternalShape,
                UserObject = this
            };

            NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject;

            if (ColliderShape.NeedsCustomCollisionCallback)
            {
                NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;

            KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)NativeCollisionObject, (BulletSharp.ConvexShape)ColliderShape.InternalShape, StepHeight);

            base.OnAttach();

            FallSpeed = fallSpeed;
            MaxSlope = maxSlope;
            JumpSpeed = jumpSpeed;
            Gravity = gravity;

            UpdatePhysicsTransformation(); //this will set position and rotation of the collider

            Simulation.AddCharacter(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
        }
Example #6
0
        public void SetPhysics()
        {
            LogHelper.LogInfo("character physics setting");

#if _USE_BEPU_PHYSICS
            mCharacterController = new BEPUphysics.Character.CharacterController(new BEPUutilities.Vector3(GetLocation().x, GetLocation().y, GetLocation().z), 1.0f, 1.0f * 0.7f, 1.0f * 0.3f, 0.5f, 0.001f, 10f, 0.8f, 1.3f
                                                                                 , 5.0f // standing speed
                                                                                 , 3f, 1.5f, 1000, 0f, 0f, 0f, 0f
                                                                                 );

            World.Instance(WorldId).space.Add(mCharacterController);
#elif _USE_BULLET_SHARP
            /*
             * m_collisionShape = new BulletSharp.CapsuleShape(0.5f, 0.5f);
             *
             * m_collisionObject = new BulletSharp.PairCachingGhostObject();
             * m_collisionObject.CollisionShape = m_collisionShape;
             * m_collisionObject.CollisionFlags = BulletSharp.CollisionFlags.CharacterObject;
             * mCharacterController = new BulletSharp.KinematicCharacterController(m_collisionObject, (BulletSharp.ConvexShape)m_collisionShape, 0.01f);
             *
             *
             * BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix();
             * worldTrans.M41 = 7f;
             * worldTrans.M42 = 10f;
             * worldTrans.M43 = 0;
             * m_collisionObject.WorldTransform = worldTrans;
             * m_collisionObject.UserObject = this;
             *
             * World.Instance(worldId).world.AddCollisionObject(m_collisionObject, BulletSharp.CollisionFilterGroups.DefaultFilter, BulletSharp.CollisionFilterGroups.Everything);
             * World.Instance(worldId).world.AddAction(mCharacterController);
             */


            //m_collisionObject = new BulletSharp.RigidBody(new BulletSharp.RigidBodyConstructionInfo(1f, new BulletSharp.DefaultMotionState(), new BulletSharp.CapsuleShape(0.5f, 0.5f), new BulletSharp.Math.Vector3(0, 1f, 0)));
            //m_collisionObject.CollisionFlags = BulletSharp.CollisionFlags.StaticObject;

            //BulletSharp.Math.Matrix worldTrans = new BulletSharp.Math.Matrix();
            //worldTrans.M41 = 0f;
            //worldTrans.M42 =1f;
            //worldTrans.M43 = 0;
            //m_collisionObject.WorldTransform = worldTrans;


            //World.Instance(worldId).world.AddRigidBody((BulletSharp.RigidBody)m_collisionObject, BulletSharp.CollisionFilterGroups.DefaultFilter, BulletSharp.CollisionFilterGroups.Everything);



            ghostObject = new BulletSharp.PairCachingGhostObject();
            ghostObject.WorldTransform = BulletSharp.Math.Matrix.Translation(7f, 10f, 0f);
            World.Instance(worldId).Broadphase.OverlappingPairCache.SetInternalGhostPairCallback(new BulletSharp.GhostPairCallback());

            const float characterHeight = 0.5f;
            const float characterWidth  = 0.5f;
            var         capsule         = new BulletSharp.CapsuleShape(characterWidth, characterHeight);
            //capsule.Margin = 0f;
            ghostObject.CollisionShape = capsule;
            ghostObject.CollisionFlags = BulletSharp.CollisionFlags.CharacterObject;

            const float stepHeight      = 0.35f;
            BulletSharp.Math.Vector3 up = new BulletSharp.Math.Vector3(Vector3.up.x, Vector3.up.y, Vector3.up.z);
            character = new BulletSharp.KinematicCharacterController(ghostObject, capsule, stepHeight, ref up);



            World.Instance(worldId).world.AddCollisionObject(ghostObject, BulletSharp.CollisionFilterGroups.CharacterFilter, BulletSharp.CollisionFilterGroups.StaticFilter | BulletSharp.CollisionFilterGroups.DefaultFilter);

            World.Instance(worldId).world.AddAction(character);
#elif _USE_BEPU_PHYSICS_V2
            var   initialPosition   = new System.Numerics.Vector3(7f, 10f, 0f);
            float speculativeMargin = 0.1f;
            float mass = 1;
            float maximumHorizontalForce   = 20;
            float maximumVerticalGlueForce = 100;
            float jumpVelocity             = 6;
            //float speed = 4;
            float maximumSlope = (float)Math.PI * 0.4f;

            var shape = new Capsule(0.5f, 1);

            var shapeIndex = World.Instance(worldId).characters.Simulation.Shapes.Add(shape);

            var bodyHandle = World.Instance(worldId).characters.Simulation.Bodies.Add(BodyDescription.CreateDynamic(initialPosition, new BodyInertia {
                InverseMass = 1f / mass
            }, new CollidableDescription(shapeIndex, speculativeMargin), new BodyActivityDescription(shape.Radius * 0.02f)));
            character                                 = World.Instance(worldId).characters.AllocateCharacter(bodyHandle);
            character.LocalUp                         = new System.Numerics.Vector3(0, 1, 0);
            character.CosMaximumSlope                 = (float)Math.Cos(maximumSlope);
            character.JumpVelocity                    = jumpVelocity;
            character.MaximumVerticalForce            = maximumVerticalGlueForce;
            character.MaximumHorizontalForce          = maximumHorizontalForce;
            character.MinimumSupportDepth             = shape.Radius * -0.01f;
            character.MinimumSupportContinuationDepth = -speculativeMargin;

            body = World.Instance(worldId).characters.Simulation.Bodies.GetBodyReference(bodyHandle);
#endif
        }