- Home /
Add a wind vector to local velocity for calculating wind speed
How can I add a wind vector to the velocity of a rigid body (in my case a helicopter) for reading the windspeed for the x-z axis?
My calculation below is working in cases where the wind is blowing directly to the x or z axis (0° or 90° angle) of the helicopter but when the wind is blowing for example in a 45° angle to the helicopter, the wind is sometimes 0 m/s.
// this is the wind direction in world space which changes
Vector3 wind=new Vector3 (-7.0f,0.0f,0.0f);
// here I add the local velocity in world space to the wind in world space
Vector3 _WindPlusVelocity = wind-rigidbody.velocity;
// here I translate the WindPlusVelocity to the local space of the helicopter
Vector3 _RelativeWindVelocity = _transform.InverseTransformDirection(_WindPlusVelocity);
// when the heli flies with the wind, the VelZ is wind plus local velocity Z.
// when he flies in the opposite direction, the VelZ is velocity Z minus wind.
string Test1="_velY: "+(_RelativeWindVelocity.y).ToString("f1");
string Test2="_velXZ: "+(Mathf.Abs(_RelativeWindVelocity.x+ _RelativeWindVelocity.z)).ToString("f1");
What I'm doing wrong?
maybeyoushouldtrytoformatthecodeitissomucheasiertoreadthen.
Please excuse me for that. Now I formatted it as code.
Answer by Klaus-Eiperle · Jun 04, 2014 at 02:06 PM
This is the solution:
float _vwx = Mathf.Abs(rigidbody.velocity.x-wind.x);
float _vwz = Mathf.Abs(rigidbody.velocity.z-wind.z);
string Test2="VelXZwind: "+ (Mathf.Sqrt(_vwx*_vwx+_vwz*_vwz)).ToString("f1");
You realise that unless you have velocity on y, that is just a much more complex way to write what I wrote...? If you do have velocity in y you can do:
Vector3 vw = rigidbody.velocity - wind;
vw.y = 0;
float v = vw.magnitude;
$$anonymous$$athf.Sqrt(_vwx*_vwx+_vwz*_vwz) is what magnitude is. You also don't need to bother with the Abs because when you square them you negate that anyway. (ie: -1 x -1 = 1).
Also, if you do have velocity in y, note that freefalling vertically will give you a speed readout of 0 (possibly intentional)
Answer by Tarlius · Jun 04, 2014 at 08:48 AM
I think what you want to do is something more like...
relativeVelocity = (selfVelocity - windSpeedVector).magnitude;
I'm not sure exactly what you are trying to calculate so not sure where the minus sign should be, but that should get you started.
Also, I think RigidBody.velocity is already in world space so you shouldn't need a conversion, but I could be wrong.
If you are trying to simply make the wind move the helicopter, you could (and probably should) just do it with RigidBody.AddForce, by the way.
If its for a UI component, (wind speed relative to helicopters speed, in km/h) you probably want to add the two together
Thank you very much for your hint.
For calculating some helicopter physics I need two different speed values. One for the y axis (steering direction up and down) and one for the yz axis which describes the sideways movement.
I'm calculating XZ speed like this: absRelativeVelocityXZ = $$anonymous$$athf.Sqrt(absRelativeVelocity.x*absRelativeVelocity.x+absRelativeVelocity.z*absRelativeVelocity.z);
When I calculate the real speed (own speed plus wind speed) by using the magnitude, I don't have any access to the real speed for the local XZ axis of the helicopter.
Yes, RigidBody.velocity is in world space, I simply need to convert it to local space when I like to read out a single axis.
On a side note, you should not do physics in anything but world space. The result get... odd.