Log In  

I was never good at math. Can someone help me with this?

P#13931 2015-09-09 16:42 ( Edited 2015-09-11 13:57)

2

Given two entities {a,b} with the following properties:

{x=(top left x),y=(top left y),width=(px width),height=(px height)}

You can treat them like rectangles. The simplest test is checking if the rectangles do not collide. This happens in the following conditions:

(a.x > b.x+b.width) -- rectangle a's left side is past rectangle b's right
or
(a.x+a.width < b.x) -- rectangle a's right side is past rectangle b's left
or
(a.y > b.y+b.height) -- rectangle a's top side is past rectangle b's bottom
or
(a.y+a.height < b.y) -- rectangle a's bottom side is past rectangle b's top

You can simply negate this with "not" to find if the rectangles intersect. You can negate it mathematically to save a grand total of one token, but it's easier to explain this way.

P#13934 2015-09-09 16:53 ( Edited 2015-09-09 20:58)

I see, thank you. As objects that I'm going to check collisions for are always on same Y position, I assume it's safe to omit last two checks, right?

P#13938 2015-09-09 17:12 ( Edited 2015-09-09 21:12)

Yes, just check the x dimension. (first two cases).

P#13940 2015-09-09 17:28 ( Edited 2015-09-09 21:28)

Thank you.

P#14010 2015-09-11 09:57 ( Edited 2015-09-11 13:57)

[Please log in to post a comment]