-
-
Save anonymous/b6574aff509dbc11b267f87b5dd63205 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BEPUphysics; | |
using BEPUphysics.Entities.Prefabs; | |
using Microsoft.Xna.Framework; | |
using Microsoft.Xna.Framework.Graphics; | |
using Microsoft.Xna.Framework.Input; | |
namespace Physics | |
{ | |
public class Game1 : Game | |
{ | |
GraphicsDeviceManager graphics; | |
SpriteBatch spriteBatch; | |
Space world; | |
Model floor, wall, player; | |
Box floorBox, wallBox, playerBox; | |
Matrix viewMatrix, projectionMatrix; | |
public Game1() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
this.graphics.SynchronizeWithVerticalRetrace = true; | |
this.IsMouseVisible = true; | |
this.graphics.PreferredBackBufferWidth = 1280; | |
this.graphics.PreferredBackBufferHeight = 720; | |
} | |
protected override void Initialize() | |
{ | |
world = new Space(); | |
viewMatrix = Matrix.CreateLookAt(new Vector3(0f, 4f, -10f), new Vector3(0f, 0f, 0f), Vector3.Up); | |
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(65f), GraphicsDevice.DisplayMode.AspectRatio, 0.5f, 1000f); | |
base.Initialize(); | |
} | |
protected override void LoadContent() | |
{ | |
// Create a new SpriteBatch, which can be used to draw textures. | |
spriteBatch = new SpriteBatch(GraphicsDevice); | |
// Load models | |
floor = this.Content.Load<Model>("Cube"); | |
wall = this.Content.Load<Model>("Cube2"); | |
player = this.Content.Load<Model>("Player"); | |
// Kinematic Boxes | |
floorBox = new Box(BEPUutilities.Vector3.Zero, 2f, 2f, 2f); | |
wallBox = new Box(new BEPUutilities.Vector3(0f, 0f, 5f), 2f, 2f, 2f); | |
// Dynamic Boxes | |
playerBox = new Box(new BEPUutilities.Vector3(0f, 4f, 0f), 2f, 2f, 2f, 10f); | |
// Add to world so collision system can do its work | |
world.Add(floorBox); | |
world.Add(playerBox); | |
world.Add(wallBox); | |
// Resize Box if scaling in draw | |
playerBox.Width *= 0.5f; | |
playerBox.Height *= 0.5f; | |
playerBox.Length *= 0.5f; | |
wallBox.Width *= 8f; | |
wallBox.Height *= 3f; | |
wallBox.Length *= 1.5f; | |
// Set a rotation (no need to multiply by worldTransform, done automatically) | |
playerBox.Orientation = BEPUutilities.Quaternion.CreateFromAxisAngle(BEPUutilities.Vector3.Up, MathHelper.ToRadians(45f)); | |
// Set the gravity | |
world.ForceUpdater.Gravity = new BEPUutilities.Vector3(0f, -20f, 0f); | |
} | |
protected override void UnloadContent() | |
{ | |
// TODO: Unload any non ContentManager content here | |
spriteBatch.Dispose(); | |
Content.Dispose(); | |
} | |
protected override void Update(GameTime gameTime) | |
{ | |
float deltaTime = (float) gameTime.ElapsedGameTime.TotalSeconds; | |
if (Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
Exit(); | |
if (Keyboard.GetState().IsKeyDown(Keys.Up)) | |
playerBox.LinearVelocity += new BEPUutilities.Vector3(0f, 0f, 1f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Down)) | |
playerBox.LinearVelocity -= new BEPUutilities.Vector3(0f, 0f, 1f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Left)) | |
playerBox.LinearVelocity += new BEPUutilities.Vector3(1f, 0f, 0f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Right)) | |
playerBox.LinearVelocity -= new BEPUutilities.Vector3(1f, 0f, 0f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Space)) | |
playerBox.LinearVelocity += new BEPUutilities.Vector3(0f, 1.5f, 0f); | |
if (playerBox.Position.Y <= -30f) | |
playerBox.Position = new BEPUutilities.Vector3(0f, 4f, 0f); | |
// Update the world | |
world.Update(); | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
foreach(ModelMesh mesh in floor.Meshes) | |
{ | |
foreach(BasicEffect effect in mesh.Effects) | |
{ | |
effect.View = viewMatrix; | |
// box.WorldTransform has translation inside of it already | |
effect.World = ConvertTo.XNAMatrix(floorBox.WorldTransform); | |
effect.Projection = projectionMatrix; | |
effect.EnableDefaultLighting(); | |
} | |
mesh.Draw(); | |
} | |
foreach (ModelMesh mesh in wall.Meshes) | |
{ | |
foreach (BasicEffect effect in mesh.Effects) | |
{ | |
effect.View = viewMatrix; | |
// box.WorldTransform has translation inside of it already | |
effect.World = ConvertTo.XNAMatrix(BEPUutilities.Matrix.CreateScale(8f, 3f, 1.5f) * wallBox.WorldTransform); | |
effect.Projection = projectionMatrix; | |
effect.EnableDefaultLighting(); | |
} | |
mesh.Draw(); | |
} | |
foreach (ModelMesh mesh in player.Meshes) | |
{ | |
foreach (BasicEffect effect in mesh.Effects) | |
{ | |
effect.View = viewMatrix; | |
// box.WorldTransform has translation inside of it already | |
effect.World = ConvertTo.XNAMatrix(BEPUutilities.Matrix.CreateScale(0.5f, 0.5f, 0.5f) * playerBox.WorldTransform); | |
effect.Projection = projectionMatrix; | |
effect.EnableDefaultLighting(); | |
} | |
mesh.Draw(); | |
} | |
base.Draw(gameTime); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment