Skip to main content
GameDev.net gamedev.net
🔒 Locked

Checking direction against a vector?

Started by AndyGil Mar 29, 2011 at 9:51 AM 11 replies 1.8k views
Original Post
AndyGil
AndyGil
Hi, im getting myself very very confused about how i can achive this.

basically im trying create an effect of "wind", so what i simply do is set a global vector (0,0,5) which gives me a wind speed of 5 in Z direction.

But, what i want to do know is to check if my object is moving "into" wind or away from the wind.

how can i do this?

Thanks
Andy
japro
japro
Use a dot product:


if(dot(wind, movement)>0)
moves into same direction
else
moves into opposite direction


Actually "=0" means movement orthogonal to wind direction, so you might want to handle that separately.
Pomnico
Pomnico
If you will use normalized vectors for object and wind direction, the dot product will give you cosine of angle between those two vectors (which is 1 for angle equal 0 degrees, 0 in case 90 degrees, -1 in case 180 degrees).
AndyGil
AndyGil
Hi Thanks for the replies.

japro:
That does indeed tell me if im heading into the vector or away which is what i wanted. And what im trying to do is get a "value" from how MUCH im heading into wind. So if im moving slightly into move but mostly perpendicular to it then the value would be little, but if i turn more into wind the value increases untill absolutly into wind.

Pomnico:
I have tried doingt his method by trying to convert to angles and checking against them but i seem to always run into problems. Using just cos as you said seems to return me strange angles? IF i use arc coc i get the correct angles but only for half of the angle, i presume this errors when it goes -0 to -180?

Thanks
Andy
Pomnico
Pomnico
I'm not sure if I understood you correctly.
If you have 2 vectors, for example a=(2,0) and b=(0,5), you can normalize them to receive:
a=(1,0), b=(0,1)
Dot product of those vectors is 0 = cos between a and b.
If you use arc cos from this value, you will receive proper angle, but it is up to you to decide if it should be positive angle or negative angle, as cos(45) = cos(-45), cos(90) = cos(-90), etc.
Please also remember, that angle 180 degrees is generally the same as angle -180 degrees, 270 degrees = -90 degrees and so on.
Brother Bob
Brother Bob

japro:
That does indeed tell me if im heading into the vector or away which is what i wanted. And what im trying to do is get a "value" from how MUCH im heading into wind. So if im moving slightly into move but mostly perpendicular to it then the value would be little, but if i turn more into wind the value increases untill absolutly into wind.

The dot product tells you exactly that. The dot product between A and B tells you how much A and B point in the same direction, or how much or A point in the direction of B (or the other way around). A small value means almost perpendicular to the wind, zero means exactly perpendicular to the wind, large positive value means along the wind, and a large negative value means against the wind.

The scale of the numbers depend on the magnitude of the vectors. So if you normalize then, you have a normalized dot product as well: 0=perpendicular, 1=along, -1=against, and anything in between is, well, anything in between the directions.
japro
japro

japro:
That does indeed tell me if im heading into the vector or away which is what i wanted. And what im trying to do is get a "value" from how MUCH im heading into wind. So if im moving slightly into move but mostly perpendicular to it then the value would be little, but if i turn more into wind the value increases untill absolutly into wind.


then just use the "normalized" dot product without converting into an angle.

x = dot(wind, movement)/(abs(wind)*abs(movement))

then if x = 1 the movement is parallel to the wind, x = -1 means it's in the exact opposite direction 0 means perpendicular etc. the values inbetween are "linear" in the parallel component if you will.

Also maybe what you want is only normalizing the wind direction:

vparallel = dot(wind, movement)/(abs(wind))

this gives you the projection of the movement speed along the wind direction.
Maybe you could explain what you need this value for. If you are trying to implement some sort of air drag then you probably don't even need to explicitly computate such a value.
haegarr
haegarr
In short:
a . b = |a| * |b| * cos(<a,b>)
AndyGil
AndyGil
Ok, thanks for the help all ,seems (from my initial tests) to be working now by simply finding the dot product of the 2 normalized vectors.

What im trying to do:
Basically i have implimented "wind" into my flight sim as a force and it works perfectly fine, but as "velocity" is a function to the lift force, "wind flow speed" also needs to be taken into account.

So when a wing is moving INTO wind, the velocity speed FOR LIFT EQUATION is that of the plane movemement and the extra wind speed. if its moving away, then it works opposite, the lift is less as theres less flow over the airfoil.

The only issue i can immidiatly see here that ill run into next is if the wind is strong and the wing (or plane) is moving backwards, then flow will be opposite, which is wrong, as the flow should still be the same but ill cross this bridge when i come to it, unless anyone has any immidiate ideas?

Thanks
Andy
japro
japro

What im trying to do:
Basically i have implimented "wind" into my flight sim as a force and it works perfectly fine, but as "velocity" is a function to the lift force, "wind flow speed" also needs to be taken into account.

So when a wing is moving INTO wind, the velocity speed FOR LIFT EQUATION is that of the plane movemement and the extra wind speed. if its moving away, then it works opposite, the lift is less as theres less flow over the airfoil.

Uhm, shouldn't the planes physics only depend on its speed relative to the air and not on the arbitrarily chosen coordinate system? if you want wind, just replace the velocity with "velocity-wind" and all the physics automatically work out.
AndyGil
AndyGil
Yes, thats correct. That is what i do.

But for the lift force, velocity is a function of it, so if we simply used the plane velocity, when the plane is going INTO wind it will slow down, and thefore lift would become less, but what should really happen is the plane slows down and lift is generated by the wind flow over the wing as apposed to just the planes velocity.

Remember this is not just air velocity, this is wind, so added air velocity.

Andy
japro
japro

Yes, thats correct. That is what i do.

But for the lift force, velocity is a function of it, so if we simply used the plane velocity, when the plane is going INTO wind it will slow down, and thefore lift would become less, but what should really happen is the plane slows down and lift is generated by the wind flow over the wing as apposed to just the planes velocity.


So, you have some function f_lift(v) that calculates the lift which is a force right?

so you do something like:
F = f_lift(v) + other forces
acceleration = F/m

and then you integrate somehow (usually with euler v=dt*a, x = dt*v ...).
now if you want wind you just write: F = f_lift(v-v_wind) + .... and everything is calculate correctly.
obviously you also have to calculate other forces like air drag with v-v_wind.

I don't get the difference between "air velocity" and wind. That's exacly what wind is. moving air with velocity != 0 relative to the ground
AndyGil
AndyGil
lol, i dont know if im confused and doing it wrong, or if im not explaining my self properly.

The reason for this is for "ridge gliding" where lift is dependant on the wind.

So using what you said above, F = f_lift(v-v_wind), would simple be the same in any direction of the plane? Not for a wind coming in a certain direction.

Thanks
Andy

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.