Angle by x/y.. again

Post everything that hasn't to do with MOHAA or MOHPA here, including site feedback/suggestions.

Moderator: Moderators

Post Reply
lizardkid
Windows Zealot
Posts: 3672
Joined: Fri Mar 19, 2004 7:16 pm
Location: Helena MT

Angle by x/y.. again

Post 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...
Moderator

۞
Abyssus pro sapientia
Olympus pro Ignarus
۞

AND STUFF™ © 2006
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

Vector

Post 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
Tom Trude,

Image
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post 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 :)
Image
lizardkid
Windows Zealot
Posts: 3672
Joined: Fri Mar 19, 2004 7:16 pm
Location: Helena MT

Post 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.
Last edited by lizardkid on Sat Aug 05, 2006 1:52 am, edited 1 time in total.
Moderator

۞
Abyssus pro sapientia
Olympus pro Ignarus
۞

AND STUFF™ © 2006
User avatar
tltrude
Chuck Norris
Posts: 4774
Joined: Sun Jul 07, 2002 4:03 am
Location: Oklahoma, USA
Contact:

lookat

Post 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?
Tom Trude,

Image
lizardkid
Windows Zealot
Posts: 3672
Joined: Fri Mar 19, 2004 7:16 pm
Location: Helena MT

Post 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:
Moderator

۞
Abyssus pro sapientia
Olympus pro Ignarus
۞

AND STUFF™ © 2006
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post 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 :)
Image
lizardkid
Windows Zealot
Posts: 3672
Joined: Fri Mar 19, 2004 7:16 pm
Location: Helena MT

Post 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!
Moderator

۞
Abyssus pro sapientia
Olympus pro Ignarus
۞

AND STUFF™ © 2006
jv_map
Site Admin
Posts: 6521
Joined: Tue Sep 03, 2002 2:53 pm
Location: The Netherlands
Contact:

Post by jv_map »

Yay :) glad to have been of help m8 :wink:
Image
Elgan
Lieutenant General
Posts: 890
Joined: Tue Apr 13, 2004 10:43 pm
Location: uk
Contact:

Post by Elgan »

is this for players or actors? why not use turnto :D hehe

less fun i know.
PKM
General
Posts: 1888
Joined: Tue Aug 09, 2005 4:43 pm
Location: Philadelpia but stuck in San Antonio TX (hell)

Post 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.
i'm not f****** angry, i'm from philadelphia .
Image
Rookie One.pl
Site Admin
Posts: 2752
Joined: Fri Jan 31, 2003 7:49 pm
Location: Nowa Wies Tworoska, Poland
Contact:

Post 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
Admin
Image
Image
Honour guide me.

here's my stuff - inequation.org | here's where I work - thefarm51.com
Post Reply