Sunday 27 November 2011

Collision detection

The collision detection was really hard on me. It was the toughest part of programing yet. First I had to make an alternative to the dark GDK integrated function because it was quite inaccurate. So what I did was, I had a loop return the X and Y coordinates of all the particles that were shot out from the center, and then if any of those coordinates were located in the 'ring' where the pad is moving AND have the same rotation as the pad, the particle would get deleted.
This is the ring area that I am talking about (marked in purple):

I achieved this by using the good old circle equation: 
x2 + y2 = radius2
Example

I simply inserted the X and Y coordinates of the particles into the function, switched the '=' with a > and for the radius I used the inner edge of the ring.
what you get is this. The next step was to limit the area from the outside, so I added the same equation, enlarged the size of the radius(by the width of the pad) and switch the '>' with a '<'. and I got me a lovely ring. The third condition requires the particle sprite to have the same angle as the pad and the whole thing works!

Well... it almost works. The real problem arises here. Spinning the pad and the particles around reqires the use of the dbWrapValue(Value) (a Dark GDK function), wich wraps the value of the angle (it doesn't let a value go out of the bounds of 0°-359° for example 359°+10°=9° instead of 369°). This caused a problem at the top of the game area (where the angle is 0°) If the pad was on the left side of the 0° mark and the particle was on the right like this:

The pad value was something like 355° and the particle value was for example 3°. Those values are far apart, so I had to make an exeption and make a separate collision function for values that revolved around the 0° mark where the values get wraped. It works perfectly now.

No comments:

Post a Comment