17 Mayıs 2012 Perşembe

Collision detection challange

To contact us Click HERE
Me and the math are not friends, but now that I need some math equations to reach my scopes, finally I understand his importance. On last week I worked on some collision detection algorithms between circles and rectangles and after some reading of my high school books I found some solutions. Between rectangles simply I check the angle points: with a fast algorithms simply I compare 8 points (4 from the two rectangles). Between a circle and a rectangle the algorithms make a square that cover the entire circle and if the rectangle is inside this square, it checks if every pixel's border of the rectangle is on the circumference or inside the circle using the equation of the circle; I made this techniques to reduce the CPU use.

My dilemma now is that I want to check when two circles collides or how to write the algorithm that checks the collisions between rotated rectangles. On the engine that I'm working for Sonic The Hedgehog HD I need to rotate the sprites between 0° and 360° and I need also to work on rotated collisions but how? I tried to imagine 4 lines with the classic formula y=m*x+q, assign m converting the angle value with arctan but I can't realize how to write the code. But this isn't the only problem. If I decide to define a rectangle collision that won't rotate with the sprite (I'm assuming that the sprite is like a square and not a long/high rectangle) I can have other problems; the sprite can be rotated using any point of the space as center point but the collision will not rotate. Maybe some pictures can give more sense:
This is my sprite defined to the center of the space:
This is the same sprite with the same center, but with a rotation of 135°:The sprite can be rotated without problems and without to rotate also the collision.Now I take the same sprite but I move his center point:
But if I want to rotate the sprite by 135° from that point?The result is weird. The sprite is rotate correctly, but the collision is fixed to that point.Assuming to use my algorithm that checks the 4 angles from the first rect and the 4 angles from the second rect, if I rotate the collision, the detection will fail giving me wrong results. So I need to check the borders as a line defined from an equation, but how I can edit the equation if the lines will rotate using a specific point as center? And how I can check if a line cross another line when them has two limit points? I need to check pixel per pixel?  

Hiç yorum yok:

Yorum Gönder