Noob Question: Primitives and BEPU from Start to Finish?
Posted: Sun Feb 14, 2010 4:23 pm
Hi, I've been messing around with BEPU for the past 2 days, and I still haven't figured out how to get it to work with Primitives. I looked through the getting started guide, but that contained some things that I had no idea how to do, and it didn't specify how to do them. I looked at the source for the Getting Started project as well, but I couldn't understand it enough to where I could put it into my own code.
Right now, I have the following code:
Game1.cs
-----------------------------------------------------------------------------
BasicShape.cs
Sorry for all the code, but I have absolutely no idea how to do this.
What I'm trying to accomplish here is to be able to create primitives and let the Physics system do the work from there. I need the easiest method as possible to do this, and if you have to explain something, please say it like you're explaining it to a 3 year old. I'm trying to make a game with building blocks essentially, where the game loads an XML file with all the properties, and then builds the map accordingly. So primitives are going to be a big necessity in this situation.
Any help would be great.
Right now, I have the following code:
Game1.cs
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using BEPUphysics;
using BEPUphysics.Entities;
using BEPUphysics.DataStructures;
namespace Sjomsites___Worlds
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Matrix worldMatrix;
Matrix cameraMatrix;
Matrix projectionMatrix;
float angle = 0f;
Space space;
BasicEffect cubeEffect;
BasicShape cube = new BasicShape(new Vector3(2, 2, 2), new Vector3(0, 0, 0));
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
/// <summary>
/// Allows the game to perform any initialization it needs to before starting to run.
/// This is where it can query for any required services and load any non-graphic
/// related content. Calling base.Initialize will enumerate through any components
/// and initialize them as well.
/// </summary>
protected override void Initialize()
{
initializeWorld();
base.Initialize();
}
/// <summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
/// </summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
//cube.shapeTexture = Content.Load<Texture2D>("Textures\\100x100 SM Icon");
// Instantiate the Space Variable
space = new Space();
// Set the gravity variable in the space variable to a more realistic standard
space.simulationSettings.motionUpdate.gravity = new Vector3(0, -9.81f, 0);
}
/// <summary>
/// UnloadContent will be called once per game and is the place to unload
/// all content.
/// </summary>
protected override void UnloadContent()
{
// TODO: Unload any non ContentManager content here
}
/// <summary>
/// Allows the game to run logic such as updating the world,
/// checking for collisions, gathering input, and playing audio.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Update(GameTime gameTime)
{
// Allows the game to exit
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
angle += 0.5f;
base.Update(gameTime);
}
/// <summary>
/// This is called when the game should draw itself.
/// </summary>
/// <param name="gameTime">Provides a snapshot of timing values.</param>
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
cubeEffect.Begin();
worldMatrix = Matrix.CreateRotationY(MathHelper.ToRadians(angle)) *
Matrix.CreateRotationX(MathHelper.ToRadians(angle));
cubeEffect.World = worldMatrix;
foreach (EffectPass pass in cubeEffect.CurrentTechnique.Passes)
{
pass.Begin();
cubeEffect.Texture = cube.shapeTexture;
cube.RenderShape(GraphicsDevice);
pass.End();
}
cubeEffect.End();
base.Draw(gameTime);
}
public void initializeWorld()
{
cameraMatrix = Matrix.CreateLookAt(
new Vector3(0, 30, 20), new Vector3(0, 0, 0), new Vector3(0, 1, 0));
projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
Window.ClientBounds.Width / Window.ClientBounds.Height, 1.0f, 50.0f);
float tilt = MathHelper.ToRadians(22.5f);
worldMatrix = Matrix.CreateRotationX(tilt) * Matrix.CreateRotationY(tilt);
cubeEffect = new BasicEffect(graphics.GraphicsDevice, null);
cubeEffect.World = worldMatrix;
cubeEffect.View = cameraMatrix;
cubeEffect.Projection = projectionMatrix;
cubeEffect.TextureEnabled = true;
}
}
}
BasicShape.cs
Code: Select all
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using BEPUphysics;
using BEPUphysics.Entities;
using BEPUphysics.DataStructures;
namespace Sjomsites___Worlds
{
class BasicShape
{
public Vector3 shapeSize;
public Vector3 shapePosition;
private StaticTriangleGroup.StaticTriangleGroupVertex[] shapeVertices;
private int shapeTriangles;
private VertexBuffer shapeBuffer;
public Texture2D shapeTexture;
public TriangleMesh shapeMesh;
public StaticTriangleGroup shapeGroup;
public BasicShape(Vector3 size, Vector3 position)
{
shapeSize = size;
shapePosition = position;
}
private void BuildShape()
{
shapeTriangles = 12;
shapeVertices = new StaticTriangleGroup.StaticTriangleGroupVertex[36];
Vector3 topLeftFront = shapePosition +
new Vector3(-1.0f, 1.0f, -1.0f) * shapeSize;
Vector3 bottomLeftFront = shapePosition +
new Vector3(-1.0f, -1.0f, -1.0f) * shapeSize;
Vector3 topRightFront = shapePosition +
new Vector3(1.0f, 1.0f, -1.0f) * shapeSize;
Vector3 bottomRightFront = shapePosition +
new Vector3(1.0f, -1.0f, -1.0f) * shapeSize;
Vector3 topLeftBack = shapePosition +
new Vector3(-1.0f, 1.0f, 1.0f) * shapeSize;
Vector3 topRightBack = shapePosition +
new Vector3(1.0f, 1.0f, 1.0f) * shapeSize;
Vector3 bottomLeftBack = shapePosition +
new Vector3(-1.0f, -1.0f, 1.0f) * shapeSize;
Vector3 bottomRightBack = shapePosition +
new Vector3(1.0f, -1.0f, 1.0f) * shapeSize;
Vector3 frontNormal = new Vector3(0.0f, 0.0f, 1.0f) * shapeSize;
Vector3 backNormal = new Vector3(0.0f, 0.0f, -1.0f) * shapeSize;
Vector3 topNormal = new Vector3(0.0f, 1.0f, 0.0f) * shapeSize;
Vector3 bottomNormal = new Vector3(0.0f, -1.0f, 0.0f) * shapeSize;
Vector3 leftNormal = new Vector3(-1.0f, 0.0f, 0.0f) * shapeSize;
Vector3 rightNormal = new Vector3(1.0f, 0.0f, 0.0f) * shapeSize;
Vector2 textureTopLeft = new Vector2(0.5f * shapeSize.X, 0.0f * shapeSize.Y);
Vector2 textureTopRight = new Vector2(0.0f * shapeSize.X, 0.0f * shapeSize.Y);
Vector2 textureBottomLeft = new Vector2(0.5f * shapeSize.X, 0.5f * shapeSize.Y);
Vector2 textureBottomRight = new Vector2(0.0f * shapeSize.X, 0.5f * shapeSize.Y);
// Front face.
shapeVertices[0] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftFront);
shapeVertices[1] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftFront);
shapeVertices[2] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightFront);
shapeVertices[3] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftFront);
shapeVertices[4] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightFront);
shapeVertices[5] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightFront);
// Back face.
shapeVertices[6] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftBack);
shapeVertices[7] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightBack);
shapeVertices[8] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftBack);
shapeVertices[9] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftBack);
shapeVertices[10] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightBack);
shapeVertices[11] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightBack);
// Top face.
shapeVertices[12] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftFront);
shapeVertices[13] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightBack);
shapeVertices[14] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftBack);
shapeVertices[15] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftFront);
shapeVertices[16] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightFront);
shapeVertices[17] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightBack);
// Bottom face.
shapeVertices[18] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftFront);
shapeVertices[19] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftBack);
shapeVertices[20] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightBack);
shapeVertices[21] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftFront);
shapeVertices[22] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightBack);
shapeVertices[23] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightFront);
// Left face.
shapeVertices[24] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftFront);
shapeVertices[25] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftBack);
shapeVertices[26] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftFront);
shapeVertices[27] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftBack);
shapeVertices[28] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomLeftBack);
shapeVertices[29] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topLeftFront);
// Right face.
shapeVertices[30] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightFront);
shapeVertices[31] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightFront);
shapeVertices[32] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightBack);
shapeVertices[33] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightBack);
shapeVertices[34] = new StaticTriangleGroup.StaticTriangleGroupVertex(
topRightFront);
shapeVertices[35] = new StaticTriangleGroup.StaticTriangleGroupVertex(
bottomRightBack);
int[] indicies = new int[6];
indicies[0] = 1;
indicies[1] = 2;
indicies[2] = 3;
indicies[3] = 4;
indicies[4] = 5;
indicies[5] = 6;
shapeMesh = new TriangleMesh(shapeVertices, indicies);
shapeGroup = new StaticTriangleGroup(shapeMesh);
}
public void RenderShape(GraphicsDevice device, Space space)
{
BuildShape();
space.add(shapeGroup);
}
}
}
What I'm trying to accomplish here is to be able to create primitives and let the Physics system do the work from there. I need the easiest method as possible to do this, and if you have to explain something, please say it like you're explaining it to a 3 year old. I'm trying to make a game with building blocks essentially, where the game loads an XML file with all the properties, and then builds the map accordingly. So primitives are going to be a big necessity in this situation.
Any help would be great.