Page 1 of 1

Angle by x/y.. again

Posted: Thu Aug 03, 2006 11:05 pm
by lizardkid
Yes i know i made a post on this before and got two methods of doing it, but i'll be in the church of pkm if i could get any of it to work.

What i need is a method or algorithm to get the amount a game entity needs to rotate to be facing a given x/y coord. distance, angles, etc aren't a problem, i just cant figure out how to do it, much less efficiently.

yes this is a "make a map for me plzplzplzplz?" post. can anyone just give me a coded way to return the amount a game ent would need to rotate to face a given xy (it'll eventually end up radians but even i can convert degrees to them)?

i'm at that desperate stage where everything else kinda needs this, so...

Vector

Posted: Fri Aug 04, 2006 3:10 am
by tltrude
I think what you are looking for is "vector_toangles". But, I can't remember how it is used, although I think it requires two entity origins or coordinates.

myvec = vector_toangles(myent1.origin, myent2.origin)

or

myvec = vector_toangles(myent1.origin - myent2.origin)

There might be something in this post that will help.
/forum/viewtopic.php?t=10489

Posted: Fri Aug 04, 2006 9:36 am
by jv_map
Your question is somewhat ambiguous. Can you illustrate what you need it for? Is the axis of rotation constrained, or arbitrary in space? In the latter case, there are an infinite number of solutions (you could choose a random 'roll' angle). So best give a bit more info :)

Posted: Fri Aug 04, 2006 9:39 pm
by lizardkid
Alright here's what i need. Imagine two soldiers on either side of a field.

Code: Select all

||                          ||    ^ and > indicate facing
||   o>                ^   ||
||                     o   ||
||                          ||
||                          ||
Now, to shoot at each other the soldiers really should be looking at each other right? This is what i'm after. What kind of algorithm would the soldier have to determine how much he should rotate to face his enemy?

in other words, how many radians does he need to turn to face his enemy from his current angle?

Getting angles x/y and distance isn't a problem, and i know there's a way to do it, (almost every game does) but i can't figure it out.

The 'soldiers' don't use vectors either, which may have been a huge mistake on my part. But i know there must be a way to do it without vectors.

lookat

Posted: Fri Aug 04, 2006 10:01 pm
by tltrude
If a 'soldier' stops to do a math problem, he'll be dead before he can act. Can't you just use the "lookat" command?

Posted: Sat Aug 05, 2006 3:29 am
by lizardkid
reading my first post again i realized i didnt say, but this is for a game i'm making as a learning experience, it's not a MOH question. :oops:

Posted: Sat Aug 05, 2006 7:08 am
by jv_map
Ok so I suppose for each soldier you have a x and a y coordinate?

Then it'll be an atan fest :)

If the language you're using supports it, just use atan2.

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

That'll give you the absolute angle in the usual mathematical convention:

Code: Select all

y
^
|      /
|    /
|  /\ positive angle
|/   |
0------------>x
Where, usually, angle ranges between [-pi,pi) or [-180,180).

Now I suppose you store a yaw angle for each soldier, then the relative angle you asked for (i.e. how much to turn), is simply

relative_angle = angle - yaw1

Note that this implies yaw1 is defined by the same convention as above.

Hope you can get it to work, let us know either way :)

Posted: Sun Aug 06, 2006 3:51 am
by lizardkid
It finally works!

I had to do quite a bit of tweaking and testing to get it to the stage i was happy with, but the algorithm works flawlessly in all circumstances i've tossed it throguh so far.

Can't tell you how happy i am this works, that was the major stumbling block i had before i could get on to more interesting things, like finding cover and intercepting enemies. thanks jv!

Posted: Sun Aug 06, 2006 7:21 am
by jv_map
Yay :) glad to have been of help m8 :wink:

Posted: Tue Aug 08, 2006 9:19 am
by Elgan
is this for players or actors? why not use turnto :D hehe

less fun i know.

Posted: Tue Aug 08, 2006 4:10 pm
by PKM
Elgan wrote:is this for players or actors? why not use turnto :D hehe

less fun i know.
what, did the old timer's modding league season end early or something ? it's like buffet time down at the retired radient home with all you pros showing up.

Posted: Wed Aug 09, 2006 12:51 pm
by Rookie One.pl
Elgan wrote:is this for players or actors? why not use turnto :D hehe

less fun i know.
lizardkid wrote:reading my first post again i realized i didnt say, but this is for a game i'm making as a learning experience, it's not a MOH question. :oops:
RTFWT. :P