• Listen to a special audio message from Bill Roper to the Hive Workshop community (Bill is a former Vice President of Blizzard Entertainment, Producer, Designer, Musician, Voice Actor) 🔗Click here to hear his message!
  • Read Evilhog's interview with Gregory Alper, the original composer of the music for WarCraft: Orcs & Humans 🔗Click here to read the full interview.

[JASS] Math, Angle Of Attack angles.

Status
Not open for further replies.
Level 18
Joined
Oct 18, 2007
Messages
930
Ok i need a math formula to get the Angle of Attack from 2 given locations and heights.

So you are given 6 variables

  1. 'x' // unit 1's x
  2. 'y' // unit 1's y
  3. 'x2' // unit 2's x
  4. 'y2' // unit 2's y
  5. 'h' // unit 1's height
  6. 'h2' // unit 2's height

How can i find the Angle Of Attack?

I need this formula for a missile thingy and the missile's angle of attack setup goes from '0' to '180' where 0 is up and 180 down
 
Level 18
Joined
Aug 23, 2008
Messages
2,319
You can calculate the angle and between 2 units like this:
  • Set TempPoint[1] = (Position of Unit1)
  • Set TempPoint[2] = (Position of Unit2)
  • Set Angle = (Angle from TempPoint[1] to TempPoint[2])
  • Set Height[1] = (Current flying height of Unit1)
  • Set Height[2] = (Current flying height of Unit2)
  • Set HeightDifference = (Height[1] - Height[2])
Angle and Height are Real variables.


EDIT: I found the height too and added it to the trigger.
 
Level 18
Joined
Oct 18, 2007
Messages
930
I'm not sure about how to do the height, but you can calculate the angle between 2 units with the command:
  • Set TempPoint[1] = (Position of Unit1)
  • Set TempPoint[2] = (Position of Unit2)
  • Set Angle = (Angle from TempPoint[1] to TempPoint[2])
Angle is a Real variable.

Not Angle but Angle Of Attack! :p
attachment.php
 

Attachments

  • A.png
    A.png
    42 KB · Views: 512
Last edited:
Level 11
Joined
May 26, 2005
Messages
194
Arctangent (From Delta), first value = zDistance, second value = xyDistance

or in jass:

Atan2(zDistance,xyDistance)

where
zDistance = h2-h1
and xyDistance = Sqrt((x2-x1)^2+(y2-y1)^2)

if the terrain isnt flat, you may want to add GetLocationZ(<loc at the units position>) to the units fly heights before calculating the zDistance.


@Avator: you are really too dumb...
 
Level 18
Joined
Oct 18, 2007
Messages
930
Arctangent (From Delta), first value = zDistance, second value = xyDistance

or in jass:

Atan2(zDistance,xyDistance)

where
zDistance = h2-h1
and xyDistance = Sqrt((x2-x1)^2+(y2-y1)^2)

if the terrain isnt flat, you may want to add GetLocationZ(<loc at the units position>) to the units fly heights before calculating the zDistance.


@Avator: you are really too dumb...

? :p Please i dont really get it. Could you maybe make it look like it would look like in the script? :p ( sorry for my dumbness )
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
JASS:
function GetAngleOfAttack takes unit u, unit u2 returns real
    local real x1 = GetUnitX(u)
    local real y1 = GetUnitY(u)
    local location stupid = Location(x1, y1)
    local real z1 = GetUnitFlyHeight(u) + GetLocationZ(stupid)
    local real x2 = GetUnitX(u2)
    local real y2 = GetUnitY(u2)
    local real z2 = GetUnitFlyHeight(u)
    local real a
    local real dist = SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    call MoveLocation(stupid, x2, y2)
    set z2 = z2 + GetLocationZ(stupid)
    set a = Atan2(z2 - z1, dist)
    call RemoveLocation(stupid)
    set stupid = null
    return a
endfunction
I think this will work.
 
Level 18
Joined
Oct 18, 2007
Messages
930
JASS:
function GetAngleOfAttack takes unit u, unit u2 returns real
    local real x1 = GetUnitX(u)
    local real y1 = GetUnitY(u)
    local location stupid = Location(x1, y1)
    local real z1 = GetUnitFlyHeight(u) + GetLocationZ(stupid)
    local real x2 = GetUnitX(u2)
    local real y2 = GetUnitY(u2)
    local real z2 = GetUnitFlyHeight(u)
    local real a
    local real dist = SquareRoot((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))
    call MoveLocation(stupid, x2, y2)
    set z2 = z2 + GetLocationZ(stupid)
    set a = Atan2(z2 - z1, dist)
    call RemoveLocation(stupid)
    set stupid = null
    return a
