I've been using boxes for collision detection and I'm not happy with it so I decided to use box2d for collision (only collision, not simulation).
Right now I'm testing it with a game object to see how it works. In the object's Update function, when the screen coordinates update, I update the b2PolygonShape object (I use screen co-ordinates for both, I think it's fine as I'm not simulating).
This is the update code:
The collision2 function test for collision:
The function returns 0 all the time(no collision points). What's wrong?
Right now I'm testing it with a game object to see how it works. In the object's Update function, when the screen coordinates update, I update the b2PolygonShape object (I use screen co-ordinates for both, I think it's fine as I'm not simulating).
This is the update code:
Code: Select all
void Obj::Update()
{
/* ... */
const uint VERTEX_COUNT = 4;
b2Vec2 vertices[VERTEX_COUNT];
vertices[0].Set(x() + w() / 2, y());
vertices[1].Set(x(), y() + h() / 2);
vertices[2].Set(x() + w() / 2, y() + h());
vertices[3].Set(x() + w(), y() + h() / 2);
dynamic_cast <b2PolygonShape*>(shape.get())->Set(vertices, VERTEX_COUNT); // using dynamic cast because I have b2Shape object, actually it's std::auto_ptr<b2Shape>
}The collision2 function test for collision:
Code: Select all
bool collision2(const GameObject &ob1, const GameObject &ob2)
{
b2Shape *s1 = ob1.Shape(),
*s2 = ob2.Shape();
b2Transform t1, t2;
t1.SetIdentity();
t2.SetIdentity();
b2Manifold manifold;
if( s1->GetType() == b2Shape::e_polygon && s2->GetType() == b2Shape::e_polygon )
b2CollidePolygons(&manifold, dynamic_cast<b2PolygonShape*>(s1), t1, dynamic_cast<b2PolygonShape*>(s2), t2); // right now I have only polygons, I'll add the other when this works ;)
return manifold.pointCount;
}The function returns 0 all the time(no collision points). What's wrong?