• 🏆 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!

what math functions to call

Status
Not open for further replies.
Level 13
Joined
May 11, 2008
Messages
1,198
"geometry, trigonometry" is what i'm trying to figure out here.

got a question about math functions with x and y reals.

actually, i don't know almost anything about those, and wish someone would make a tutorial on them.

anyway...here's the pressing question i have for now.

let's say i have a hero select a target x/y a certain distance away from him...let's say 200-750 range away.

now i want to get the math to get the x/y points for two more angles of a triangle. but i don't know anything about the math functions to do that.

let's say that there is a line for the triangle crossing with the caster's facing direction. since he's facing the direction of the target x/y when casting there...the line will cross his viewing angle...and i want to in the trigger select the points a certain distance away from there.

so let's say the spell is casted 500 range or more away from the caster. now we want to get a line to draw us a couple more points that will be closer to the caster. each a certain distance from the center of the line.
JASS:
    t

p_______p


    c
something like that.
i need the math functions to be able to get those points so i can use them...also i want to be able to select targets within this triangle of the target, and 2 other points which are extrapolated from the caster's position.

of course...if the target is close to the hero, then the triangle will be drawn differently.

JASS:
p_______p


    t

    c
like that

i knowthere's a tutorial
http://www.hiveworkshop.com/forums/archive/index.php/t-172620.html
there, but i don't understand it.

ok, i'll post an example of what i have so far.


JASS:
local real uf=GetUnitFacing(SpellEvent.CastingUnit)
local real lf=uf+90.00//these two reals go left and right from the center of the
local real rf=uf-90.00//hypotanuse...thus, getting ready to form the two angles of it
local real dist=getdisr(SpellEvent.TargetX,SpellEvent.TargetY,SpellEvent.CastingUnit)
local integer lvl=GetUnitAbilityLevel(SpellEvent.CastingUnit,iac)
local real disfrocen=300.00//we will use this 3 times, twice in establishing
// the angles of the hypotanuse, and once to get the last angle.
//  the distance numbers can change, that doesn't matter what they are.
local unit xa=null
local unit xb=null
local unit xc=null
local player y = GetOwningPlayer(SpellEvent.CastingUnit)
local real tx = SpellEvent.TargetX
local real ty = SpellEvent.TargetY
if dist<500 then
set xa = CreateUnit(y, 'ospw', tx + disfrocen * Cos(lf * bj_DEGTORAD), ty + disfrocen * Sin(lf * bj_DEGTORAD), lf+180.00)
//i don't know what these functions do...i only know they seem to let me make units
//that are at an angle at a distance from a point...(Cos, Sin, bjDEG)
set xb = CreateUnit(y, 'ospw', tx + disfrocen * Cos(rf * bj_DEGTORAD), ty + disfrocen * Sin(rf * bj_DEGTORAD), rf+180.00)
set xc = CreateUnit(y, 'ospw', tx + disfrocen * Cos(uf * bj_DEGTORAD), ty + disfrocen * Sin(uf * bj_DEGTORAD), uf+180.00)
else
set xa = CreateUnit(y, 'ospw', tx - disfrocen * Cos(lf * bj_DEGTORAD), ty - disfrocen * Sin(lf * bj_DEGTORAD), lf-180.00)
set xb = CreateUnit(y, 'ospw', tx - disfrocen * Cos(rf * bj_DEGTORAD), ty - disfrocen * Sin(rf * bj_DEGTORAD), rf-180.00)
set xc = CreateUnit(y, 'ospw', tx - disfrocen * Cos(uf * bj_DEGTORAD), ty - disfrocen * Sin(uf * bj_DEGTORAD), uf-180.00)
endif
set xa = null
set xb = null
set xc = null

so here's where i'm stuck. although i can target the 3 points depending on where the target point is...how am i to calculate for what is in the triangle?

i would think that it would be good to get the degrees of the angles. but i don't know how to do that. i don't know how to get the degrees in the triangle even though with those functions which i don't even know what they do i was able to establish the angles of the triangle. i could make 3 units cast siphon mana on eachother...that would display the sides...but i have no way to know how to do anything physically with that. it's not like you could make units that try to cross that line get attacked by the line.

