• Advertisement
Sign in to follow this  

2D collisions

This topic is 3344 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

q1) Does it make sense to have a collision object in OOP where it works out how objects collide and their responses . A collision is a verb and not a noun or a thing so it is confusing but it separates much code from main procedure. q2)I have an object 50X50 pixels and a 64X64 pixel object. The 50X50 object can move any direction but 64X64 object is still. To simply test to see how they collide I am looking at testing every corner on 50X50 object, and each side. That makes 8 tests and not to mention if the object can find itself over each other so that makes 9 tests.Is there an easier way?

Share this post


Link to post
Share on other sites
Advertisement
q1 You can use either bounding box or bounding circle (or sphere in 3d) and both can be done in a single function. The only reason I can think of for putting it in a class would be a base class called Collideable that a Sprite class derives from, but personally I think thats overkill.

Also I wouldn't get to hung up on the verb/noun thing. I'm hardly a vet in game programming but I have never seen or heard of anyone ever using that technique. Other may disagree.

q2 Yes these is. This is bounding box collision


bool CollisionTest(float x1, float y1, float w1, float h1, float x2, float y2, float w2, float h2)
{
float x1w1, x2w2;
float y1h1, y2h2;

x1w1 = x1 + w1;
x2w2 = x2 + w2;
y1h1 = y1 + h1;
y2h2 = y2 + h2;

if(x1w1 > x2 && y1h1 > y2 && x1 < x2w2 && y1 < y2h2)
{
return true; //there was a collision
}

return false;
}

Share this post


Link to post
Share on other sites
Alternatively, though it's quite much more work, you could always go the route of the distance formula. If you can calculate the distance at which these two would collide, then if would function in the exact same manner as bounding boxes, and it'll do you much good with circles, given that you know the appropiate radius. Caveat, it's a pain to use and implement for beginners and you would have to know the coordinates of an object's center. It's not too hard to find out, but by default, some libraries have their sprites orientated in the upper corner for x,y values. Plus, if you've got some slightly irregular sprites, using the distance formula makes it less awkward when programming actions based on collision than if you would bounding boxes, but bounding boxes usually solve 95% of everything I need from 2D gaming.

Share this post


Link to post
Share on other sites
ok i should have said I am making a 2D game so that is different to bounding box for 3D.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement