public PointOnPoint(RigidBody rigidBody, Vector3 position) { Vector3 positionInLocal = position + rigidBody.Position; grabber = new MotorizedGrabSpring(); grabber.Setup(rigidBody.entity, new XVector3(positionInLocal.X, positionInLocal.Y, positionInLocal.Z)); grabber.LocalOffset = new XVector3(position.X, position.Y, position.Z); }
/// <summary> /// Updates Main Gameplay Loop code here, this is affected by whether or not the scene is paused. /// </summary> /// <param name="gameTime"></param> /// <param name="otherScreenHasFocus"></param> /// <param name="coveredByOtherScreen"></param> public override void UpdateScene(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) { character.Update((float)gameTime.ElapsedGameTime.TotalSeconds, vxEngine.InputManager.PreviousKeyboardState, vxEngine.InputManager.KeyboardState, vxEngine.InputManager.PreviousGamePadState, vxEngine.InputManager.GamePadState); if (this.IsActive) { vxEngine.InputManager.ShowCursor = false; } else { vxEngine.InputManager.ShowCursor = true; } //Update grabber if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Pressed && !grabber.IsGrabbing) { //Find the earliest ray hit RayCastResult raycastResult; if (BEPUPhyicsSpace.RayCast(new Ray(Camera.Position, Camera.WorldMatrix.Forward), 500, rayCastFilter, out raycastResult)) { var entityCollision = raycastResult.HitObject as EntityCollidable; //If there's a valid ray hit, then grab the connected object! if (entityCollision != null && entityCollision.Entity.IsDynamic) { Console.WriteLine("GRABBING ITEM: {0}", entityCollision.Entity.GetType().ToString()); grabber.Setup(entityCollision.Entity, raycastResult.HitData.Location); //grabberGraphic.IsDrawing = true; grabDistance = raycastResult.HitData.T; } } } if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Pressed && grabber.IsUpdating) { if (grabDistance < 4) { grabDistance = 3; grabber.GoalPosition = Camera.Position + Camera.WorldMatrix.Forward * grabDistance; } } else if (vxEngine.InputManager.MouseState.RightButton == ButtonState.Released && grabber.IsUpdating) { grabber.Release(); } base.UpdateScene(gameTime, otherScreenHasFocus, coveredByOtherScreen); }
public Player() : base(null, null, 100) { character = new CharacterControllerInput(GameManager.Space, RenderingDevice.Camera as CharacterCamera, new Vector3(-1, -16, 5)); character.CharacterController.Body.Tag = this; character.CharacterController.HorizontalMotionConstraint.Speed = 10; character.CharacterController.HorizontalMotionConstraint.CrouchingSpeed = 5; PhysicsObject = character.CharacterController.Body; // for posterity PhysicsObject.CollisionInformation.CollisionRules.Group = dynamicObjects; // also for posterity rayCastFilter = RayCastFilter; sword = new BaseModel(delegate { return(Program.Game.Loader.SwordModel); }, false, true, new Vector3(0.5f, -15, 4.2f)); CollisionRules.AddRule(sword.Ent, PhysicsObject, CollisionRule.NoBroadPhase); sword.Ent.CollisionInformation.LocalPosition = new Vector3(0, -1, 0); sword.Ent.Orientation = Quaternion.CreateFromAxisAngle(Vector3.UnitX, -MathHelper.PiOver2); swordgrabber = new MotorizedGrabSpring(); swordgrabber.Setup(sword.Ent, sword.ModelPosition - Vector3.UnitX * 0.5f); sword.Ent.CollisionInformation.Events.PairTouching += onCollision; }
public override void Update(Fix64 dt) { #region Kapow-Shooter Input //Update kapow-shooter if (!vehicle.IsActive) #if !WINDOWS { if (Game.GamePadInput.IsButtonDown(Buttons.RightTrigger)) #else { if (Game.MouseInput.LeftButton == ButtonState.Pressed) #endif { if (character.IsActive) //Keep the ball out of the character's body if its being used. { kapow.Position = Game.Camera.Position + Game.Camera.WorldMatrix.Forward * 3; } else { kapow.Position = Game.Camera.Position + Game.Camera.WorldMatrix.Forward; } kapow.AngularVelocity = Vector3.Zero; kapow.LinearVelocity = Game.Camera.WorldMatrix.Forward * 30; } } #endregion #region Grabber Input //Update grabber #if !WINDOWS if (Game.GamePadInput.IsButtonDown(Buttons.RightShoulder) && !grabber.IsUpdating) #else if (Game.MouseInput.RightButton == ButtonState.Pressed && !grabber.IsGrabbing) #endif { //Find the earliest ray hit RayCastResult raycastResult; if (Space.RayCast(new Ray(Game.Camera.Position, Game.Camera.WorldMatrix.Forward), 1000, rayCastFilter, out raycastResult)) { var entityCollision = raycastResult.HitObject as EntityCollidable; //If there's a valid ray hit, then grab the connected object! if (entityCollision != null && entityCollision.Entity.IsDynamic) { grabber.Setup(entityCollision.Entity, raycastResult.HitData.Location); grabberGraphic.IsDrawing = true; grabDistance = raycastResult.HitData.T; } } } #if !WINDOWS if (Game.GamePadInput.IsButtonDown(Buttons.RightShoulder) && grabber.IsUpdating) #else else if (Game.MouseInput.RightButton == ButtonState.Pressed && grabber.IsUpdating) #endif { grabber.GoalPosition = Game.Camera.Position + Game.Camera.WorldMatrix.Forward * grabDistance; } #if !WINDOWS if (!Game.GamePadInput.IsButtonDown(Buttons.RightShoulder) && grabber.IsUpdating) #else else if (Game.MouseInput.RightButton == ButtonState.Released && grabber.IsUpdating) #endif { grabber.Release(); grabberGraphic.IsDrawing = false; } #endregion #region Control State Input #if !WINDOWS if (!vehicle.IsActive && Game.WasButtonPressed(Buttons.LeftTrigger)) { kapowMaker.Position = kapow.Position; kapowMaker.Explode(); } if (Game.WasButtonPressed(Buttons.A)) {//Toggle character perspective. if (!character.IsActive) { vehicle.Deactivate(); character.Activate(); } else { character.Deactivate(); } } if (Game.WasButtonPressed(Buttons.B)) {//Toggle vehicle perspective. if (!vehicle.IsActive) { character.Deactivate(); vehicle.Activate(); } else { vehicle.Deactivate(); } } #else if (!vehicle.IsActive && Game.WasKeyPressed(Keys.Space)) { //Detonate the bomb kapowMaker.Position = kapow.Position; kapowMaker.Explode(); } if (Game.WasKeyPressed(Keys.C)) { //Toggle character perspective. if (!character.IsActive) { vehicle.Deactivate(); character.Activate(); } else { character.Deactivate(); } } if (Game.WasKeyPressed(Keys.V)) { //Toggle vehicle perspective. if (!vehicle.IsActive) { character.Deactivate(); vehicle.Activate(Game.Camera.Position); } else { vehicle.Deactivate(); } } #endif #endregion base.Update(dt); //Base.update updates the space, which needs to be done before the camera is updated. character.Update(dt, Game.PreviousKeyboardInput, Game.KeyboardInput, Game.PreviousGamePadInput, Game.GamePadInput); vehicle.Update(dt, Game.KeyboardInput, Game.GamePadInput); //If neither are active, just use the default camera movement style. if (!character.IsActive && !vehicle.IsActive) { freeCameraControlScheme.Update(dt); } }
/// <summary> /// Handles the input and movement of the character. /// </summary> /// <param name="dt">Time since last frame in simulation seconds.</param> /// <param name="previousKeyboardInput">The last frame's keyboard state.</param> /// <param name="keyboardInput">The current frame's keyboard state.</param> /// <param name="previousGamePadInput">The last frame's gamepad state.</param> /// <param name="gamePadInput">The current frame's keyboard state.</param> public void Update(float dt) { if (IsActive) { //Note that the character controller's update method is not called here; this is because it is handled within its owning space. //This method's job is simply to tell the character to move around. CameraControlScheme.Update(dt); Vector2 totalMovement = Vector2.Zero; if (Input.ControlScheme == ControlScheme.XboxController) { totalMovement += new Vector2(Input.CurrentPad.ThumbSticks.Left.X, Input.CurrentPad.ThumbSticks.Left.Y); CharacterController.HorizontalMotionConstraint.SpeedScale = Math.Min(totalMovement.Length(), 1); //Don't trust the game pad to output perfectly normalized values. CharacterController.HorizontalMotionConstraint.MovementDirection = totalMovement; CharacterController.StanceManager.DesiredStance = Input.CurrentPad.IsButtonDown(Buttons.B) ? Stance.Crouching : Stance.Standing; //Jumping if (Input.CurrentPadLastFrame.IsButtonUp(Buttons.A) && Input.CurrentPad.IsButtonDown(Buttons.A)) { CharacterController.Jump(); } } else if (Input.ControlScheme == ControlScheme.Keyboard) { //Collect the movement impulses. if (Input.KeyboardState.IsKeyDown(Keys.W)) { totalMovement += new Vector2(0, 1); } if (Input.KeyboardState.IsKeyDown(Keys.S)) { totalMovement += new Vector2(0, -1); } if (Input.KeyboardState.IsKeyDown(Keys.A)) { totalMovement += new Vector2(-1, 0); } if (Input.KeyboardState.IsKeyDown(Keys.D)) { totalMovement += new Vector2(1, 0); } if (totalMovement == Vector2.Zero) { CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Zero; } else { CharacterController.HorizontalMotionConstraint.MovementDirection = Vector2.Normalize(totalMovement); } CharacterController.StanceManager.DesiredStance = Input.KeyboardState.IsKeyDown(Keys.LeftShift) ? Stance.Crouching : Stance.Standing; //Jumping if (Input.KeyboardLastFrame.IsKeyUp(Keys.Space) && Input.KeyboardState.IsKeyDown(Keys.Space)) { CharacterController.Jump(); } } CharacterController.ViewDirection = Camera.WorldMatrix.Forward; #region Grabber Input //Update grabber if (((Input.ControlScheme == ControlScheme.XboxController && Input.CheckXboxJustPressed(Buttons.X)) || (Input.ControlScheme == ControlScheme.Keyboard && Input.CheckKeyboardPress(Keys.E))) && !grabber.IsUpdating) { //Find the earliest ray hit RayCastResult raycastResult; if (Space.RayCast(new Ray(Renderer.Camera.Position, Renderer.Camera.WorldMatrix.Forward), 6, RayCastFilter, out raycastResult)) { var entityCollision = raycastResult.HitObject as EntityCollidable; //If there's a valid ray hit, then grab the connected object! if (entityCollision != null) { var tag = entityCollision.Entity.Tag as GameModel; if (tag != null) { tag.Texture.GameProperties.RayCastHit(tag.Texture.GameProperties); if (!tag.Texture.Wireframe && tag.Texture.GameProperties.Grabbable) { grabber.Setup(entityCollision.Entity, raycastResult.HitData.Location); try { CollisionRules.AddRule(entityCollision.Entity, CharacterController.Body, CollisionRule.NoBroadPhase); } catch { } grabDistance = raycastResult.HitData.T; } else { Program.Game.Loader.InvalidApplicationRemovalGrab.Play(); } } else { Program.Game.Loader.InvalidApplicationRemovalGrab.Play(); } } } else { Program.Game.Loader.InvalidApplicationRemovalGrab.Play(); } } else if (grabber.IsUpdating) { if ((Input.ControlScheme == ControlScheme.XboxController && Input.CheckXboxJustPressed(Buttons.X)) || (Input.ControlScheme == ControlScheme.Keyboard && Input.CheckKeyboardPress(Keys.E))) { try { CollisionRules.RemoveRule(grabber.Entity, CharacterController.Body); } catch { } grabber.Release(); } grabber.GoalPosition = Renderer.Camera.Position + Renderer.Camera.WorldMatrix.Forward * grabDistance; } #endregion } }