Skip to content

Instantly share code, notes, and snippets.

/Game1.cpp Secret

Created September 17, 2016 03:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/f4a8a5937062c61c1665d463ca9af325 to your computer and use it in GitHub Desktop.
Save anonymous/f4a8a5937062c61c1665d463ca9af325 to your computer and use it in GitHub Desktop.
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