endfunction
I think this will work.

k, il try :p
JASS:
R2I(Atan2((GetUnitFlyHeight(Reciver) + GetLocationZ(LocX)) - (GetUnitFlyHeight(Sender) + GetLocation>(LocX)), SquareRoot((x2 - x1)*(x2 - x1)+(y2 - y1)*(y2 - y1)))))
 
Level 6
Joined
Sep 5, 2007
Messages
264
I know that this topic is over with, but I have a question of relevance...
How would you get "normalized" versions of the angles?
IE: if you have a timer (0.05 seconds), you're given a speed, and initially given a target destination... how would you go about storing the x/y/z changes (per tick) into 3 variables (a velocity vector)?
 
Level 22
Joined
Dec 31, 2006
Messages
2,216
JASS:
function whatever takes unit u, real tx, real ty, real time returns nothing
    local real vx = (GetUnitX(u) - tx) / (time/0.05)
    local real vy = (GetUnitY(u) - ty) / (time/0.05)
    local real vz = (9.81*time) / 2
...
endfunction
 
Level 6
Joined
Sep 5, 2007
Messages
264
Thanx for that, but I have seen that equation before. That is the parabolic arc equation. What I mean is: When given destination & source points, how do you get the "velocity factor" for each of the 3 axis of movement.

EG:
Moving from 0,0,0 to 0,0,100 obviously this only involves movement on the z axis so the answer is (0,0,1)
Moving from 0,0,0 to 100,100,0 this involves equal movement on the x & y axis, so the answer is (0.5, 0.5, 0)

I need the velocity vector (all 3 axis) to be a factor of 1... a "normalized vector" as they call it.
 
Level 7
Joined
Jul 20, 2008
Messages
377
Thanx for that, but I have seen that equation before. That is the parabolic arc equation. What I mean is: When given destination & source points, how do you get the "velocity factor" for each of the 3 axis of movement.

EG:
Moving from 0,0,0 to 0,0,100 obviously this only involves movement on the z axis so the answer is (0,0,1)
Moving from 0,0,0 to 100,100,0 this involves equal movement on the x & y axis, so the answer is (0.5, 0.5, 0)

I need the velocity vector (all 3 axis) to be a factor of 1... a "normalized vector" as they call it.

Actually, moving from 0, 0, 0 to 100, 100, 0 would yield the vector (1, 1, 0). But yeah, look up spherical coordinates if you want the formula.
 
Level 6
Joined
Sep 5, 2007
Messages
264
I have looked up sherical co-ordinates, but I've had an annoying time putting it into Wc3... And you are right about the (1,1,0) thing, the way I put it above, the object would move at 1/2 speed.

I'm most interested in getting the fration of velocity that the movement on the z-axis requires, then having that effect the movement on the x/y axis. I can do x/y movement, that is easy. It's the z axis that's getting me.
 
Level 8
Joined
Aug 4, 2006
Messages
357
i'm pretty sure a normalized vector has a magnitude of 1, meaning the squareroot((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) = 1. So for your example of (0, 0, 0) to (100, 100, 0), a vector of (1,1,0) would give you a magnitude of sqrt(1 + 1 + 0), which = sqrt(2) which does not = 1. To find the normalized vector, we would divide each of the components of the non-normalized vector by the magnitude of the vector. (100/sqrt(20000), 100/sqrt(20000), 0) = (.707, .707, 0)

So, to find the normalized vector for movement to any point in three dimensions, say from (0,0,0) to (4,90,36) we would first make a vector using the changes in x,y,z: (4,90,36).
then we plug in the coordinates into the distance equation.
sqrt(4^2 + 90^2 + 36^2) = 97.02...
and divide each component of our vector by this magnitude
(4/97, 90/97, 36/97) = (.04, .93, .37). to double check our work, we plug these into the magnitude formula and sure enough, we get 1.00

so once you find the normalized vector, you can multiply the components by whatever max speed you want the unit to have, and voila!
 
Status
Not open for further replies.
Top