Sign in to follow this  

Collision detection

This topic is 4860 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

I am trying to make a 2d side scroller but the graphics are such that the tiles used for blocking some are complete 32 * 32 and the graphics occupy the whole tile are but some occupy just on half or more but not complete. How do I check collision dectection. Please help

Share this post


Link to post
Share on other sites
I'm not really in the place to put my thoughts down, but this should get you what you need. It's often just a matter of knowing the terminology.

Searching these forums for the same (and just collision detection) will get you plenty of detailed responses.

Also, this may come in handy, if you're doing any bullet ricochets etc.

Lastly, the articles on this site, specifically the Collision Detection ones should help you out.

Share this post


Link to post
Share on other sites
Nice and easy if you're dealing with rectangles. Test each corner point of one rectangle against the destination.

Here's the basic collision detection code from jsInvaders. It only reports if the two collision boxes overlap, further tests such as per pixel tests can be done after this to fine tune the collision.



// Simple test to see if collision box of WeaponFire (wf) intersects with collision box of alien

// bl = bottom left, tl = top left, tr = top right, tl = top left


vMin.x = alien->collisionbox.bl.x;
vMin.y = alien->collisionbox.bl.y;

vMax.x = alien->collisionbox.tr.x;
vMax.y = alien->collisionbox.tr.y;

//compare collisions
if (( wf->collisionbox.tr.x >= vMin.x ) && ( wf->collisionbox.tr.x <= vMax.x ) )
{
// matches in the x
if (( wf->collisionbox.br.y >= vMin.y ) && ( wf->collisionbox.br.y <= vMax.y ) )
{
/// A COLLISION HAS HAPPENED

}
}


Share this post


Link to post
Share on other sites

This topic is 4860 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.

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