Camera-Object Sliding collision

Discuss any questions about BEPUphysics or problems encountered.
Post Reply
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Camera-Object Sliding collision

Post by Orentz »

Hi,
I'm new here and I have to say that this engine looks amazing!

For days now I've been trying to make a simple sliding collision between the camera and 3D objects with no luck.

I'm sure this engine could help me do it easily. but I don't know how...

I've read the documentations and still didn't fully understand how to use this engine.

So please tell me - how do I do an FPS camera sliding collision with 3D objects using the engine?

Thanks alot in advanced!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Camera-Object Sliding collision

Post by Norbo »

For very simple collision, you can create a physical Sphere entity and set your camera's position to the sphere's position each frame. To move around with input, you would modify the velocities of the sphere. If you want a fixed speed with no acceleration, setting the linearVelocity of the sphere each frame to the appropriate value (determined by the camera's facing direction and movement speed properties) would work. You would also probably want to make gravity not affect the sphere by setting sphere.isAffectedByGravity = false.

Since the sphere is dynamic, it will respond to collisions (keeping you from going through obstacles) and also get knocked around by other objects that might hit it. If you don't want the sphere to rotate at all, set its localSpaceInertiaTensorInverse to a matrix full of zeros.

To create a level, you can use a StaticTriangleGroup and add it to the space. An example of this can be found in the Playground demo of the demos project.

For some basic set up, check out the PhysicsThread project in this post: http://www.bepu-games.com/forums/viewto ... read#p4250. It includes some more advanced implementations like offloading the physics to another thread, but the basic setup for a space/simple renderer is there. The space.update can be moved into the Update method of the XNA game class instead of on a separate thread to simplify things.

More advanced character based motion (jumping, walking, etc.) is done using a character controller, like the one in the demos project.
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Re: Camera-Object Sliding collision

Post by Orentz »

So from what I got so far, and correct me if I'm wrong:

I need to define a new space for my entire scene, to that space I add entities (box, sphere, models etc).

For my camera I use a sphere for example, but for my 3D models I need to create a triangleMesh entity like this:

StaticTriangleGroup.StaticTriangleGroupVertex[] staticTriangleVertices;
int[] staticTriangleIndices;

StaticTriangleGroup.getVerticesAndIndicesFromModel(myModel, out staticTriangleVertices, out staticTriangleIndices);
TriangleMesh mesh = new TriangleMesh(staticTriangleVertices, staticTriangleIndices, 0);
StaticTriangleGroup group = new StaticTriangleGroup(mesh);

(better done in the content pipeline and added to the tag property.)

Then I modify the Entities velocity and update the position of the model according to that (and collisions are automaticlly taken into acount), right?

so a few questions:

1. Do I add ALL of the models of the game into the "space"? does the engine first do a sphere-to-sphere check to see if the object is close? or do I need to do that check my self
and add to the space only objects which are near?

2. How do I get the entity's new position? (for rendering) ?


Or did I get the entire concept wrong?
I never used a physics engine before so I have no idea how it is supposed to wrok....
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Re: Camera-Object Sliding collision

Post by Orentz »

After playing around with the link you've posted, I tried to do the following:

Create a space, add my 3D objects as triangle Meshes to that space.
I also used your characterControllerInput + CharacterController classes, and updated their input in my update method.
When I tried to do "space.update(dt)" it says: "Index was out of range. Must be non-negative and less than the size of the collection."
(dt was 0.8 at that time)

I tried to move the update into a different thread and still - same exception is thrown.

What does it mean? what am I doing wrong?
and - once I get pass that exception, I should be able to collide with the objects right?

Thanks,

Oren
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Camera-Object Sliding collision

Post by Norbo »

Orentz wrote: Then I modify the Entities velocity and update the position of the model according to that (and collisions are automaticlly taken into acount), right?
Yes.
Orentz wrote: 1. Do I add ALL of the models of the game into the "space"? does the engine first do a sphere-to-sphere check to see if the object is close? or do I need to do that check my self
and add to the space only objects which are near?
Add everything you want the space to simulate up front. It handles everything, including property updating and the broad/narrow phase collision detection.
Orentz wrote: 2. How do I get the entity's new position? (for rendering) ?
(Entity).centerPosition will do the trick. In the PhysicsThread project you can check out the EntityModel class for an example simple rendering system that grabs entity information.
Orentz wrote: When I tried to do "space.update(dt)" it says: "Index was out of range. Must be non-negative and less than the size of the collection."
This is likely the same bug as http://bepu-games.com/forums/viewtopic.php?f=4&t=444; you can either give the constructor of the space a different Broadphase (perhaps a PersistentUniformGrid(10)) or you can just add an entity before calling space.update at some point.
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Re: Camera-Object Sliding collision

Post by Orentz »

Hi,
First of all thanks for all your help!

I got it working now and I must say it's amazing :)
After struggling for days to implement some basic sliding collision, your engine enabled me to do it with a few lines...

Some help is still needed though:

I couldn't make the terrain collision work...
I did this:

Terrain PhyiscsEntity;
PhysicsEntity = new BEPUphysics.Terrain(Vector3.Zero);
PhysicsEntity.setData(heightData, QuadFormats.upperLeftLowerRight, scale.X, scale.Z);

Then added the "physicsEntity" to my Space

heightData is the heightData from my Heightmap, I'm not sure what the second argument is... I tried both "QuadFormats" and neither worked.

My indices buffer is setted like this:

for (int y = 0; y < terrainLength - 1; y++)
{
for (int x = 0; x < terrainWidth - 1; x++)
{
int lowerLeft = x + y * terrainWidth;
int lowerRight = (x + 1) + y * terrainWidth;
int topLeft = x + (y + 1) * terrainWidth;
int topRight = (x + 1) + (y + 1) * terrainWidth;

indices[counter++] = topLeft;
indices[counter++] = lowerRight;
indices[counter++] = lowerLeft;

indices[counter++] = topLeft;
indices[counter++] = topRight;
indices[counter++] = lowerRight;
}
}



So what may be the problem?

Thanks again!
Norbo
Site Admin
Posts: 4929
Joined: Tue Jul 04, 2006 4:45 am

Re: Camera-Object Sliding collision

Post by Norbo »

The quadformats are the way the terrain interprets the heights to create triangles during collisions, where there are two triangles per quad. As long as your heights and scales are valid, the physics engine should be generating triangles when entities come near. My best guess is that the rendered terrain is offset from the physics representation, causing what looks like 'falling through' (if that is indeed what you are observing).
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Re: Camera-Object Sliding collision

Post by Orentz »

You're right, there's an offset...
The reason is that my vertices start from point 0,0 and advance in the positive X axis and NEGATIVE Z Axis while your engine apparently considers the Y coords of the heightmap as POSITIVE Z .

Any way to inverse that?

I tried multiplying the scale.Z by -1 , but that created incorrect collision...

I could recreate the entire level on the positive Z but that would make a mess because XNA considers the negative Z as "going forward" and inversing that would inverse my X axis too....
Orentz
Posts: 10
Joined: Sun Apr 05, 2009 7:02 pm

Re: Camera-Object Sliding collision

Post by Orentz »

Never mind I fixed it :)

I just set the Z value of the starting position of the map to the length of the map and created a new float[ , ] with
reversed Z values (HeightMap Length - original Z)
Post Reply