Ejemplo n.º 1
0
        public override void Start()
        {
            playerController = Entity.Get<PhysicsComponent>().Elements[0].Character;

            //Please remember that in the GameStudio element the parameter Step Height is extremely important, it not set properly it will cause the entity to snap fast to the ground
            playerController.JumpSpeed = 5.0f;
            playerController.Gravity = -10.0f;
            playerController.FallSpeed = 10.0f;

            playerController.FirstContactStart += playerController_OnFirstContactBegin;

            idleGroup = Asset.Load<SpriteSheet>("player_idle");
            runGroup = Asset.Load<SpriteSheet>("player_run");
            playerSprite = Entity.Get<SpriteComponent>();
            PlayIdle();
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Removes the character from the engine processing pipeline.
        /// </summary>
        /// <param name="character">The character.</param>
        /// <exception cref="System.Exception">Cannot perform this action when the physics engine is set to CollisionsOnly</exception>
        public void RemoveCharacter(Character character)
        {
            if (discreteDynamicsWorld == null) throw new Exception("Cannot perform this action when the physics engine is set to CollisionsOnly");

            var collider = character.InternalCollider;
            var action = character.KinematicCharacter;
            discreteDynamicsWorld.RemoveCollisionObject(collider);
            discreteDynamicsWorld.RemoveCharacter(action);

            character.Simulation = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Creates the character.
        /// </summary>
        /// <param name="collider">The collider.</param>
        /// <param name="stepHeight">Height of the step.</param>
        /// <returns></returns>
        public Character CreateCharacter(ColliderShape collider, float stepHeight)
        {
            var ch = new Character(collider)
            {
                InternalCollider = new BulletSharp.PairCachingGhostObject
                {
                    CollisionShape = collider.InternalShape
                }
            };

            ch.InternalCollider.UserObject = ch;

            ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CharacterObject;

            if (collider.NeedsCustomCollisionCallback)
            {
                ch.InternalCollider.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
            }

            ch.InternalCollider.ContactProcessingThreshold = !canCcd ? 1e18f : 1e30f;

            ch.KinematicCharacter = new BulletSharp.KinematicCharacterController((BulletSharp.PairCachingGhostObject)ch.InternalCollider, (BulletSharp.ConvexShape)collider.InternalShape, stepHeight);

            return ch;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds the character to the engine processing pipeline.
        /// </summary>
        /// <param name="character">The character.</param>
        /// <param name="group">The group.</param>
        /// <param name="mask">The mask.</param>
        /// <exception cref="System.Exception">Cannot perform this action when the physics engine is set to CollisionsOnly</exception>
        public void AddCharacter(Character character, CollisionFilterGroupFlags group, CollisionFilterGroupFlags mask)
        {
            if (discreteDynamicsWorld == null) throw new Exception("Cannot perform this action when the physics engine is set to CollisionsOnly");

            var collider = character.InternalCollider;
            var action = character.KinematicCharacter;
            discreteDynamicsWorld.AddCollisionObject(collider, (BulletSharp.CollisionFilterGroups)group, (BulletSharp.CollisionFilterGroups)mask);
            discreteDynamicsWorld.AddCharacter(action);

            character.Simulation = this;
        }
Ejemplo n.º 5
0
        public override void Start()
        {
            desiredYaw =
                yaw =
                    (float)
                        Math.Asin(2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.Y +
                                  2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.W);

            desiredPitch =
                pitch =
                    (float)
                        Math.Atan2(
                            2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.W -
                            2 * Entity.Transform.Rotation.Y * Entity.Transform.Rotation.Z,
                            1 - 2 * Entity.Transform.Rotation.X * Entity.Transform.Rotation.X -
                            2 * Entity.Transform.Rotation.Z * Entity.Transform.Rotation.Z);

            Input.LockMousePosition(true);
            Game.IsMouseVisible = false;

            var physComponent = Entity.Get<PhysicsComponent>();
            if (physComponent == null)
            {
                physComponent = new PhysicsComponent();
                Entity.Add(physComponent);
            }

            var element = Entity.Get<PhysicsComponent>().Elements.FirstOrDefault(x => x is CharacterElement);
            if (element != null)
            {
                characterElement = (CharacterElement)element;
            }
            else
            {
                Log.Error("Could not find a physics character element. (" + Entity.Name + ")");

                characterElement = new CharacterElement();
                characterElement.ColliderShapes.Add(new ColliderShapeAssetDesc { Shape = PhysicsColliderShape.New(new CapsuleColliderShapeDesc()) });
                physComponent.Elements.Add(characterElement);

                //this is currently the only way to make sure the physics element is actually created.
                SceneSystem.SceneInstance.Scene.Entities.Remove(Entity);
                SceneSystem.SceneInstance.Scene.Entities.Add(Entity);
            }

            character = characterElement.Character;

            if (CameraEntity == null)
            {
                for (int index = 0; index < Entity.Transform.Children.Count; index++)
                {
                    var child = Entity.Transform.Children[index];
                    var camera = child.Entity.Get<CameraComponent>();
                    if (camera != null)
                    {
                        CameraEntity = child.Entity;
                        break;
                    }
                }

                if (CameraEntity == null)
                {
                    Log.Error("Could not find a camera component. (" + Entity.Name + ")");
                }
            }

            if (CameraEntity != null)
            {
                baseCameraRotation = CameraEntity.Transform.Rotation;
            }
        }