• 🏆 Texturing Contest #33 is OPEN! Contestants must re-texture a SD unit model found in-game (Warcraft 3 Classic), recreating the unit into a peaceful NPC version. 🔗Click here to enter!
  • It's time for the first HD Modeling Contest of 2024. Join the theme discussion for Hive's HD Modeling Contest #6! Click here to post your idea!

X/Y Coordinates and Facing

Status
Not open for further replies.
it's not really facing per se, but what I need is to get a point between two points, using strictly x/y and no locations (I know I say points but I do really mean x/y only). Suppose I have point A, B and C. Point will C lie somewhere between point A and B. If I was using locations, I would simply use a polar offset, but I've never used x/y coordinates before :\

Let's assume that C is 100 units away from A, towards angle between A and B. How would I calculate for C's x/y? I'm assuming there will be some trigonometry involved.
 
It is very similar to polar offset (which is calculated through trig).

(1) Calculate the angle from A to B
(2) Calculate polar offset from A towards that angle

JASS:
function Example takes real x1, real y1, real x2, real y2 returns nothing
    local real angle = Atan2(y2 - y1, x2 - x1)
    local real newX = x1 + 100 * Cos(angle)
    local real newY = y1 + 100 * Sin(angle)
    // do stuff
endfunction

Note: I'm not 100% sure whether the coordinates should be swapped in Atan2(). If it goes in the wrong direction, just swap the coordinates or invert the angle.

This will calculate a point 100 units away from (x1, y1), on the line towards (x2, y2). The new coordinates are stored under 'newX' and 'newY'.

Trigonometry can be confusing at times. While it is useful to understand the nitty gritty, you can actually get quite far just by knowing polar offset and angle between points. Angle between points can be calcuated by Atan2(∆y, ∆x). Polar offset simply takes the base coodinate + offset * Cos(angle) (use Sin(angle) for the y coordinate). Always be sure to remember whether things are in radians or degrees! Atan2 returns radians and Cos/Sin accept radians, so it is fine in this case. But if you are using unit facing (degrees), then you would need to convert that to radians before doing meaningful stuff with it.
 
Last edited:

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,199
No need for trigonometry?

Say you want point C 100 units away from A towards B.
Code:
hAC = 100
dxAB = xB - xA
dyAB = yB - yA
hAB = (dxAB^2 + dyAB^2)^0.5
rACAB = hAC / hAB
xC = xA + dxAB * rACAB
yC = yA + dyAB * rACAB
Only problem is working out the distance of AB which requires a power function call (square root).

ATan2 result needs to be adjusted tho, because it returns (-pi; pi>, and angles in wc3 are <0; 2pi)
WC3 Sin, Cos and Tan are defined for any real. You can pass it 980.584 and it will still get you the correct value.
 
Tried PurgeandFire's solution and it worked.

@Doc
Is that calculus? I was never good at that :p. Assuming I want to adjust the angle relative to A to B, to get points D and E that are both 15 degrees apart from the angle b/w A and B (still 100 units away), how would I do that? With trig I'd just have to add/substract the value to/from angle.
 

Dr Super Good

Spell Reviewer
Level 64
Joined
Jan 18, 2005
Messages
27,199
Is that calculus? I was never good at that :p. Assuming I want to adjust the angle relative to A to B, to get points D and E that are both 15 degrees apart from the angle b/w A and B (still 100 units away), how would I do that? With trig I'd just have to add/substract the value to/from angle.
You never asked for that until now. You asked for fragmenting a line segment which does not involve trigonometry at all. Obviously if you introduce angular offsets then you will need to use trigonometry.

You convert the offset between A and B into a vector then scale the vector appropriately before offsetting it to A to get the point of C. The scaled vector used is by multiplying the unit vector form of the vector from A to B with the desired length which requires that you divide the vector from A to B by its own magnitude. Simple high school mathematics.
 
Status
Not open for further replies.
Top