private static void handleInput(GameTime gameTime, GraphicsDevice device) { if (ks.IsKeyDown(Keys.E) && lastks.IsKeyUp(Keys.E)) { inEditorMode = !inEditorMode; if (inEditorMode) { camPos = Vector2.Zero; resetCar(); } } if (!inEditorMode) { if (inGame) { if (ks.IsKeyDown(Keys.Space) && lastks.IsKeyUp(Keys.Space)) { isPaused = !isPaused; } #region debug if (ks.IsKeyDown(Keys.T) && lastks.IsKeyUp(Keys.T)) { Debug.print("Attempting to attach objects"); for (int i = 0; i < objects.Length; i++) { if (objects[i].active) { Vector2 m = ConvertUnits.ToSimUnits(new Vector2(ms.X, ms.Y)); if (!objects[i].attached) { Debug.AttachObject(i); Debug.print("Attached object " + i); } } } } #endregion if (ks.IsKeyDown(Keys.Y) && lastks.IsKeyUp(Keys.Y)) { Debug.print("De attaching all objects"); for (int i = 0; i < Debug.attachedObjects.Length; i++) { Debug.DeattachObject(i); Debug.print("Deattached object " + i); } } if (!isPaused) { if (ms.LeftButton == ButtonState.Pressed && lastms.LeftButton == ButtonState.Released) { PhysicsObject obj = PhysicsObject.createRectangle(1, 1); obj.dposition = new Vector2(ms.X, ms.Y) + camPos; obj.texture = TextureCreator.TextureFromShape(obj.body.FixtureList[0].Shape, 6, Color.Orange, 1.5f, device); obj.matType = 6; } if (ms.RightButton == ButtonState.Pressed) { } if (ks.IsKeyDown(Keys.R)) { _car.body[0].body.AngularVelocity = 1f; } if (ks.IsKeyDown(Keys.A)) { if (_car.lJoints[1].MotorSpeed > -_car.maxSpeed) { _car.lJoints[1].MotorSpeed -= (float)gameTime.ElapsedGameTime.TotalSeconds; } if (_car.lJoints[0].MotorSpeed > -_car.maxSpeed) { _car.lJoints[0].MotorSpeed -= (float)gameTime.ElapsedGameTime.TotalSeconds; } } else if (ks.IsKeyDown(Keys.D)) { if (_car.lJoints[1].MotorSpeed < _car.maxSpeed) { _car.lJoints[1].MotorSpeed += (float)gameTime.ElapsedGameTime.TotalSeconds; } if (_car.lJoints[0].MotorSpeed < _car.maxSpeed) { _car.lJoints[0].MotorSpeed += (float)gameTime.ElapsedGameTime.TotalSeconds; } } else { _car.lJoints[1].MotorSpeed = MathHelper.Lerp(_car.lJoints[1].MotorSpeed, 0.0f, 0.01f); _car.lJoints[0].MotorSpeed = MathHelper.Lerp(_car.lJoints[1].MotorSpeed, 0.0f, 0.01f); } if (ks.IsKeyDown(Keys.X)) { _car.lJoints[1].MotorSpeed = 0.0f; _car.lJoints[0].MotorSpeed = 0.0f; } int inc = 2; if (ks.IsKeyDown(Keys.LeftShift)) { inc = 5; } if (ks.IsKeyDown(Keys.Right)) { camPos.X += inc; } else if (ks.IsKeyDown(Keys.Left)) { camPos.X -= inc; } if (ks.IsKeyDown(Keys.Up)) { camPos.Y -= inc; } else if (ks.IsKeyDown(Keys.Down)) { camPos.Y += inc; } PhysicsObject.UpdateAll(); } } } else // EDITOR MODE // { int inc = 5; if (ks.IsKeyDown(Keys.LeftShift)) { inc = 10; } if (ks.IsKeyDown(Keys.D)) { camPos.X += inc; } else if (ks.IsKeyDown(Keys.A)) { camPos.X -= inc; } if (ks.IsKeyDown(Keys.W)) { camPos.Y -= inc; } else if (ks.IsKeyDown(Keys.S)) { camPos.Y += inc; } if (ms.LeftButton == ButtonState.Pressed && lastms.LeftButton == ButtonState.Released) { editVerts.Add(ConvertUnits.ToSimUnits(new Vector2(ms.X, ms.Y) + camPos)); editGround.body = new Body(physicsWorld); if (editVerts.Count > 1) { for (int i = 0; i < editVerts.Count; i++) { try { FixtureFactory.AttachEdge(editVerts[i], editVerts[i + 1], editGround.body); } catch { } } } } if (ms.RightButton == ButtonState.Pressed && lastms.RightButton == ButtonState.Released) { if (editVerts.Count > 0) { editVerts.Remove(editVerts[editVerts.Count - 1]); } editGround.body = new Body(physicsWorld); if (editVerts.Count > 1) { for (int i = 0; i < editVerts.Count; i++) { try { FixtureFactory.AttachEdge(editVerts[i], editVerts[i + 1], editGround.body); } catch { } } } } } }
public static Vehicle createJeep(Vector2 pos, float damping = 0.5f, float freq = 2.5f, float torque = 1.0f, float friction = 2f, float bodyMassMultiplier = 1f, float wheelMassMultiplier = 1f) { pos = ConvertUnits.ToDisplayUnits(pos); PhysicsObject _car; PhysicsObject _fwheel; PhysicsObject _rwheel; LineJoint _fjoint; LineJoint _rjoint; // car body _car = PhysicsObject.createFromTexture(Main.sprTextures[2], Main.sprTextures[2].Width); _car.dposition = pos; _car.texture = Main.sprTextures[2]; _car.body.Mass *= bodyMassMultiplier; // wheels _fwheel = PhysicsObject.createCircle(ConvertUnits.ToSimUnits(65)); _fwheel.dposition = pos + new Vector2(459, 209); _fwheel.texture = Main.sprTextures[3]; _fwheel.origin = TextureCreator.CalculateOrigin(_fwheel.body); _fwheel.body.Friction = friction; _fwheel.body.Mass *= wheelMassMultiplier; _rwheel = PhysicsObject.createCircle(ConvertUnits.ToSimUnits(65)); _rwheel.dposition = pos + new Vector2(127, 209); _rwheel.texture = Main.sprTextures[3]; _fwheel.origin = TextureCreator.CalculateOrigin(_fwheel.body); _rwheel.body.Friction = friction; _rwheel.body.Mass *= wheelMassMultiplier; // wheel joints Vector2 axis = new Vector2(0, 1f); _fjoint = new LineJoint(_car.body, _fwheel.body, _fwheel.position, axis); _fjoint.MotorEnabled = true; _fjoint.MotorSpeed = 0.0f; _fjoint.MaxMotorTorque = torque; _fjoint.Frequency = freq; _fjoint.DampingRatio = damping; Main.physicsWorld.AddJoint(_fjoint); _rjoint = new LineJoint(_car.body, _rwheel.body, _rwheel.position, axis); _rjoint.MotorEnabled = true; _rjoint.MotorSpeed = 0.0f; _rjoint.MaxMotorTorque = torque; _rjoint.Frequency = freq; _rjoint.DampingRatio = damping; Main.physicsWorld.AddJoint(_rjoint); Vehicle car = new Vehicle(); car.body.Add(_car); car.wheels.Add(_fwheel); car.wheels.Add(_rwheel); car.lJoints.Add(_fjoint); car.lJoints.Add(_rjoint); car.maxSpeed = 10.0f; return(car); }