void TranslateToObjectSpace(PhysicsObject po) { // handy function to move into object space given a PhysicsObject gl.Translatef(po.Location.X, po.Location.Y, po.Location.Z); }
void ProcessShip(float elapsedTime, float sceneDepth) { myShip.Velocity = GetShipVelocityVector(); // this makes the ship rotate slowly if the user turns it. // the ship can do a max turn of 360 degrees in 1 second, so enforce that float angle = ZRotationFromVector(myShip.Velocity); float maxRotateAllowed = 360f * elapsedTime; float delta = angle - myShipRotation; if (Math.Abs(delta) > maxRotateAllowed) { if ((delta > 0 && delta < 180) || delta < -180) myShipRotation += maxRotateAllowed; else myShipRotation -= maxRotateAllowed; } else { myShipRotation = angle; } if (myShipRotation < 0) myShipRotation += 360; else if (myShipRotation > 360) myShipRotation -= 360; // move the ship myShip = ApplyPhysics(myShip, Vector4f.Zero, elapsedTime, true); gl.PushMatrix(); myShip.Location.Z = sceneDepth; TranslateToObjectSpace(myShip); if (!float.IsNaN(myShipRotation)) gl.Rotatef(myShipRotation, 0, 0, 1); // draw the ship myShipTexture.DrawCenteredSprite(); gl.PopMatrix(); }
// create a bullet flying at the given angle // the starting point is the ship PhysicsObject CreateBullet(float angle) { PhysicsObject bullet = new PhysicsObject(); bullet.Location = myShip.Location; bullet.Velocity.X = (float)Math.Cos(angle / 180 * Math.PI); bullet.Velocity.Y = (float)Math.Sin(angle / 180 * Math.PI); bullet.Velocity = bullet.Velocity.Normalize().Scale(500); return bullet; }
bool IsInBounds(PhysicsObject po) { // return whether the object is in screen bounds or not return po.Location.X < myWidth / 2 && po.Location.X > -myWidth / 2 && po.Location.Y < myHeight / 2 && po.Location.Y > -myHeight / 2; }
PhysicsObject ApplyPhysics(PhysicsObject po, Vector4f acceleration, float elapsedTime, bool rollOver) { // update the position and velocity of the object po.Location += po.Velocity.Scale(elapsedTime); po.Velocity += acceleration.Scale(elapsedTime); if (rollOver) { // roll it over the edge of the screen if necessary Rollover(ref po.Location.X, myWidth); Rollover(ref po.Location.Y, myHeight); } return po; }