and what about figuring out how to make one of the 3 units face the direction that is in the middle of the other two units? like i said, i don't know how to figure out what the degrees of an angle is, so how can i cut it in half if i don't even know what it is? wouldn't the degrees of an angle depend on the distances?


hmm...well, i think i figured out kindof how to do it by reading some wiki articles about triangles.

in my code if i write it like this, then the 3 sides will be the same length...meaning that they have the same angle, which is 1/3 of 180, or 60.

they said that

a/aa=b/ba=c/ca. that's the formula.
there was another formula that made me realize that the 3 sides were the same length...lol

d^2+c^2=a^2+b^2
so since the line going to the far angle was 300(d was this times 2), and it going to the other angles was both 300, that was 600times 600...or shorten it to 6x6 giving me 36. and m was c cut in half(why not they post c, i replace it, 2m^2 is a little confusing, i also replace 2d^2 and just call that line times 2 d). which was what i did. and d was that first number. so it was all the same. another 36. so a and b making the 72 meant they both had to have the range of 600 each. it didn't take long after that to realize the angles all had the same degrees.

EDIT: nevermind...it's too confusing...forget i even posted this.
 
Last edited:
Level 28
Joined
Jan 26, 2007
Messages
4,789
Argl, I just made a post for it! D:
Damn me and my long time to create posts >_>

I'll say it anyway, maybe it's still of any use, and if you really don't want it: just ignore this :D



If you want this:

trianglel.jpg

(Dotted lines are only used as reference and will not be visible, unless you want to of course :p).

