Lil Help

Started by
0 comments, last by Ys 22 years, 5 months ago
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
Im not fat just big boned :)
Advertisement
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; 

This topic is closed to new replies.

Advertisement