# Thread: 2D collision handling between two different moving objects

1. Member
Join Date
Feb 2011
Posts
9
Rep Power
0

## 2D collision handling between two different moving objects

Hi,

I'm making a 2d top-down space strategy game with a variety of space ships in Java. Up till now I've simply had collision detection between ships and weaponry, edges of the screen etc, but now I want to try and go a step up and cause ships to damage and rebound in the appropriate directions when colliding with one another. Actually detecting the collision is not too difficult but I'm struggling with what to do after that.

How can I work out from the ships Velocities, Mass and angle of direction what the effect on them should be?

2. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
9
Presumably you already know the velocities of each object, else you can't paint them in the first place.

Collision detection of moving objects (even if 1 is stationary) can be difficult if the velocities are high enough, because as you update positions, it's possible for a fast object to logically pass through another object that point-by-point collision detection will not discover. This can happen because positions are updated in discrete jumps to correspond with the frame rate. To handle that, you're in the realm of solving linear differential equations. For objects moving in straight lines at constant velocity, the collision check is pretty straightforward, but if you don't have a good math background, you're going to need to partner with someone who does, or use a physics engine, or limit velocities such that this scenario is impossible.

At any rate, if two nondeforming point-objects collide, their new velocities are determined based on their respective velocities and masses according to the law of conservation of momentum, which states that m1*v1+m2*v2 = a constant, combined with Newton's 3rd law which states that the the sum of the forces each object experiences in each dimension will be 0. If your eyes haven't glazed over let me know.

3. Member
Join Date
Feb 2011
Posts
9
Rep Power
0
yeah... unfortunately I only understood about half of what you just said. :( I don't think the velocities should be so high that objects can pass through each other without recognising a collision, though I am working on changing the way I look at ship to ship collision detection as currently it waits till a ship has moved and then checks if it has moved into the same position as another ship, but I instead want to check the position it is going to move to BEFORE allowing the move, if that makes sense.

I've also yet to assign masses to any of my ship types as I am still trying to understand everything. Could you perhaps explain a bit more?

Thanks so far

4. Senior Member
Join Date
Jan 2009
Posts
671
Rep Power
9
Have you had a basic physics class, and do you have a good grasp of trig?

5. Member
Join Date
Feb 2011
Posts
9
Rep Power
0
I can handle the trig ok but I'v never done any physics, which is where I'm falling down

6. Member
Join Date
Apr 2011
Location
Bengaluru
Posts
4
Rep Power
0
I can help u.. If u r ready to accept objects as Non-Elastic....
i.e., U should assume that your space ships r not undergoing any deformation in
their shape. If their shapes change.. Then law m1*v1=m2*v2 fails.
Because the momentum of both ships is lost due absorption of energy in changing the shape of space ship.

7. Member
Join Date
Feb 2011
Posts
9
Rep Power
0
hmm well I'm fairly certain I want it to be non-elastic. The idea is that the ships will take some damage based on speed and weight, e.g. if a small ship goes flying into a larger one, the small ship might ricochet a lot and taken a lot of damage while the larger one is affected a lot less.

8. Member
Join Date
Apr 2011
Location
Bengaluru
Posts
4
Rep Power
0
I hope u still have idea of having Elastic collision...
It will be difficult to analyse it precisely.. So u can adopt following method

1. Assume elastic collision
2. Find the final change in direction & velocity of space ships
3. Since u want to assume damage to space ship, reduce the final velocity of
both ships by some % ( Say 3-5 % ).
* Do not change the direction of ships

Still if u do not know to calculate change in velocity & direction of ships after collision as Non-Elastic,i well tell u how to do that

9. Member
Join Date
Feb 2011
Posts
9
Rep Power
0
I'm happy to still do elastic collision and just estimate some damage afterwards, but its working out the change in velocity and direction that I need the help with.

10. Member
Join Date
Apr 2011
Location
Bengaluru
Posts
4
Rep Power
0

## Scanned copies of problem

Sorry for taking such a long time..
Though i completed these very early..
I could not upload these because i was busy in my work.

11. Member
Join Date
Apr 2011
Location
Bengaluru
Posts
4
Rep Power
0

## Re: 2D collision handling between two different moving objects

I see lot of views for this thread. Has any body observed my calculations. Give feed back.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•