Jump to content
• Advertisement

Asteroid collisions

This topic is 3899 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi I'm trying to make a Asteroids-style game. I've just getting to the collision response part of it. What I would like to do is:
1. Calculate the kinetic energy for each asteroid
2. Pass that to a function on the other asteroid to calculate what happens (asteroid splits if there's enough energy) and returns the remaining energy
3. The physics engine calculates the changes to the velocity.
I can't seem to find anything on Google that quite matches what I'm doing. I have the kinetic energy in Joules, but I'm unsure how to convert that to a force. Am I going about this all wrong? Thanks & Regards elFarto

Share this post

Share on other sites
Advertisement
You never use force, you use acceleration. (And if you have elasticity, you might use impulse, or shock.)

Share this post

Share on other sites
You want to involve momentum.

m*v=f*t=P(momentum)

where
m is the mass
v is the velocity
f is the average force applied to each object during the collision.
t is the amount of time the collision takes.

In a system, in your case the two asteroids colliding, momentum is the same before and after the collision.

m1v1+m2v2=m1v1'+m2v2'

The axis you want to do the math for in the collision is a line that goes through the center of both asteroids.

So you are need to find the momentum each asteroid has in that direction.

The easy way to do this is create a unit vector that is at one asteroid and points to the other.

dist = sqrt((x1-x2)^2+(y1-y2)^2)
Ux=(x2-x1)/dist
Uy=(y2-y1)/dist

so the momentum for each asteroid is defined like this.
P = m(Ux*Vx+Uy*Vy)
where m is the mass of the asteroid and Vx, Vy is the x and y velocity.

So now we have the momentum for each asteroid. In the end we want the final velocities for each. What seems to work is to switch the momentum for each asteroid or in mathwords.
P1=P2
P2=P1
Not sure why this works, it just does for perfectly elastic collisions.

Now we have the new momentums for the asteroids. Now we work backwards to find the velocity.
V=P/m

Then split it back up into x and y components.
Vx=V*Ux
Vt=V*Ut

Not sure if I was clear on all of that. If you have any other questions feel free to ask, and just because I am feeling generous today. I am going to post code to go along with it. Not sure what language you are using but I will go ahead and code it up in C++.

void applyCollision(CAsteroid *a1, CAsteroid *a2){     double ux = a2->GetX()-a1->GetX();     double uy = a2->GetY()-a1->GetY();     double dist = sqrt(ux*ux+uy*uy);     ux /= dist;     uy /= dist;     double p1 = a2->GetMass()*(ux*a2->GetVX()+uy*a2->GetVY()); //The p1, p2 switch happens here     double p2 = a1->GetMass()*(ux*a1->GetVX()+uy*al->GetVY());     double v1 = p1/a1->GetMass();     double v2 = p2/a2->GetMass();     a1->SetVelocity(v1*ux, v1*uy);     a2->SetVelocity(v2*ux, v2*uy);}

I think that should work, didn't test it. If you have any other questions feel free to ask.

Share this post

Share on other sites
Thank you very much HappyCoder.

Is there no way of using kinetic energy in these calculations? As I would like to have that as a form of damage in my game.

Thanks & Regards
elFarto

Share this post

Share on other sites
Well the kinetic energy before and after a collision for any given asteroid is going to be different. So maybe you could use the difference in kinetic energy to determine how much damage it took during a collision. Of course this fails to take into account the change in direction the asteroid will experience. So a collision that takes an asteroid with a velocity in one direction and sends it back with the same speed in the other direction, that will result in the same energy.

Maybe you could use momentum in this because it involves direction. You could calculate the change in momentum and then using

m*v=f*t

you can set some constant for t for the collision time (unrealistic but it is easy and a realistic approach wouldn't make much of a noticeable difference)
Then you take the change in momentum for the given asteroid and divide by the time to get the amount of force on the object.

m*v/t=f

Then use force to calculate damage.

I just realized that switching the momentums for each asteroid will only be realistic for asteroids of the same mass. Let me figure out how to take into account objects of different masses.

Share this post

Share on other sites
Okay so I looked up the wikipedia article
Elastic Collision
And there is this useful equation.

so applying this to the code.

void applyCollision(CAsteroid *a1, CAsteroid *a2){     double ux = a2->GetX()-a1->GetX();     double uy = a2->GetY()-a1->GetY();     double dist = sqrt(ux*ux+uy*uy);     ux /= dist;     uy /= dist;     double u1 = ux*a1.GetVX() + uy*a1.GetVY();     double u2 = ux*a2.GetVX() + uy*a2.GetVY();     double v1 = (u1*(a1.GetMass()-a2.GetMass())+2*a2.GetMass()*u2)/(a1.GetMass()+a2.GetMass());     double v1 = (u2*(a2.GetMass()-a1.GetMass())+2*a1.GetMass()*u1)/(a1.GetMass()+a2.GetMass());     a1->SetVelocity(v1*ux, v1*uy);     a2->SetVelocity(v2*ux, v2*uy);}

That should work a lot better.

Share this post

Share on other sites
Quote:
 Original post by elFartoThank you very much HappyCoder.Is there no way of using kinetic energy in these calculations? As I would like to have that as a form of damage in my game.

You might encounter a problem called material hardeness. Pure E = (1/2)*m*v^2 isn't enough (in some situations).

In hypervelocity collisions the projectile explodes anyway, so you might apply a termal shock.

Share this post

Share on other sites
Thank you very much for your help.

Regards
elFarto

Share this post

Share on other sites

• Advertisement
• Advertisement

• Popular Contributors

1. 1
Rutin
46
2. 2
3. 3
4. 4
5. 5
JoeJ
19
• Advertisement

• 11
• 13
• 9
• 10
• 12
• Forum Statistics

• Total Topics
633003
• Total Posts
3009824
• Who's Online (See full list)

There are no registered users currently online

×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!