Sign in to follow this  
sjaakiejj

[ODE] Access Violation at dCollide when using TriMeshes

Recommended Posts

Hey guys,

I've been scratching my head at this for a while now, and Google hasn't been my friend for this particular issue. This is my first time using ODE after somebody recommended it to me, and it seems to be a fairly nice and straightforward library to use. However, I'm having some trouble with the use of TriMeshes. Since I want to have terrain collision detection, I decided to use ODE's built-in TriMesh to represent the terrain. Of course, I start off with a simple 2-triangle square representing my "Terrain". I do that with the following code:

[code]
void PhysicsManager::createTriMeshBB(Model* model)
{
const int vCount = 4;
float vertices[vCount * 3] = {
0,0,0,
20*30,0,0,
20*30,0,20*30,
0,0,20*30
};
dTriIndex indices[6] = {
0,1,2,
0,2,3
};

dTriMeshDataID dataID;
dGeomID geomID;

dataID = dGeomTriMeshDataCreate();
dGeomTriMeshDataBuildSingle(dataID, vertices, sizeof(float)*3, vCount, indices, 6, sizeof(dTriIndex)*3);
//dGeomTriMeshDataBuildSimple(dataID, vertices, 4, tmpCreateIndices(), 6);

geomID = dCreateTriMesh(mSpace, dataID, 0, 0, 0);

dGeomSetPosition(geomID,0,0,0);
dGeomSetBody(geomID, 0);

model->setGeometryID(geomID);
}
[/code]

After having done this, I specify a callback function - say "tmpCallback" as below:

[code]
void PhysicsManager::tmpCallback(void * d, dGeomID id1, dGeomID id2)
{


int i;

// Get the dynamics bodies
dBodyID b1 = dGeomGetBody(id1);
dBodyID b2 = dGeomGetBody(id2);

std::cout << "Callback" << std::endl;
// Holds the contact joints - Need to read up on this
dContact contact[32];
dContactGeom ptr;

for (i = 0; i < 32; i++)
{
contact[i].surface.mode = dContactBounce;
contact[i].surface.mu = dInfinity;
contact[i].surface.mu2 = 0;
contact[i].surface.bounce = 0.01;
contact[i].surface.bounce_vel = 0.5;
contact[i].surface.soft_cfm = 0.01;
}
int numc = dCollide(id1, id2, 1, &contact[0].geom, sizeof(dContact));
if (numc > 0)
{
for (i = 0; i < numc; i++)
{
dJointID c = dJointCreateContact(PhysicsManager::get()->getWorld(), PhysicsManager::get()->getContactGroup(), contact + i);
dJointAttach(c, b1, b2);
}
}
}
[/code]

This callback function works perfectly when doing plane to sphere collision detection. When the TriMesh is involved however, the program crashes at "dCollide(id1,id2, 1, &contact[0].geom, sizeof(dContact))", throwing an unhandled Access Violation from inside the standard libraries. I'm at a loss at what's causing the problem, and I've tried everything I could think of to solve it.

If you want to slap me in the face and tell me I shouldn't be using ODE, that's fine as well - please do give your own recommendations however :)

Thanks in advance

Share this post


Link to post
Share on other sites
[quote name='sjaakiejj' timestamp='1333314094' post='4927275']
dGeomTriMeshDataBuildSingle(dataID, vertices, sizeof(float)*3, vCount, indices, 6, sizeof(dTriIndex)*3);
[/quote]
From the doc over [url="http://192.121.234.229/manualer/programering/Tao-doc/Tao.Ode/Tao.Ode.Ode.dGeomTriMeshDataBuildSingle.html"]here[/url]:
[quote]
Applies to all the dGeomTriMeshDataBuild single and double versions. (From http://ode.org/ode-latest-userguide.html#sec_10_7_6) Used for filling a dTriMeshData object with data. [u][b]No data is copied here[/b][/u], so the pointers passed into this function must remain valid.
[/quote]
So, you fill your data with temporary data which will be gone once you leave the createTriMeshBB method. Here's a fast hack, please do this not at home:
[CODE]
static float vertices[vCount * 3] = {
0,0,0,
20*30,0,0,
20*30,0,20*30,
0,0,20*30
};
static dTriIndex indices[6] = {
0,1,2,
0,2,3
};
[/CODE]

When this work, try to use some proper memory management to handle your mesh data.

Share this post


Link to post
Share on other sites
[quote name='Ashaman73' timestamp='1333444971' post='4927825']
[quote name='sjaakiejj' timestamp='1333314094' post='4927275']
dGeomTriMeshDataBuildSingle(dataID, vertices, sizeof(float)*3, vCount, indices, 6, sizeof(dTriIndex)*3);
[/quote]
From the doc over [url="http://192.121.234.229/manualer/programering/Tao-doc/Tao.Ode/Tao.Ode.Ode.dGeomTriMeshDataBuildSingle.html"]here[/url]:
[quote]
Applies to all the dGeomTriMeshDataBuild single and double versions. (From [url="http://ode.org/ode-latest-userguide.html#sec_10_7_6"]http://ode.org/ode-l...html#sec_10_7_6[/url]) Used for filling a dTriMeshData object with data. [u][b]No data is copied here[/b][/u], so the pointers passed into this function must remain valid.
[/quote]
So, you fill your data with temporary data which will be gone once you leave the createTriMeshBB method. Here's a fast hack, please do this not at home:
[CODE]
static float vertices[vCount * 3] = {
0,0,0,
20*30,0,0,
20*30,0,20*30,
0,0,20*30
};
static dTriIndex indices[6] = {
0,1,2,
0,2,3
};
[/CODE]

When this work, try to use some proper memory management to handle your mesh data.
[/quote]

How could I have missed that line? Besides, it's pretty logical... Thanks a lot, I'll go try it immediately [img]http://public.gamedev.net//public/style_emoticons/default/smile.png[/img]

Edit: It works, I had an additional issue afterwards which was that my object was just falling through the plane, but turned out the plane was upside down.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this