Sunday 15 May 2011

geometry - How to get which edge of two rectangles are colliding -



geometry - How to get which edge of two rectangles are colliding -

i have working physics system. when 2 objects collide, each have it's oncollide function called may react collision.

my issue comes handling collisions both x , y. know border of rectangle can collide 1 border of other rectangle. however, don't know math determine border is.

so how tell rectangle 1 border rectangle 2 collided knowing x,y,height,and width of each rectangle?

(i'm working in c# way)

after bit of trial , error, able find solution.

first, check axis collision happening "the most" on. 1 time you've determined that, compare values on axis see direction it's happening. next psudocode should nicely when translated language of choice.

if absolutevalue(box1.y - box2.y) > absolutevalue(box1.x - box2.x) if box1.y > box2.y box 1 collding top of box 2 else box 1 colliding bottom of box 2 else if box1.x < box2.x box 1 collding left of box 2 else box 1 collding right of box 2

this doesn't take boxes height , width account, don't think should problem in cases. though may run issues dealing rectangles largely different sizes in axis.

geometry computational-geometry

No comments:

Post a Comment