コード例 #1
0
ファイル: CharacterComponent.cs プロジェクト: rohitshe/Code
        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);
        }
コード例 #2
0
        /// <summary>
        /// Counts how many objects are overlapping with a given ColliderShape. If trackObjects are true, you can
        /// use OverlappedWith to check if another PhysicsComponent was part of the overlapping objects
        /// </summary>
        /// <param name="shape">ColliderShape to check for overlaps with</param>
        /// <param name="position">Position to move the ColliderShape, in addition to its LocalOffset</param>
        /// <param name="myGroup">What collision group is the ColliderShape in?</param>
        /// <param name="overlapsWith">What collision groups does the ColliderShape overlap with?</param>
        /// <param name="contactTest">If true, contact test overlapping objects. See ContactResults for output. Defaults to false</param>
        /// <param name="stopAfterFirstContact">If contact testing, should we stop contact testing after our first contact was found?</param>
        /// <returns>Number of overlapping objects</returns>
        public static int PerformOverlapTest(ColliderShape shape, Xenko.Core.Mathematics.Vector3?position = null,
                                             CollisionFilterGroups myGroup          = CollisionFilterGroups.DefaultFilter,
                                             CollisionFilterGroupFlags overlapsWith = CollisionFilterGroupFlags.AllFilter,
                                             bool contactTest = false, bool stopAfterFirstContact = false)
        {
            // doesn't support multithreading
            if (mySimulation.simulationLocker != null)
            {
                throw new InvalidOperationException("Overlap testing not supported with multithreaded physics");
            }

            if (ghostObject == null)
            {
                ghostObject = new BulletSharp.PairCachingGhostObject
                {
                    ContactProcessingThreshold = 1e18f,
                    UserObject = shape
                };
                ghostObject.CollisionFlags |= BulletSharp.CollisionFlags.NoContactResponse;

                internalResults = new OverlapCallback();

                NativeOverlappingObjects = new HashSet <object>();
            }

            ghostObject.CollisionShape = shape.InternalShape;
            ghostObject.WorldTransform = Matrix.Transformation(shape.Scaling, shape.LocalRotation, position.HasValue ? position.Value + shape.LocalOffset : shape.LocalOffset);

            mySimulation.collisionWorld.AddCollisionObject(ghostObject, (BulletSharp.CollisionFilterGroups)myGroup, (BulletSharp.CollisionFilterGroups)overlapsWith);

            int overlapCount = ghostObject.NumOverlappingObjects;

            NativeOverlappingObjects.Clear();
            for (int i = 0; i < overlapCount; i++)
            {
                NativeOverlappingObjects.Add(ghostObject.OverlappingPairs[i]);
            }

            if (contactTest)
            {
                internalResults.Clear();
                internalResults.CollisionFilterGroup = (int)myGroup;
                internalResults.CollisionFilterMask  = (int)overlapsWith;

                foreach (object nativeobj in NativeOverlappingObjects)
                {
                    mySimulation.collisionWorld.ContactPairTest(ghostObject, (BulletSharp.CollisionObject)nativeobj, internalResults);
                    if (stopAfterFirstContact && internalResults.Contacts.Count > 0)
                    {
                        break;
                    }
                }
            }

            mySimulation.collisionWorld.RemoveCollisionObject(ghostObject);

            return(overlapCount);
        }
コード例 #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);
        }
コード例 #4
0
ファイル: CharacterComponent.cs プロジェクト: cg123/xenko
        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);
        }
コード例 #5
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
        }