void createVehicle(Vector3 position) { float width = 15; float height = 2; float length = 5; float wheelWidth = 1; float wheelRadius = 2; BepuEntity chassis = createBox(position, width, height, length); chassis.LoadContent(); chassis.body.Mass = 100; BepuEntity wheel; RevoluteJoint joint; wheel = createWheel(new Vector3(position.X - (width / 2) + wheelRadius, position.Y, position.Z - (length / 2) - wheelWidth), wheelWidth, wheelRadius); joint = new RevoluteJoint(chassis.body, wheel.body, wheel.body.Position, new Vector3(0, 0, -1)); space.Add(joint); wheel = createWheel(new Vector3(position.X + (width / 2) - wheelRadius, position.Y, position.Z - (length / 2) - wheelWidth), wheelWidth, wheelRadius); joint = new RevoluteJoint(chassis.body, wheel.body, wheel.body.Position, new Vector3(0, 0, -1)); space.Add(joint); wheel = createWheel(new Vector3(position.X - (width / 2) + wheelRadius, position.Y, position.Z + (length / 2) + wheelWidth), wheelWidth, wheelRadius); joint = new RevoluteJoint(chassis.body, wheel.body, wheel.body.Position, new Vector3(0, 0, -1)); space.Add(joint); wheel = createWheel(new Vector3(position.X + (width / 2) - wheelRadius, position.Y, position.Z + (length / 2) + wheelWidth), wheelWidth, wheelRadius); joint = new RevoluteJoint(chassis.body, wheel.body, wheel.body.Position, new Vector3(0, 0, -1)); space.Add(joint); }
BepuEntity createBox(Vector3 position, float width, float height, float length) { BepuEntity theBox = new BepuEntity(); theBox.modelName = "cube"; theBox.LoadContent(); theBox.localTransform = Matrix.CreateScale(new Vector3(width, height, length)); theBox.body = new Box(position, width, height, length, 1); theBox.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); theBox.configureEvents(); space.Add(theBox.body); children.Add(theBox); return(theBox); }
BepuEntity createWheel(Vector3 position, float wheelWidth, float wheelRadius) { BepuEntity wheelEntity = new BepuEntity(); wheelEntity.modelName = "cyl"; wheelEntity.LoadContent(); wheelEntity.body = new Cylinder(position, wheelWidth, wheelRadius, wheelRadius); wheelEntity.localTransform = Matrix.CreateScale(wheelRadius, wheelWidth, wheelRadius); wheelEntity.body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.PiOver2); wheelEntity.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); space.Add(wheelEntity.body); children.Add(wheelEntity); return(wheelEntity); }
BepuEntity fireBall() { BepuEntity ball = new BepuEntity(); ball.modelName = "sphere"; float size = 1; ball.localTransform = Matrix.CreateScale(new Vector3(size, size, size)); ball.body = new Sphere(Camera.Position + (Camera.Look * 8), size, size * 10); ball.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); space.Add(ball.body); ball.LoadContent(); ball.configureEvents(); ball.body.ApplyImpulse(Camera.Position, Camera.Look * 500); children.Add(ball); return(ball); }
BepuEntity createFromMesh(Vector3 position, string mesh, float scale) { BepuEntity entity = new BepuEntity(); entity.modelName = mesh; entity.LoadContent(); Vector3[] vertices; int[] indices; TriangleMesh.GetVerticesAndIndicesFromModel(entity.model, out vertices, out indices); AffineTransform localTransform = new AffineTransform(new Vector3(scale, scale, scale), Quaternion.Identity, new Vector3(0, 0, 0)); MobileMesh mobileMesh = new MobileMesh(vertices, indices, localTransform, BEPUphysics.CollisionShapes.MobileMeshSolidity.Counterclockwise, 1); entity.localTransform = Matrix.CreateScale(scale, scale, scale); entity.body = mobileMesh; entity.HasColor = true; entity.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); entity.body.Position = position; space.Add(entity.body); children.Add(entity); return(entity); }
BepuEntity createCog(Vector3 position, float radius, int gears) { BepuEntity wheel = createWheel(position, 1, radius); float angleDelta = (MathHelper.Pi * 2.0f) / (float)gears; float cogHeight = radius * 0.2f; for (int i = 0; i < gears; i++) { float angle = ((float)i) * angleDelta; float x = (radius + (cogHeight / 2.0f)) * (float)Math.Sin(angle); float y = (radius + (cogHeight / 2.0f)) * (float)Math.Cos(angle); Vector3 cogPos = new Vector3(x, y, 0) + position; BepuEntity cog = createBox(cogPos, cogHeight, cogHeight, 1); cog.LoadContent(); cog.body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(0, 0, -1), angle); WeldJoint weld = new WeldJoint(wheel.body, cog.body); space.Add(weld); } return(wheel); }
BepuEntity fireBall() { BepuEntity ball = new BepuEntity(); ball.modelName = "sphere"; float size = 1; ball.localTransform = Matrix.CreateScale(new Vector3(size, size, size)); ball.body = new Sphere(Camera.Position + (Camera.Look * 8), size, size * 100); ball.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); space.Add(ball.body); ball.LoadContent(); ball.configureEvents(); ball.body.ApplyImpulse(Vector3.Zero, Camera.Look * 500); children.Add(ball); return ball; }
BepuEntity createBox(Vector3 position, float width, float height, float length) { BepuEntity theBox = new BepuEntity(); theBox.modelName = "cube"; theBox.localTransform = Matrix.CreateScale(new Vector3(width, height, length)); theBox.body = new Box(position, width, height, length, 1); theBox.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); theBox.configureEvents(); space.Add(theBox.body); children.Add(theBox); return theBox; }
BepuEntity createWheel(Vector3 position, float wheelWidth, float wheelRadius) { BepuEntity wheelEntity = new BepuEntity(); wheelEntity.modelName = "cyl"; wheelEntity.LoadContent(); wheelEntity.body = new Cylinder(position, wheelWidth, wheelRadius, wheelRadius); wheelEntity.localTransform = Matrix.CreateScale(wheelRadius, wheelWidth, wheelRadius); wheelEntity.body.Orientation = Quaternion.CreateFromAxisAngle(new Vector3(1, 0, 0), MathHelper.PiOver2); wheelEntity.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); space.Add(wheelEntity.body); children.Add(wheelEntity); return wheelEntity; }
BepuEntity createFromMesh(Vector3 position, string mesh, float scale) { BepuEntity entity = new BepuEntity(); entity.modelName = mesh; entity.LoadContent(); Vector3[] vertices; int[] indices; TriangleMesh.GetVerticesAndIndicesFromModel(entity.model, out vertices, out indices); AffineTransform localTransform = new AffineTransform(new Vector3(scale, scale, scale), Quaternion.Identity, new Vector3(0, 0, 0)); MobileMesh mobileMesh = new MobileMesh(vertices, indices, localTransform, BEPUphysics.CollisionShapes.MobileMeshSolidity.Counterclockwise, 1); entity.localTransform = Matrix.CreateScale(scale, scale, scale); entity.body = mobileMesh; entity.HasColor = true; entity.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); entity.body.Position = position; space.Add(entity.body); children.Add(entity); return entity; }
void fireBall() { BepuEntity ball = new BepuEntity(); ball.modelName = "sphere"; float size = 2; ball.localTransform = Matrix.CreateScale(new Vector3(size, size, size)); ball.body = new Sphere(Camera.pos + (Camera.look * 5), size, size); ball.diffuse = new Vector3((float)random.NextDouble(), (float)random.NextDouble(), (float)random.NextDouble()); space.Add(ball.body); ball.LoadContent(); ball.body.ApplyImpulse(Vector3.Zero, Camera.look * 50); children.Add(ball); }
protected override void Update(GameTime gameTime) { // Allows the game to exit float timeDelta = (float)gameTime.ElapsedGameTime.TotalSeconds; KeyboardState keyState = Keyboard.GetState(); MouseState mouseState = Mouse.GetState(); if (keyState.IsKeyDown(Keys.Escape)) { this.Exit(); } if (keyState.IsKeyDown(Keys.F1)) { if (!didSpawn) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; Vector3 point; if (ground.rayIntersects(ray, out point)) { point.Y = 10; createFromMesh(point, "fish", 10); didSpawn = true; } } } else if (keyState.IsKeyDown(Keys.F2)) { if (!didSpawn) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; Vector3 point; if (ground.rayIntersects(ray, out point)) { point.Y = 10; createFromMesh(point, "tube", 1); didSpawn = true; } } } else if (keyState.IsKeyDown(Keys.F3)) { if (!didSpawn) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; Vector3 point; if (ground.rayIntersects(ray, out point)) { point.Y = 10; createVehicle(point); didSpawn = true; } } } else if (keyState.IsKeyDown(Keys.F4)) { if (!didSpawn) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; Vector3 point; if (ground.rayIntersects(ray, out point)) { point.Y = 10; createFromMesh(point, "guy", 10); didSpawn = true; } } } else if (keyState.IsKeyDown(Keys.F5)) { if (!didSpawn) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; Vector3 point; if (ground.rayIntersects(ray, out point)) { point.Y = 25; BepuEntity cog1 = createCog(point, 10, 20); //BepuEntity cog2 = createCog(point, 10, 20); didSpawn = true; } } } else if (keyState.IsKeyDown(Keys.F12)) { resetScene(); didSpawn = true; } else { didSpawn = false; } if (mouseState.LeftButton == ButtonState.Pressed) { if (pickedUp == null) { Ray ray; ray.Position = Camera.Position; ray.Direction = Camera.Look; RayCastResult result; bool didDit = space.RayCast(ray, out result); if (didDit) { pickedUp = ((EntityCollidable)result.HitObject).Entity; if (pickedUp == groundBox) { pickedUp = null; } } } if (pickedUp != null) { float fDistance = 15.0f; float powerfactor = 50.0f; // Higher values causes the targets moving faster to the holding point. float maxVel = 40.0f; // Lower values prevent objects flying through walls. // Calculate the hold point in front of the camera Vector3 holdPos = Camera.Position + (Camera.Look * fDistance); Vector3 v = holdPos - pickedUp.Position; // direction to move the Target v *= powerfactor; // powerfactor of the GravityGun if (v.Length() > maxVel) { // if the correction-velocity is bigger than maximum v.Normalize(); v *= maxVel; // just set correction-velocity to the maximum } pickedUp.LinearVelocity = v; } } else { pickedUp = null; } if (keyState.IsKeyDown(Keys.F) & pickedUp != null) { pickedUp.LinearVelocity = Vector3.Zero; pickedUp.ApplyImpulse(pickedUp.Position, Camera.Look * 100.0f); pickedUp = null; } if (keyState.IsKeyDown(Keys.B) & lastFired > 0.25f) { fireBall(); lastFired = 0.0f; } lastFired += timeDelta; for (int i = 0; i < children.Count; i++) { children[i].Update(gameTime); } cameraCylindar.Position = camera.Position; space.Update(); base.Update(gameTime); }