Home   Help Search Login Register  

Author Topic: Help with vector math  (Read 558 times)

0 Members and 3 Guests are viewing this topic.

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Help with vector math
« on: 02 Dec 2003, 22:17:47 »
Okay, who likes math? I know I could figure this one out, but I just don't have the will to sit down and try it out right now. So I was hoping somebody else would be able to tell me the answer off the top of their head, thus saving me the time!

Here is what I want to do, in the form of a function:

Picture an imaginary line running perpendicular to the direction a unit is facing. I.e. a unit facing due north would have a line that runs east/west. Now imagine a unit is somewhere in front of (or behind) that line.  I will call that line "L".What I want is to find the closest distance between that unit and L (which would be the length of a line running through that unit and perpendicular to the line L). If the unit is infront of the line (meaning the unit who "made" the line is facing in the direction of the unit), then this distance will be positive; if he is behind the line, the distance will be negative.

Simple enough, right? The location of the line L isn't really that important, because all I need is the differences in the distances returned by a function described above. I'm thinking that such a function would be passed (A) a position and direction (to make the line) and (B) a unit (or position) to find the distance.

Anyone wanna help? Much thanks!
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Offline macguba

  • Former Staff
  • ****
    • macguba's operation flashpoint page
Re:Help with vector math
« Reply #1 on: 02 Dec 2003, 23:46:46 »
This is not a complete answer, it's just a start ....

Using toady's trigonometry guide in the Ed Depot figure out the angle between loon1 and loon2 = _a

_b = loon1 distance loon2

_c = getDir loon1

_d = _c + 90   (the angle of L)

_p = _a - _d


sin _p = _x/_b
=> _x = _b(sin _p)

where _x is what you're solving for - the distance between loon2 and L.  

Is that right?  I'm too tired to tell.   You may have to play with it a bit to make sure _p is acute.    The key point is that the right angled triangle has the straight line between the two units as its hypotenuse.   L is one side and _x is the other side of the triangle.    _p is the angle between the hypotenuse and L.
« Last Edit: 02 Dec 2003, 23:48:58 by macguba »
Plenty of reviewed ArmA missions for you to play

Offline General Barron

  • Former Staff
  • ****
  • Semper Fi!
Re:Help with vector math
« Reply #2 on: 06 Dec 2003, 08:37:04 »
Hmm.... it looks good. Lol, leave it to me to try and make things more complicated than they have to be. I'll check this out to see if it works. Thanks!
HANDSIGNALS COMMAND SYSTEM-- A realistic squad-control modification for OFP
kexp.org-- The best radio station in the world, right here at home! Listen to John Richards!

Rubble_Maker

  • Guest
Re:Help with vector math
« Reply #3 on: 07 Dec 2003, 17:20:55 »
This is quite simple if we use a implicit line equation,
because then we need the normal to the direction vector, which is simply the vector that the unit is facing. Lets call this vector A, then we compute A=(ax,ay) as

ax=sin(getdir unit)
ay=cos(getdir unit)

next we need a point on the line, lets call it P=(px,py):

px=getpos unit select 0
py=getpos unit select 1

Next we need the position of the unit whose shortest distance to the line you want, lets call it R=(rx,ry)

rx=getpos dude select 0
ry=getpos dude select 1

Ok here we go: We have a normal vector A of the line, a point P which is
on the line, and another point R whose distance to the line we want to
compute.

The implicit line equation of the perpendicular line now simply is

(X-P)*A=0

where * is the 2-dimensional dot product, and X is any point on the line.
(note: the dot product of two vectors U and V is computed as (ux*vx+uy*vy)). If the point X we stick into the equation is ON the line, then the result is 0. If its not, the result will be a positive or negative value d. Now we plug in the point R:

(R-P)*A=d

and the shortest distance s you're looking for now is

s=((R-P)*A)/|A|

(this is due to the fact that the dot product equals the cosine of the angle between the two vectors, multiplied with their length. in this case the cosine is exactly what we need to find the length of the side opposite to the angle)

ok here's the code, I've set W=(wx,wy)=R-P for simplicity:

wx=rx-px
wy=ry-py
s=(wx*ax+wy*ay)/sqrt(ax*ax+ay*ay)

hope it works ;)