public void defaultKeyboardHandler() { GamePadButtons b = GamePad.GetState(PlayerIndex.One).Buttons; // gamepad controller support (may get rid of) KeyboardState kb = Keyboard.GetState(); // if either escape is pressed, or, on a gamepad, back is pressed if (escDown(kb) || gamepadBackPressed(b)) { game.Exit(); } if (sprite != null) { // here, you can change how fast the sprite moves int xVelocity = 5; int yVelocity = 10; int xDiff = 0; int yDiff = 0; if (leftDown(kb)) { xDiff -= xVelocity; } if (rightDown(kb)) { xDiff += xVelocity; } if (upDown(kb)) { yDiff -= yVelocity; } if (downDown(kb)) { yDiff += yVelocity; } if (spaceDown(kb)) { yDiff += 30; } //Velocity jumpVelocity = Velocity.FromDirection(90, yDiff); sprite.velocity = Velocity.FromCoordinates(xDiff, yDiff); //+ jumpVelocity; } OKBS = kb; }
public static Velocity collisionWithSlip(BasicSprite s1, BasicSprite s2) { if (willCollide(s1, s2)) { s1.onCollide(s2); s2.onCollide(s1); if (s1.collidable && s2.collidable) { float vf1, vf2; Velocity v1 = Velocity.FromCoordinates(s1.velocity.getDirection().X, 0.0f); Velocity v2 = Velocity.FromCoordinates(s2.velocity.getDirection().X, 0.0f); Rectangle collision = Rectangle.Intersect(new Rectangle(s1.getUpdatePositionFromVelocity(v1).ToPoint(), s1.spriteSize), new Rectangle(s2.getUpdatePositionFromVelocity(v2).ToPoint(), s2.spriteSize)); if (collision != Rectangle.Empty) { vf1 = (int)v1.getDirection().X + (Math.Sign(v1.getDirection().X) * -collision.Width); } else { vf1 = v1.getDirection().X; } v1 = Velocity.FromCoordinates(0.0f, s1.velocity.getDirection().Y); v2 = Velocity.FromCoordinates(0.0f, s2.velocity.getDirection().Y); collision = Rectangle.Intersect(new Rectangle(s1.getUpdatePositionFromVelocity(v1).ToPoint(), s1.spriteSize), new Rectangle(s2.getUpdatePositionFromVelocity(v2).ToPoint(), s2.spriteSize)); if (collision != Rectangle.Empty) { vf2 = (int)v1.getDirection().Y + (Math.Sign(v1.getDirection().Y) * -collision.Height); } else { vf2 = v1.getDirection().Y; } return(Velocity.FromCoordinates(vf1, vf2)); } else { return(s1.velocity); } } else { return(s1.velocity); } }
public static Velocity operator *(Velocity v1, Velocity v2) { return(Velocity.FromCoordinates(v1.direction.X * v2.direction.X, v1.direction.Y * v2.direction.Y)); }