Tutorial :Actionscript collisions: solving exceptions and strange cases



Question:

I have created a collision class to detect collisions using pixels. In my class I've also developed some functions for determining the collsion angle. Based on this I created some examples:

http://megaswf.com/serve/25437/

http://megaswf.com/serve/25436/

(Space to change gravity, right/left to give some speed to the ball.)

As you will probably notice, there are strange things that happen:

  • When the ball speed is very low
  • When the direction of the ball is almost tangent to the obstacle.

collision http://img514.imageshack.us/img514/4059/colisao.png

The above image shows how I calculate the collision angle. I call the red dots the keypoints. I search for a maximum number of keypoints (normally 4). If I find more then 2 keypoints, I choose the 2 farthest ones (as shown in one of the blue objects). Thats how I then find the normal angle to where in the surface the object collided. I don't know if this is an obsolete way of doing things.

based on that angle, I rotate the speed vector to do the bouncing.

The piece of code to do the maths is here:

static public function newSpeedVector(speedX: Number, speedY: Number, normalAngle: Number): Object{              var vector_angle: Number = Math.atan2(speedY, speedX) * (180/Math.PI);              var rotating_angle: Number = (2*(normalAngle - vector_angle) + 180) % 360;              var cos_ang: Number = Math.cos(rotating_angle/DEGREES_OF_1RAD);              var sin_ang: Number = Math.sin(rotating_angle/DEGREES_OF_1RAD);              var final_speedX: Number = speedX * cos_ang - speedY * sin_ang;              var final_speedY: Number = speedX * sin_ang + speedY * cos_ang;                return {x_speed: final_speedX, y_speed: final_speedY};          }  

This is how the new speed vector is calculated...

My question is, has anyone faced this kind of problem or has some idea on how to avoid this from happening?


Solution:1

Without seeing your code, this is the best I can provide.

Collision physics should have some velocity threshold that is considered "stopped". That is, once the velocity gets small enough you should explicitly mark an object as stopped and exempt it from your collision code. This will help stabilize slow moving objects. Trial and error is required for a good threshold.

When a collision happens, it is important to at least attempt to correct any inter-penetration. This is most likely the reason why tangent collisions are behaving strangely. Attempt to move the colliding object away from what it hit in a reasonable manner.

Your method of determining the normal should work fine. Game physics is all about cheating (cheating in a way that still looks good). I take it rotation and friction isn't a part of what you're going for?

Try this. You have the contact normal. When you detect a collision, calculate the penetration depth and move your object along the normal so that is isn't penetrating anymore. You can use the two points you have already calculated to get one point of penetration, you'll need to calculate the other though. For circles it's easy (center point + radius in direction of normal). There are more complex ways of going about this but see if the simple method works well for you.

I have read and recommend this book: Game Physics Engine Development


Solution:2

I did something a little like that and got a similar problem. What I did is that when the pixels from the bouncing object (a ball in your case) overlap with the pixels from the obstacle, you need to move the ball out of the obstacle so that they are not overlapping.

Say the ball is rolling on the obstacle, before your render the ball again you need to move it back by the amount of 'overlap'. If you know at what angle the ball hit the obstable just move it out along that angle.

You also need to add some damping otherwise the ball will never stop. Take a (very) small value, if the ball velocity is bellow that value, set the velocity to 0.


Solution:3

You can try one more thing, which I think is very effective.

You can make functions such as getNextX() and getNextY()(Which of course give their position coordinated after the next update) in your game objects and Check collision on objects based on their next position instead of their current position.

This way, the objects will never overlap, you'll know when they are about collide and apply your after collision physics gracefully!


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »