Page 1 of 1
BEPUphysics world Shift+1
Posted: Thu May 24, 2012 9:09 am
by Keta
Hey everyone.
I'm currently looking for the best suitable way to make the world for my game, and I'm using BEPUphysics. I've been looking at the world in the BEPUphysics examples you can get into by pressing Shift+1. And the ground in that world looks pretty much like what I need. So I was pretty much wondering if someone could tell me how I am going to go about generating the collision for my FBX world. Because in theory I could look through the code and try to figure it out for myself, but I'm not exactly a professional programmer, so I'm 99% sure I'm going to fail utterly the first MANY times. And if any of you guys knows exactly how its done and would share, I would be really grateful.
Thanks
Re: BEPUphysics world Shift+1
Posted: Thu May 24, 2012 2:35 pm
by Norbo
The BEPUphysicsDemos source code is available in the
main source download; you don't have to worry about digging through the main library project or anything, just jump down into the BEPUphysicsDemos project's Demos folder. The Shift+1 demo is the StaticMeshDemo. The StaticMesh itself is made like so:
Code: Select all
//Load in mesh data and create the collision mesh.
Vector3[] staticTriangleVertices;
int[] staticTriangleIndices;
var playgroundModel = game.Content.Load<Model>("playground");
//This is a little convenience method used to extract vertices and indices from a model.
//It doesn't do anything special; any approach that gets valid vertices and indices will work.
TriangleMesh.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3X3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
staticMesh.Sidedness = TriangleSidedness.Counterclockwise;
Space.Add(staticMesh);
Playing with the demos source is one of the best ways to get familiar with the engine. I'd recommend just trying to make stuff, squish stuff, throw stuff and have fun for a while to get a hang of it

Re: BEPUphysics world Shift+1
Posted: Thu May 24, 2012 4:08 pm
by Keta
Could someone explain to me exactly why it says "Index is outside of the list's bounds." here?
Code: Select all
var playgroundModel = Content.Load<Model>("TownSquare");
//This is a little convenience method used to extract vertices and indices from a model.
//It doesn't do anything special; any approach that gets valid vertices and indices will work.
TriangleMesh.GetVerticesAndIndicesFromModel(playgroundModel, out staticTriangleVertices, out staticTriangleIndices);
var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3X3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
staticMesh.Sidedness = TriangleSidedness.Counterclockwise;
space.Add(staticMesh);
space.ForceUpdater.Gravity = new Vector3(0, -9.81f, 0);
Box GroundBox = space.Entities[0] as Box; // <--- Here its complaining
Matrix scaling = Matrix.CreateScale(GroundBox.Width, GroundBox.Height, GroundBox.Length);
Components.Add(new EntityModel(space.Entities[0], playgroundModel, scaling, this, true));
I'm thinking maybe its because its not a box... But how AM I supposed to add it to the Components then? :s
Thanks
Re: BEPUphysics world Shift+1
Posted: Thu May 24, 2012 4:15 pm
by Norbo
If you have not added an entity to the Space yet, there are no entities in the Space.Entities list. Attempting to grab things out of the empty list will find nothing and throw an exception.
The StaticMesh is not an entity. Entities are objects capable of motion (dynamic or kinematic). A StaticMesh is not capable of motion. (Further, if it were an Entity, attempting to cast it to a Box would still fail. Only objects which are Boxes or inherit from Box can be cast to Boxes.)
Components are not necessary to draw things, and they (and all of graphics) are completely separate from the physics simulation. The EntityModel is not necessary to draw things either. You can use any form of rendering. Check out the
App Hub website for a bunch of samples about rendering and related topics.
Re: BEPUphysics world Shift+1
Posted: Thu May 24, 2012 7:29 pm
by Keta
Alright, I'm beginning to understand what you mean. However... Could you tell me if this code is correct?
Initialize
Code: Select all
Vector3[] staticTriangleVertices;
int[] staticTriangleIndices;
playgroundModel = new GameModel(Content);
playgroundModel.Load("TownSquare");
TriangleMesh.GetVerticesAndIndicesFromModel(playgroundModel.TestModel, out staticTriangleVertices, out staticTriangleIndices);
var staticMesh = new StaticMesh(staticTriangleVertices, staticTriangleIndices, new AffineTransform(Matrix3X3.CreateFromAxisAngle(Vector3.Up, MathHelper.Pi), new Vector3(0, -10, 0)));
staticMesh.Sidedness = TriangleSidedness.Counterclockwise;
space.Add(staticMesh);
Draw
Code: Select all
playgroundModel.TestModel.CopyAbsoluteBoneTransformsTo(boneTransforms);
foreach (ModelMesh mesh in playgroundModel.TestModel.Meshes)
{
foreach (BasicEffect effect in mesh.Effects)
{
effect.World = boneTransforms[mesh.ParentBone.Index];
effect.View = myCamera.View;
effect.Projection = myCamera.Projection;
effect.EnableDefaultLighting();
effect.SpecularColor = new Vector3(0.25f);
effect.SpecularPower = 16;
}
mesh.Draw();
}
And the class which playgroundModel is:
http://pastebin.com/Jtufm8Lx
Is that correct? Because when I spawn my character, it collides very oddly with the world

If I'm wrong, please do let me know where exactly I'm wrong.
Thanks
Re: BEPUphysics world Shift+1
Posted: Thu May 24, 2012 7:34 pm
by Norbo
The StaticMesh is given a non-identity AffineTransform. However, it appears the graphic is not using the same transform, so they do not match. Instead of including only the model's bone transforms in the effect's World matrix, the StaticMesh's transform must be applied too.
If you ever run into a case where you're not sure if something is caused by a graphics problem or a physics configuration issue, jump over to the BEPUphysicsDemos and try setting up an isolated demo using the BEPUphysicsDrawer to visualize it. The BEPUphysicsDrawer is a good debug visualizer because it understands how to interpret physics objects (though not in a very glamorous-looking way). The BEPUphysicsDrawer debug visualizer can also be used in other projects for debugging.