Archived

This topic is now archived and is closed to further replies.

Lil Help

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

Can someone please help me on this code for collision detection im trying to get to work cus ive been trying for ages and when i try to use it, it works ok for the y co-ords but it says there is a collision anywhere on the along the x axis so an object will say its been hit when its at the right hieght but not if the x co-ords are the same please help. o yeh Obj is a structure that holds the x and y co-ords of an object. int DetectCollisions( Obj quad1, Obj quad2 ) { float quadWidth = 0.5f; float quadHeight = 0.5f; if ( quad1.y <= (quad2.y + quadHeight) && quad1.x <(quad2.x + quadWidth) && (quad1.x + quadWidth) > quad2.x ) { // collision occured return 1; } return 0; } thanks for any help sorry about the indentation and layout of the code. Chris

Share this post


Link to post
Share on other sites
quote:
Original post by Ys
if ( quad1.y <= (quad2.y + quadHeight) && quad1.x <(quad2.x + quadWidth) && (quad1.x + quadWidth) > quad2.x )

Pause. Take a deep breath (maybe even with your eyes closed). Now open your eyes and take a look at this line, and realize why it fails...

if( (quad1.y - quad2.y) <= quadHeight && // wont work if quad2 is lower
((quad1.x - quad2.x) <= quadWidth || // you used &&. Not what you wanted
(quad2.x - quad1.x) <= quadWidth))
return 1;

Share this post


Link to post
Share on other sites