Bouncing around the screen

Moderator
Posts: 508
Joined: 2002.09
Post: #1
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.

"When you dream, there are no rules..."
Quote this message in a reply
Moderator
Posts: 1,560
Joined: 2003.10
Post: #2
You can compute orientation from a direction vector using atan2().
Quote this message in a reply
Moderator
Posts: 508
Joined: 2002.09
Post: #3
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?

So:

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

correct?

"When you dream, there are no rules..."
Quote this message in a reply
Member
Posts: 89
Joined: 2005.06
Post: #4
Or just use Chipmunk Grin

Well since everyone else is doing it:
Twilight Coders
CIMyAdmin
Whatever
Quote this message in a reply
⌘-R in Chief
Posts: 1,247
Joined: 2002.05
Post: #5
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.
Quote this message in a reply
Member
Posts: 65
Joined: 2009.01
Post: #6
ThemsAllTook Wrote:You can compute orientation from a direction vector using atan2().
There is a classic article explaining the theory behind this:
http://www.red3d.com/cwr/papers/1999/gdc99steer.html

Check out the part where he discusses "velocity alignment", I think its on page 7 of the original paper.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Ball Bouncing in a circle strikerjax 6 4,948 Jul 6, 2009 02:39 PM
Last Post: strikerjax