# Bouncing away from object on collision?

• 03-03-2007, 03:42 AM
Hosma293
Bouncing away from object on collision?
Hi there guys,

I've spent the last 12 hrs on this issue and its starting to tick me off. I have a Space craft that is moved via the standard sine/cosine formula (I.e. xSpeed = Math.sin(craft._rotation*Math.Pi/180) etc..), I also have a group of Asteroids floating around in space. Basically, I need the ship to get bounced away from the Asteroids if they collide with each other.

E.g If the Asteroid's movement angle is 45 degrees and the craft's movement angle is 225 degrees, if they collide, I'd need the ship to be blasted back at an angle of 45 degrees. With me? I hope so :P

Thanks for any help :)
Hosma
• 03-03-2007, 04:00 AM
691175002
You are probbaly looking for "billiard ball" collisions. I suggest you just try to fake it as much as possible, because the math behind realistic circle to circle collisions is really complex.

Just by skimming, this apears to be close to what you need:
http://archive.ncsa.uiuc.edu/Classes...send/math.html

There is a chapter on billiard ball collisions in Flash Game Design Demistified.
Part of it is on google books (the page you are looking for is 136):
But the entire chapter is not included so aside from an image and a table of contence you get nothing that will help you.

To fake it, I would personally just use a verlet integration for the moving objects, and set up a 100% strength spring to push the ship and asteroid apart when they get close. Its still somewhat complex, but the math is manageable.
• 03-03-2007, 07:52 PM
Hosma293
Yes its Billiard Ball collisions that I'm after, I knew the name was something like that. :P

When you say complex, do you mean to the level that its actually hard to read? When I was trying to get it to work, I came up with the idea of just setting the crafts velocity based on whether it had its back to the asteroid or not. The fomula that I came up with needed a full 360 degrees angle variable (not flashes _rotation as that only has values up to 180, then goes into negatives) sop I wrote a function that converted the _rotation value to a true angle.
The result of this formula was that if the ship was facing the asteroid when it collided (a threshhold of 180 degrees was used), it would bounce back like it was supposed to, however if the ship had its back to the rock, it would be sucked through the asteroid and sent flying backwards. :P

I eventually figured out the reason for this was because I had forgotten that I was setting the x and y speed values for the craft (which are of course, negative inclusive variables), and not one speed value that only included positive numbers.

Anyways, a friend has that book I think so I'll refer to it sometime soon.
• 03-03-2007, 08:33 PM
691175002
Well the book essentially has the same formulas and diagrams as the first link in my previous post, its just a little more readable. If you can understand it (or just use it) you should be able to have realistic collisions.
• 03-10-2007, 01:15 AM
Hosma293
Sorry I've taken so long to write back, I've been busy with game development. I have since read the chapters in the book and have successfully transfered the functions over for use in my game! I have to say, those collisions are more realistic then anything I could have hoped to achieve on my own. :P

Thank you for your help! :)
Hosma