- Home /
SImply yet tricky question about RELATIVE VELOCITIES...
Here's a straightforward question for Unity EXPERTS.
Say you have two rigidbodys a and b and they have velocities. (Obviously a.velocity and b.velocity )
What is the usual idiom in Unity, to calculate the velocity of b, as seen from a ??
I assume the answer is simply:
var speedOfBSeenFromA:Vector3;
speedOfBSeenFromA = b.velocity - a.velocity;
Is this correct?? Can an expert please confirm this? Maybe there's something I don't understand about Unity?
Once again, of course I know how to add "actual" vectors in Newtonian space, as in high school physics class. My question is specifically: what is the usual Unity idiom? Thanks!
To be clear, by "relative velocity" I simply mean as any high-school physics teacher would think of it! For example: imagine B is heading "straight up" at 10 m/s, and A is heading "straight down" at 2 m/s. In this case, essentially 10 - (-2) = +12, which seems correct.
Note: it seems somewhat odd there is not a function "relative velocity" since it's so common, maybe I'm missing something? (I realise it's just one minus the other - maybe that's the reason they didn't include such a function?)
Note: there is the somewhat confusing function GetRelativePointVelocity which appears to give the velocity of something passing seem from a strictly stationary point. (Assuming I understand that function correctly.)
So, I appreciate a seasoned expert letting me know if I'm correct in that the answer is very simply b.velocity-a.bvelocity. thanks!
PS no need to calculate Einsteinien time dilation for velocities approaching c !! :)
You need no unity expert to confirm this. There is a very short and concise Wikipedia article in this exact subject:
Hi @Christian ! I truly thank you for your input, just to be clear, I completely understand vector mathematics and even a little physics! (I think I even majored in these things at University - but I was pretty drunk at the time! :) ) $$anonymous$$y question is, in fact is that "how it is done" in Unity idiom (considering all the usual problems that can arise), and indeed secondly, is there a specific function, and indeed thirdly is my understanding correct re the little-mentioned GetRelativePointVelocity function. Cheers!
There isn't an inbuilt function for this, as I'm sure you know from reading the documentation. I'm a little mystified as to its absence, but it's not too hard to write a C# extension method for adding the functionality in. Do you want me to post something, or is that enough for you?
EDIT: I did it anyway, just out of curiosity.
Answer by syclamoth · Nov 02, 2011 at 10:37 AM
Here's something for C# users- it adds methods to the Rigidbody class for comparing velocity with another rigidbody, as well as a variant for comparing velocity at a point (e.g, for collisions). I do like extension methods.
public static class Utility {
public static Vector3 GetRelativeVelocity(this Rigidbody body, Rigidbody other)
{
return (body.velocity - other.velocity);
}
public static Vector3 GetRelativeVelocityAtPoint(this Rigidbody body, Rigidbody other, Vector3 point)
{
return (body.GetPointVelocity(point) - other.GetPointVelocity(point));
}
}
Hi @Sycla! To confirm, and to be clear, as far as you know (a) there's no built-in function and (b) in fact the usual idiom amongst developers is indeed just subtracting the two vectors - there's no "gotchya" associated with that? (For example, no bizarre gimbal lock problems, to pull an example out of the air, or whatever else specific to Unity - who knows. I assume not, but I'm just asking.)
Well, you can call it what you want... and no, there's nothing weird that can happen with this calculation.
Your answer
![](https://koobas.hobune.stream/wayback/20220613030713im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Move Player Car in Forward Direction using Physics Force 1 Answer
Why is the relativeVelocity of a Collision always zero in OnCollisionEnter and OnCollisionStay? 0 Answers
Velocity Relative to Rotation 1 Answer
Adding multiple "global velocities" to an object 1 Answer
2d side scrolling problem 5 Answers