Mesh

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
marceloguto
Posts: 7
Joined: Wed Apr 23, 2008 4:23 pm

Mesh

Post by marceloguto »

Hi Guys,

Is there some class that inherit Entity that I can use with Mesh Models and apply physic like Box for example? I saw StaticTriangleGroup, but doesnt inherit Entity class and DiplayModel only display I can not apply physic.

Because, I´ve a Human model, then I´d like to walk on the another model B, this model B is a terrain where I´m using the StaticTriangleGroup class to load and do the treatment of collision.

I saw the examples like character movement, but all examples use primitives object (Box, Cube, and other), In my case I´d like to use Model´s.

Thanks for all.

Sorry for my english!
Marcelo
User avatar
Zukarakox
Not a Site Admin
Posts: 426
Joined: Mon Jul 10, 2006 4:28 am

Re: Mesh

Post by Zukarakox »

For models, you can give them to StaticTriangleGroups, which takes all the trianges of a mesh and makes into into a Static object.

Code: Select all

StaticTriangleGroup group = new StaticTriangleGroup(.5f, 1, 0, 0, .4f, 0);
//This loading method gathers data from the vertex and index buffers of the model.  Other initialization methods are available.
group.initializeData(playgroundModel.Meshes[0]);
For objects that you want to move and be physically active, you have to create a physical representation of the model using things like boxes.
For example, if you wanted to have a can rolling around, you would create a cylinder that matched the can, add the cylinder to space, and give the model to the DisplayModel class with the entity.

Code: Select all

 toAddBox = PrimitiveToolbox.constructCube(new Vector3(4, 1, 2), 1f, 3);
toAddBox.bounciness = 0;
toAddBox.friction = .3f;
space.add(toAddBox);
//Give the boxes a non-standard model.
displayBox = new DisplayModel(boxModel, toAddBox);
entityRenderer.displayModels.Add(displayBox);
toAddBox.tags.Add("usesDisplayModel", null); //Prevent a normal DisplayObject from being created for this entity later.
To create more complicated physical objects, you would need to use the CompoundBody class.


For the full example, download the BEPUphysics demo source and find the 'Static Triangle Group' demo.
i has multiple toes
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Mesh

Post by Norbo »

If you're looking to use concave meshes for physical objects, they are not currently explicitly supported. The closest you can get is compound bodies as mentioned by Zukarakox, or perhaps if your mesh is convex, giving the triangles to a ConvexPolyhedron for simulation.

Dynamic concave meshes are particularly difficult to simulate expediently, so in BEPUphysics I have thus far 'dodged' their performance issue and recommended much faster alternatives. Note that in the vast majority of games, collision geometry only loosely fits the character. In the few that don't, it's still usually a set of approximations (for humans, capsules are used commonly for limbs) that aren't actually the graphical model.
marceloguto
Posts: 7
Joined: Wed Apr 23, 2008 4:23 pm

Re: Mesh

Post by marceloguto »

Thanks my friends fo help!

Do you have some example about how to use CompoundBody class? I think this is the best way, because I think in create a capsule for each limbs, it´s ok ??

regards,
Marcelo
User avatar
Zukarakox
Not a Site Admin
Posts: 426
Joined: Mon Jul 10, 2006 4:28 am

Re: Mesh

Post by Zukarakox »

Yes, you can do that.

Creating a compound body class is like this.

Code: Select all

CompundBody cBody = new CompoundBody()
Entity EntToAdd;

EntToAdd = new Box(new Vector3(0, 2, 0), 12, 4, 1, 30f);
cBody.AddBody(EntToAdd);
EntToAdd = new Box(new Vector3(0, 0.5f, 2f), 12, 1, 5, 30f);
cBody.AddBody(EntToAdd);

Game1.space.add(cBody);
Compound bodies can hold as many entities as necessary, but they can't be static.

Note: When you add the CompoundBody's Model to the EntityDrawer, it won't always be perfectly aligned with the CompoundBody. I suggest adding both the model and the CompoundBody to the EntityDrawer and then adding an offset until it matches.

Heres my code for creating an offset in the DisplayModel class:

Replace the old worldMatrix code with this in the Draw method.

Code: Select all

worldMatrix = Matrix.CreateTranslation(offset) * entity.rotationMatrix * Matrix.CreateTranslation(entity.centerPosition);
Add this method and variable:

Code: Select all

public DisplayModel(Model modelToDraw, Entity entityToFollow, Vector3 Offset)
{
model = modelToDraw;
entity = entityToFollow;
offset = Offset;
}
Vector3 offset = Vector3.Zero;
Then to add a model with an offset, just do 'DisplayModel model = new DisplayModel(myModel, cBody, new Vector3(2, 3, -1)'

You'll have to play with the offset a bit, but eventually you'll match it up and it'll look great :D
i has multiple toes
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Mesh

Post by Norbo »

I should note that CompoundBodies are not well suited to animating characters (if that was your goal). Additionally, even if a hack was put together that let it work 'acceptably' for animated characters, there would still be significant problems with response (as the compound parts would be moved nonphysically). I would recommend that the collision against the world take place using a nonchanging shape (or only sometimes changing, like crouching or going prone). You could separately keep track of limb-representing capsules that follow the model and can be tested against other things, like bullets via rays.

Also, you can find examples of compound bodies in the demos source.
marceloguto
Posts: 7
Joined: Wed Apr 23, 2008 4:23 pm

Re: Mesh

Post by marceloguto »

Norbo and Zukarakox thanks for help =]

I´ll try do all things that you tell me, after that I send the code to rich the forum.

[]´s
Marcelo
Post Reply