Strange problem with collisions between static and non-stati

Official forum for the Chipmunk2D Physics Library.
Post Reply
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Strange problem with collisions between static and non-stati

Post by jj2009 »

Hi, first off, thanks for writing and making available chipmunk, it really is a godsend for all us programmers like me who haven't a clue about physics.

I am working on a little game project which involves a spaceship flying around some pre-defined levels, with objects etc, all bound into the chipmunk engine. The integration of chipmunk into my code has gone more or less smoothly, the objects behave as they should for the most part, although i have noticed a very strange behaviour, which I have not a clue how to solve, and wondered if someone who has more experience with chipmunk may have encountered this problem before.

The problem is, if I create a shape with a flat edge(horizontal), or even just a box, which then drops down onto another perfectly horizontal static line shape (the level geometry), it doesnt fall off the line until both vertices have fallen off the line. I would expect after about half-way over the edge of the line, it should topple over and fall, but it doesnt.
Since my explanation is rubbish, i have attached a picture as well. I tried initially with other poly shapes, like trapezoids, and eventually just substituted for a box copy and pasted box from one of the demos, and the same thing happens with all.
shape-problem.JPG

If anyone else has encountered this issue, or has any suggestion on how to solve it, please comment as I am stuck :?
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange problem with collisions between static and non-stati

Post by slembcke »

My best guess is that you are using an infinite (or just very large) moment of inertia. The moment of inertia is like the mass of an object, but describes how hard it is to get an object spinning instead of how hard it is to get it moving. Can you put up some of the code you use to create the cpBody for your object?
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Re: Strange problem with collisions between static and non-stati

Post by jj2009 »

