-
-
Save anonymous/f4a8a5937062c61c1665d463ca9af325 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 cube, sphere; | |
Box cubeBox, sphereBox; | |
Matrix viewMatrix, projectionMatrix; | |
KeyboardState current, previous; | |
public Game1() | |
{ | |
graphics = new GraphicsDeviceManager(this); | |
Content.RootDirectory = "Content"; | |
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 | |
cube = this.Content.Load<Model>("Cube"); | |
sphere = this.Content.Load<Model>("Cube"); | |
cubeBox = new Box(BEPUutilities.Vector3.Zero, 2f, 2f, 2f); | |
sphereBox = new Box(new BEPUutilities.Vector3(0f, 4f, 0f), 2f, 2f, 2f, 10f); | |
world.Add(cubeBox); | |
world.Add(sphereBox); | |
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) | |
{ | |
if (Keyboard.GetState().IsKeyDown(Keys.Escape)) | |
Exit(); | |
previous = current; | |
current = Keyboard.GetState(); | |
if(current != previous) | |
{ | |
if (Keyboard.GetState().IsKeyDown(Keys.Left)) | |
sphereBox.LinearVelocity += new BEPUutilities.Vector3(5f, 0f, 0f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Right)) | |
sphereBox.LinearVelocity -= new BEPUutilities.Vector3(5f, 0f, 0f); | |
if (Keyboard.GetState().IsKeyDown(Keys.Space)) | |
sphereBox.LinearVelocity += new BEPUutilities.Vector3(0f, 5f, 0f); | |
} | |
// Update the world | |
world.Update(); | |
base.Update(gameTime); | |
} | |
protected override void Draw(GameTime gameTime) | |
{ | |
GraphicsDevice.Clear(Color.CornflowerBlue); | |
foreach(ModelMesh mesh in cube.Meshes) | |
{ | |
foreach(BasicEffect effect in mesh.Effects) | |
{ | |
effect.View = viewMatrix; | |
effect.World = Matrix.CreateTranslation(new Vector3(cubeBox.Position.X, cubeBox.Position.Y, cubeBox.Position.Z)); | |
effect.Projection = projectionMatrix; | |
effect.EnableDefaultLighting(); | |
} | |
mesh.Draw(); | |
} | |
foreach (ModelMesh mesh in sphere.Meshes) | |
{ | |
foreach (BasicEffect effect in mesh.Effects) | |
{ | |
effect.View = viewMatrix; | |
effect.World = Matrix.CreateTranslation(new Vector3(sphereBox.Position.X, sphereBox.Position.Y, sphereBox.Position.Z)); | |
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