public override void Draw(RenderContext renderContext, GameTime gameTime) { if (renderContext.layerPass != RenderContext.LayerPass.MAIN_PASS) { return; } if (Game1.RECORDING) { MoveShip(renderContext.graphicsDevice); } SphereVector unitPosition = new SphereVector(position.Normalized()); SphereVector up = unitPosition.WalkNorth(Math.PI / 2); SphereVector right = new SphereVector(up.Cross(unitPosition).Normalized()); double rotation = Math.Atan2(forward.Dot(-right), forward.Dot(up)); // we want up to be 0 and a positive rotation to be cw Matrixd shipWVP = CreateShipWVP(renderContext, rotation); foreach (ModelMesh mesh in GlobalContent.StartingShuttle.Meshes) { foreach (BasicEffect eff in mesh.Effects) { eff.EnableDefaultLighting(); eff.World = shipWVP.toMatrix(); eff.VertexColorEnabled = false; eff.Alpha = 1; } mesh.Draw(); } }
private void MoveShip1(GraphicsDevice graphicsDevice) { double rotationAccel = 0; double accel = AccelWithKeys(); Keyboard.GetState().AffectNumber(ref rotationAccel, Keys.Left, Keys.Right, Keys.A, Keys.D, 0.01); rotationSpeed = Math.Max(Math.Min(rotationSpeed + rotationAccel, 0.1), -0.1); rotationSpeed *= 0.9; // apply rotation SphereVector right = new SphereVector(forward.Cross(position).Normalized()); forward = forward.WalkTowards(right, rotationSpeed); // move forwards velocity += forward * accel * Math.Pow(0.5, zoom) * 20; position = new SphereVector((position + velocity).Normalized()); // flatten our forward/velocity against current position forward = new SphereVector(forward.Cross(position).Cross(-position).Normalized()); if (velocity.Length() > 0.0000000001) { double currentSpeed = velocity.Length(); //if (rotationSpeed != 0) currentSpeed *= velocity.Normalized().Dot(forward); double max = Math.Pow(0.5, zoom) * 0.2; double min = Math.Pow(0.5, zoom) * 0; currentSpeed = Math.Max(Math.Min(currentSpeed, max), min); velocity = (velocity.Cross(position).Cross(-position).Normalized() * 0.8 + forward * 0.2).Normalized() * currentSpeed; } else { velocity = velocity.Cross(position).Cross(-position); } // now update altitude ControlZoomWithKeys(); SphereVector unitPosition2 = new SphereVector(position.Normalized()); camera.cameraRotX = unitPosition2.ToLongLat().X; camera.cameraRotY = unitPosition2.ToLongLat().Y; camera.UpdateCamera(graphicsDevice); }
public double RotateWithMouse(GraphicsDevice graphicsDevice) { SphereVector unitPosition = new SphereVector(position.Normalized()); SphereVector up2 = unitPosition.WalkNorth(Math.PI / 2); SphereVector right2 = new SphereVector(up2.Cross(unitPosition).Normalized()); double screenSpaceRotation = Math.Atan2(-forward.Dot(up2), forward.Dot(right2)); // we want up to be 0 and a positive rotation to be cw Point mousePos = Mouse.GetState().Position; Vector2d mouseV = new Vector2d((mousePos.X / (double)graphicsDevice.Viewport.Width - 0.5) * graphicsDevice.Viewport.AspectRatio, mousePos.Y / (double)graphicsDevice.Viewport.Height - 0.5); double mouseRotation = Math.Atan2(mouseV.Y, mouseV.X); double diff1 = (mouseRotation + 4 * Math.PI - screenSpaceRotation) % (2 * Math.PI); double diff2 = (screenSpaceRotation + 4 * Math.PI - mouseRotation) % (2 * Math.PI); double rotationSpeed; if (diff1 < diff2) { return(diff1 / 10); } else { return(-diff2 / 10); } }
// have the mouse control everything private void MoveShip2(GraphicsDevice graphicsDevice) { double accel = AccelerateWithMouse(graphicsDevice); //double accel = AccelWithKeys(); rotationSpeed = RotateWithMouse(graphicsDevice); SphereVector right = new SphereVector(forward.Cross(position).Normalized()); forward = forward.WalkTowards(right, rotationSpeed); // move forwards velocity += forward * accel * Math.Pow(0.5, zoom) * 20; position = new SphereVector((position + velocity).Normalized()); // flatten our forward/velocity against current position forward = new SphereVector(forward.Cross(position).Cross(-position).Normalized()); if (velocity.Length() > 0.0000000001) { double currentSpeed = velocity.Length(); //if (rotationSpeed != 0) currentSpeed *= velocity.Normalized().Dot(forward); double max = Math.Pow(0.5, zoom) * 0.2; double min = Math.Pow(0.5, zoom) * 0; currentSpeed = Math.Max(Math.Min(currentSpeed, max), min); velocity = (velocity.Cross(position).Cross(-position).Normalized() * 0.8 + forward * 0.2).Normalized() * currentSpeed; } else { velocity = velocity.Cross(position).Cross(-position); } // now update altitude SphereVector unitPosition2 = new SphereVector(position.Normalized()); camera.cameraRotX = unitPosition2.ToLongLat().X; camera.cameraRotY = unitPosition2.ToLongLat().Y; BaseZoomOnSpeed(); //ControlZoomWithKeys(); camera.UpdateCamera(graphicsDevice); }