protected override void OnUpdate(float timeStep) { Input input = Input; if (vehicle != null) { // Get movement controls and assign them to the vehicle component. If UI has a focused element, clear controls if (UI.FocusElement == null) { vehicle.Controls.Set(Vehicle.CtrlForward, input.GetKeyDown(Key.W)); vehicle.Controls.Set(Vehicle.CtrlBack, input.GetKeyDown(Key.S)); vehicle.Controls.Set(Vehicle.CtrlLeft, input.GetKeyDown(Key.A)); vehicle.Controls.Set(Vehicle.CtrlRight, input.GetKeyDown(Key.D)); // Add yaw & pitch from the mouse motion or touch input. Used only for the camera, does not affect motion if (TouchEnabled) { for (uint i = 0; i < input.NumTouches; ++i) { TouchState state = input.GetTouch(i); Camera camera = CameraNode.GetComponent<Camera>(); if (camera == null) return; var graphics = Graphics; vehicle.Controls.Yaw += TouchSensitivity*camera.Fov/graphics.Height*state.Delta.X; vehicle.Controls.Pitch += TouchSensitivity*camera.Fov/graphics.Height*state.Delta.Y; } } else { vehicle.Controls.Yaw += (float)input.MouseMoveX * Vehicle.YawSensitivity; vehicle.Controls.Pitch += (float)input.MouseMoveY * Vehicle.YawSensitivity; } // Limit pitch vehicle.Controls.Pitch = MathHelper.Clamp(vehicle.Controls.Pitch, 0.0f, 80.0f); // Check for loading / saving the scene if (input.GetKeyPress(Key.F5)) { scene.SaveXml(FileSystem.ProgramDir + "Data/Scenes/VehicleDemo.xml"); } if (input.GetKeyPress(Key.F7)) { scene.LoadXml(FileSystem.ProgramDir + "Data/Scenes/VehicleDemo.xml"); // After loading we have to reacquire the weak pointer to the Vehicle component, as it has been recreated // Simply find the vehicle's scene node by name as there's only one of them Node vehicleNode = scene.GetChild("Vehicle", true); if (vehicleNode != null) vehicle = vehicleNode.GetComponent<Vehicle>(); } } else vehicle.Controls.Set(Vehicle.CtrlForward | Vehicle.CtrlBack | Vehicle.CtrlLeft | Vehicle.CtrlRight, false); } }
void CreateVehicle() { Node vehicleNode = scene.CreateChild("Vehicle"); vehicleNode.Position = (new Vector3(0.0f, 5.0f, 0.0f)); // Create the vehicle logic component vehicle = new Vehicle(); vehicleNode.AddComponent(vehicle); // Create the rendering and physics components vehicle.Init(); }