iDevGames Forums
Bouncing around the screen - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Game Programming Fundamentals (/forum-7.html)
+--- Thread: Bouncing around the screen (/thread-1098.html)

Bouncing around the screen - Taxxodium - Jun 23, 2009 01:23 PM

Just a quick question. I'm writing a game where a bunch of enemies spawn every x seconds. Each enemy has a random location, random speed and ideally random orientation.

Now I've been strugling with how to do the bouncing when an enemy collides with the border of the screen. I know how to detect the collision but not how to respond to it.

The easy way to do this is to have a direction property which is a vector and just multiply it's x and y values by -1 depending on the side the collision occurs. However, using this method the enemy will always have the same orientation, and thus if it went to the lower left, it won't rotate to face the upper left.

So instead of having a vector, I think it's best to have an orientation property which is just a float describing the angle the enemy is facing. But how do you know the new orientation if the enemy collides with the left edge of the screen?
You can't just multiply by -1 or turn by 180 degrees.

Thanks in advance.

Bouncing around the screen - ThemsAllTook - Jun 23, 2009 01:32 PM

You can compute orientation from a direction vector using atan2().

Bouncing around the screen - Taxxodium - Jun 23, 2009 01:43 PM

so you recommend I use a direction vector instead of an orientation value? If I'm not mistaken direction will have the values 1 or -1, correct?


position.x = direction.x*speed;
position.y = direction.y*speed;


Bouncing around the screen - Volte - Jun 23, 2009 05:26 PM

Or just use Chipmunk Grin

Bouncing around the screen - SethWillits - Jun 23, 2009 05:31 PM

No. As a unit vector, the components of the direction have the complete range of [0, 1], and the magnitude is always 1. Speed would be represented separately. Or, represent speed in the vector by not using a unit vector. In that case, the magnitude would be [0, max_speed]. The important thing is that to move at any arbitrary angle, you need to be able to represent any arbitrary angle. -1, 0, or 1 doesn't do that; you'd only get 8 angles.

When your enemy collides with the world bounds, the simplest thing to do is just negate the correct component. If it hits the top or bottom of the bounds, negate the y component. Left or right, negate the x component.

For example, if (0, 0) is the bottom left, then if the vector is (3, 8) the enemy is traveling up and to the right. If it hits the top, the new vector is (3, -8). If it then hits the right it becomes (-3, -8). It'll hit the bottom and go to (-3, 8). After the top (-3, -8). Left (3, 8)...

If you want your enemy to be rotated to point exactly the direction it's traveling then you'd do what Alex said, and use atan2 to compute the angle you need to use for the rotation from the direction vector.

Bouncing around the screen - johncmurphy - Jun 25, 2009 02:10 PM

ThemsAllTook Wrote:You can compute orientation from a direction vector using atan2().
There is a classic article explaining the theory behind this:

Check out the part where he discusses "velocity alignment", I think its on page 7 of the original paper.