- Home /
Calculating vectors for two colliding objects
In a game I'm building I have two gameObjects, balls. They are sprites, with spriterender, rigidbody2d's and collider2d's. There are two players who can "throw" the balls by pressing a button. Players try to "kill" the other ball by hitting them hard with their own ball.
Currently, I'm doing something like this:
if(lastVelocity>5) {
coll.gameObject.GetComponent(Player).doDamage(lastVelocity);
}
So if the ball's velocity.magnitude before the collision was greater than 5, that does damage to the other ball. Works nicely for some cases and I can actually play around and fine tune other aspects of the game.
HOWEVER, big issue is that this doesn't take into account the vector of the balls. So let's say that the other ball is flying directly up with magnitude 8 and i hit it from below (about the same direction) with the magnitude of 10. The other ball gets damage of 10, but my ball gets damage of 8, because that was the velocity before impact. The other ball didn't have velocity towards my ball, but the opposite, so it shouldn't damage be much or at all.
How I'd like it to be is that the vector is taken into account as a multiplier, or similar. The more towards the other ball my ball is going, the higher the damage is, and vice versa.
So, if i know the position, heading, velocity,etc of these two balls, how can I calculate "how much towards" the other ball is my ball going, so I could adjust the damage properly?
Answer by robertbu · Oct 20, 2014 at 05:14 PM
The Collision2D pass in the OnCollisionEnter2D() has a relativeVelocity:
http://docs.unity3d.com/ScriptReference/Collision2D-relativeVelocity.html
Thanks, but that is the relative velocity of the two objects, i.e. it's the same for both colliding objects. I'd need to have one number for ball1 and another for ball2, depending on their vectors..
Why do you need one number for each ball? The magnitude of relativeVelocity gives you a measure of how hard the two balls hit. Say the two balls were chasing each other and one was traveling at a speed of 999.999 and the other was traveling at a speed of 1000, the impact is going to be $$anonymous$$or for both objects. If the mass is different, then you may want to dig deeper.
"The more towards the other ball my ball is going, the higher the damage is, and vice versa."
If this is really how you want to handle it, Vector3.Dot() can calculate a value for you:
http://docs.unity3d.com/ScriptReference/Vector3.Dot.html
http://www.mathsisfun.com/algebra/vectors-dot-product.html
If you normalize the two velocity vectors passed (not necessarily what you want to do), then a value of -1 means they are going in opposite directions. A value of 1 means they are going in the same direction, and a value of 0 means they are moving at right angles with respect to each other.
Again not necessarily what you want, but Vector3.Angle() of the two velocity going into the collision will give you an unsigned angle that gives you a value you can use to see the relationship between the two objdcts.
Your answer
Follow this Question
Related Questions
Create a collider for area created by mouse 0 Answers
c# Destroy upon collision isn't working 2 Answers
Tilemap Collision Issues 0 Answers
Ignore collision in particular situation 3 Answers
Non - Physics collision System 0 Answers