Then points P1 and P2 are 'easily' calculated (since I don't know what exactly you know and what you don't, I'll try to explain it as detailed as possible, making it look a lot longer than what it actually is).

You know the angle between C (coords x1, y1) and T (coords x2, y2) which is:
Code:
Angle between C and T

angle = Atan2(y2-y1, x2-x1)

And you know the distance at which the line 'a' should be (I thought you wanted it to be static? Meaning it will not be changed?).
You also know h (as you mentioned before, it's also static).

Then all you need to do is this:
Code:
// First we calculate the angle between line h and b.
// As h splits a exactly in the center, the line we will use is a/2 (the left half of a, which forms a triangle with h and b).
Tan(P1angle) = (a/2)/h
=> Tan(P1angle) = a/2h
=> P1angle = Atan(a/2h)

// Now we have the angle, we can calculate b (which we could've done before as well, but who cares?)
b² = (a/2)² + h²
=> b = Sqrt((a/2)² + h²)

You could also use:
Cos(P1angle) = h/b
=> b = h/Cos(P1angle)

// Now we have everything we need (the length of b and the angle between P1 and C).
// Remember: x1 and y1 are C's coordinates
Px1 = x1 + b * Cos(P1angle)
Py1 = y1 + b * Sin(P1angle)

In JASS:

JASS:
local real h = (pre-set)
local real a = (pre-set)
local real x1 = GetUnitX(c)
local real y1 = GetUnitY(c)
local real x3 = GetUnitX(t)
local real y3 = GetUnitY(t1)
local real angle = Atan2(y3-y1, x3-x1)
local real angle2 = Atan(a/2h) + angle // We add angle, because otherwise it wouldn't face the correct direction
local real b = h/Cos(angle)
local real x2 = x1 + b * Cos(angle2)
local real y2 = y1 + b * Sin(angle2)

If this isn't what you asked for, then could you create a drawing or something to make it easier? :S
I hope this is at least a little bit useful :p
 
Last edited:
Level 13
Joined
May 11, 2008
Messages
1,198
Ok then!

Post reported (reason above ^^)

Also I think that you posted this in wrong forum!
Use Triggers & Scripts next time!

you're going to report a guy for admitting there's no way he can solve his own problem? wow, that's harsh. when i said 'this', i was in no way referring to the entirety of my post. just the mistakes that i made in it.

ap0calypse...this looks interesting...but it's a little hard to follow. can you use numbers to help? i'm having a hard time knowing what the variables are...

and i don't know what Atan2, Atan, Cos or Sin does...should i just start tossing out numbers and R2S them?
 
Level 13
Joined
May 11, 2008
Messages
1,198
i'm not sure how much sense you can make out of what i drew but i can't think of anything better right now.
so in the picture...on the leftt kindof shows different ways i was thinking of doing the triangle...i would like to be able to make the triangle hit a larger area at far if the targeted x/y is far from the hero, and the triangle hit a larger area closer to the caster if the x/y target is close to the caster.

in other words...at far, the hypotenuse goes on the other side, at close casting range, the hypotenuse ends up on the near side. and it crosses angles with the caster's facing angle.

now, from there...it gets kindof complicated.

i want to be able to calculate some things

i want to find out for example...let's look on the right...(lol i keep calling me left right and right left since i've become lefthanded...fixed those mistakes i hope)and we should see pink and blue lines, and 3 red dots. now, i don't really care which of those red dots we use, we just need to use one of them i can understand how it works. now, initially i was using the far dot, but now i think the close one would work better. there's also the idea that the middle dot might work out...i really don't know.

i want to calculate all those pink and blue lines. for example, if i can know what the blue lines are, i want to know what the pink lines are, and vice versa. i hope i drew that properly. anyway...as it is right now although i can place some dummy units to check for the positions of the angles as i place them down, i don't seem to always get the results i'm looking for...i don't really know what i'm doing. i need help knowing how to use all those functions...and i don't know how i'm supposed to figure all that out...

and finally..i don't even know how it is possible...but i guess maybe it is...i want to get a group of the units stuck inside the triangle and select them and do something to them.

i'm not sure exactly what kind of triangle i want for it all...but i was thinking an equilateral triangle, with the crossing side close if casted near and far if casted far is what i would find acceptable...but an isoscoles triangle would be preferred.

oh...i think i figured out the angles a bit better. i accidently left an angle not halved, looks like...

anyway...so let's say i make the angle and use the first red dot. i make the angle 60. i put a unit in the direction of those angles...of distance 600. that should mean the hypotenuse will be 600 distance long. but what if i want to make the hypotenuse short or longer? and keep the other sides at 600 distance? then i will have to change the angle between the first red dot and the pink angles, right? so how do i know how much of an angle will change the hypotenuse by how much? not sure if that question made sense...

also, is this the most efficient way of doing what i described?
JASS:
CreateUnit(y, adu, tx + 600.00 * Cos(GetUnitFacing(SpellEvent.CastingUnit)+30.00 * bj_DEGTORAD), ty + 600.00 * Sin(GetUnitFacing(SpellEvent.CastingUnit)+30.00 * bj_DEGTORAD), ??.??)
CreateUnit(y, adu, tx + 600.00 * Cos(GetUnitFacing(SpellEvent.CastingUnit)-30.00 * bj_DEGTORAD), ty + 600.00 * Sin(GetUnitFacing(SpellEvent.CastingUnit)-30.00 * bj_DEGTORAD), ??.??)
CreateUnit(y, adu, tx, ty, ??,??)
i left that last part question marks cuz i didn't turn the orbs back into walkers yet...
 

Attachments

  • triangles.jpg
    triangles.jpg
    29.6 KB · Views: 169
Last edited:
Level 28
Joined
Jan 26, 2007
Messages
4,789
[READ FIRST]Long post ftw...
You can read through this entire thing if you want, if you don't: I have attached a sample-map for you.[/READ FIRST]

Uhm, sorry: I didn't completely understand those drawings :|
(Where's the caster/target?)


However, I've made a test-map for you to see.
Use the spell "channel" on a unit to create a triangle.

If that's what you want, I can explain how I did it ^^
Additionally, I can pick all units inside that triangle (basically you need to calculate the angle between each corner and the unit).
If you still want something to add, or make it... just different, then you need to tell me (of course this isn't the final version yet, as I don't really know what you want).

In the meanwhile, I'll explain some trigonometry (extremely important for coding).


Trigonometry

There are a lot of formula's for trigonometry.

The main formula is:
Cos(A)² + Sin(A)² = 1
You shall never forget this!

Right Triangles:


attachment.php



The basic formula's (apart from the Pythagoraen theorem, which is an extremely common/important formula) are:
  • Cos(angle) = cathetus touching the angle / Hypotenuse.
  • Sin(angle = cathetus not touching the angle / Hypotenuse.
  • Tan(angle = cathetus not touching the angle / cathetus touching the angle
Due to my lack of English, I don't know if there are already existing words for "cathetus (not) touching the angle".
To give you an example you might understand:
  • Cos(A) = b/c (since b is touching the angle A).
  • Tan(B) = b/a (since b is not touching the angle B and a is).

Note that you can divide every triangle into two right triangles by drawing the triangle's altitude (which could make things easier from time to time).

However: right triangles aren't always available, so from here we'll go to the random triangle, which can be any shape (as long as it's a triangle of course).


Random Triangle:


attachment.php



Two things you have to know are the law of Cosines/sines.

The law of cosines can be used to calculate the length of a particular line when you know the opposite angle and the two other sides.
It's something like this:

c² = a²+b²-2ab*cos(C)

Sounds familiar?
It should: it's the pythagorean theorem, but then a bit extended (as the pythagorean theorem only talks about right triangles, while this formula is for all of them).
What happens in a right triangle? Simple: C would be 90º, the cosine of 90º is 0, thus you get c² = a²+b²-2ac*0 => c² = a²+b² ( = pythagorean theorem).

The law of sines is easier.
It states that if you divide all lines divided by the sine of their opposite angle are all equal.

Thus: a/Sin(A) = b/Sin(B) = c/Sin(C)

That means that if you know 2 angles and 1 line, or 1 angle and 2 lines, you can calculate something with this.



Unit Circle


attachment.php



If you wonder where those numbers come from, what exactly the cosine/sine/tangent of a corner mean, then I'd have to show you the Unit Circle.
This is a circle with as radius 1.
The perimeter is then 2pi (meaning that 360º is 2pi, thus 180º = pi).

As you can see: The Cosine of an angle is the first coordinate (X-value) of a point located somewhere on the unit circle.
The Sine is the second coordinate (Y-value).
The Tangent can then be calculated by dividing the Sine by the Cosine (Tan(A) = Sin(A)/Cos(A)).


Misc:

Atan2 is a function which has been invented for programming (afaik), it calculates the angle between two points.
Basically, Atan2(y, x) calculates the Arctangent (the inverse tangent) of y/x, but Atan2 also uses quadrants to return the correct angle.

If you want to know the angle between unit 1 (x1, y1) and unit 2 (x2, y2), you need to do:
Atan2(y2-y1, x2-x1).
However, this will return the angle in radians (a very common thing: all goniometric functions in JASS return/use radians, apart from the BJ's).
Thus you need to multiply it by 180º/pi = 57.295779513 (why this number? Well: check wikipedia for that, my English isn't really good enough for this).

NOTE: every time you use a goniometric function (such as Cos, Sin, Tan), you need to multiply the angle by pi/180º (as you want to convert your angle to radians).
Blizzard already made a function for that: bj_DEGTORAD (Degrees to Radians) and bj_RADTODEG (Radians to Degrees), but I always use the values immediately (up to 9 numbers behind the comma).

Thus: Cos(90.) will be completely wrong, unless you do it like: Cos(90. * bj_DEGTORAD) or Cos(90. * 0.017453292).


To calculate the distance between two points (2-dimensional), you need to use this formula:
SquareRoot((x2-x1)² + (y2-y1)²)
In 3D, it'd be the same but then you need to add (z2-z1)² as well: SquareRoot((x2-x1)² + (y2-y1)² + (z2-z1)²).
 

Attachments

  • TriangleSharp.png
    TriangleSharp.png
    9.1 KB · Views: 330
  • UnitCircle.jpg
    UnitCircle.jpg
    44 KB · Views: 396
  • Triangle.w3x
    18.4 KB · Views: 66
  • TriangleRight.jpg
    TriangleRight.jpg
    12.2 KB · Views: 1,500
Status
Not open for further replies.
Top