yes, i can put it tommorow but i dont have it with my just now :(

I dont think its the moment of inertia, since the shapes will spin if they collide with each other, or if they fall down a slope. besides, i tried random values for the moment of inertia in the cPaddBody() or whatever its called, such as 0,0.1,100, INFINITY, etc.. all of them had the same effect .. they just wont fall over if they are perched on the edge.
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange problem with collisions between static and non-stati

Post by slembcke »

That does sound rather odd then. It doesn't quite sound like it fits the symptoms, but it could also be that you are adding the body that the static shapes are attached to to the space.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Re: Strange problem with collisions between static and non-stati

Post by jj2009 »

hi, thanks for looking at this, i have managed to copy the code that i was using, and have extracted the chipmunk related interesting bits. It turns out, I am not adding the body to the space :(

SETUP
------

void cLevel::InitialiseChipmunkEnvironment()
{
FinishChipmunkEnvironment();

cpInitChipmunk();

m_Space = cpSpaceNew();
m_Space->iterations = 10;
m_Space->elasticIterations = 10;

cpSpaceResizeActiveHash(m_Space, 128.0f, 50000);

m_Space->gravity = cpv(0,-100);

}

void cLevel::FinishChipmunkEnvironment()
{
if (m_Space)
{
// cpSpaceFreeChildren(m_Space);
// cpSpaceFree(m_Space);

cpSpaceDestroy(m_Space);
m_Space = NULL;
}
}

BINDING LEVEL GEOMETRY
----------------------

void cCompiledLevelGeometryBucket::BindChipmunk(cpSpace * space)
{
ASSERT_MSG(m_FloorBody == NULL,"error: only call bindchipmunk() once");

m_FloorBody = cpBodyNew(INFINITY, INFINITY);
m_FloorBody->p = cpv(0, 0);

NILineDefVector::iterator line_iter = m_PhysLineDefs.begin();

cpVect verts[2];
cpShape *newShape;
while (line_iter != m_PhysLineDefs.end())
{
verts[0] = cpv((*line_iter).v[0].x,-(*line_iter).v[0].y);
verts[1] = cpv((*line_iter).v[1].x,-(*line_iter).v[1].y);

newShape = cpPolyShapeNew(m_FloorBody, 2, verts, cpv(0.0f, 0.0f));
newShape->e = 0.5; newShape->u = 0.5; newShape->collision_type = 0;

cpSpaceAddStaticShape(space,newShape);


line_iter++;
}
}

CREATING THE TEST BOXES
------------------------

void cLevelObject::LinkWithChipmunk(cpSpace * space)
{
//add the circle thing if there wasnt a proper shape
if ((m_FixedData.m_ShapeObj) && (m_FixedData.m_ShapeObj->GetRootLayer()->GetCPVertsCount() > 2))
{

const cpFloat size = 25.0f;
const cpFloat mass = 100.0f;

cpVect verts[] = {
cpv(-size,-size),
cpv(-size, size),
cpv( size, size),
cpv( size,-size),
};

cpFloat radius = cpvlength(cpv(size, size));

m_CpBody = cpSpaceAddBody(space, cpBodyNew(mass, cpMomentForPoly(mass, 4, verts, cpvzero)));

cpShape *shape = cpSpaceAddShape(space, cpPolyShapeNew(m_CpBody, 4, verts, cpvzero));
shape->e = 0.2f; shape->u = 0.7f;


m_CpShape= shape;
vects,cpv(0,0));
} else {
m_CpShape = cpCircleShapeNew(m_CpBody, 10.0, cpvzero);
cpSpaceAddShape(space,m_CpShape);
}

}

void cLevelObject::ReleaseFromChipmunk()
{
if (m_CpBody)
{
cpBodyDestroy(m_CpBody);
cpShapeDestroy(m_CpShape);
}
m_CpBody = NULL;
m_CpShape = NULL;
}

//RENDERING THE OUTLINE OF THE TEST BOXES
-----------------------------------------

glPushMatrix();

cpBody *body = m_CpBody;
cpPolyShape *poly = (cpPolyShape*)m_CpShape;

int count = poly->numVerts;
float VAR[50];
glVertexPointer(2, GL_FLOAT, 0, VAR);

cpVect *verts = poly->verts;
for(int i=0; i<count; i++){
cpVect v = cpvadd(body->p, cpvrotate(verts, body->rot));
VAR[2*i ] = v.x;
VAR[2*i + 1] = -v.y;
}


glColor4f(1,1,0,1);
glDrawArrays(GL_LINE_LOOP, 0, count);
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Re: Strange problem with collisions between static and non-stati

Post by jj2009 »

ok, even wierder now, i replace the level geometry, instead of polyshapes with 2 verts, for segments with 2 verts and a radius of 1, and guess what? the problem has dissapeared...
User avatar
slembcke
Site Admin
Posts: 4166
Joined: Tue Aug 14, 2007 7:13 pm
Contact:

Re: Strange problem with collisions between static and non-stati

Post by slembcke »

Oh... Yeah. I didn't catch that in the code above.

Originally, poly shapes generated collision points as vertexes from other shapes that were inside their bounds. This was problematic for deeply penetrated or thin/pointy polys as a collision could be detected without being able to find any collision points. The solution to this was to find the separating axis and flag all vertexes behind it as collision points.

In the case of a box that is perfectly aligned with the "segment" poly, it could be picking the segment's axis and any vertex in the box below the segment would be used as a collision point. The problem wouldn't occur if the box was even slightly rotated, the problem wouldn't occur I think.

I'm not quite sure how to solve both issues, and since this one only occurs with perfect alignment it doesn't seem quite as important.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
jj2009
Posts: 6
Joined: Wed Jan 13, 2010 10:20 am
Contact:

Re: Strange problem with collisions between static and non-stati

Post by jj2009 »

don't worry, im just going to use the line segments instead, they work better than polyshapes anyhow because they overlap each other slightly at the edges. probably more efficient too.. maybe you could add a class like a segmentlinestrip or something for cases where there are many line segments in a strip. i guess this could happen very often, for example, in any game which utilises some form of terrain. (i don't know if that would optimise anything, since i haven't looked in any detail at the chipmunk internal code)

well, guess i must have caught a rare case in your code, thanks for looking at it anyway
Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests