I was never good at math. Can someone help me with this?
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.
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?
[Please log in to post a comment]