#### Archived

This topic is now archived and is closed to further replies.

# Please some one help me with orientation !!!

This topic is 5882 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi Guys, my question is really easy, but i''m so dumb in math that i can''t figure it out !!! i have two objects in my game -enemy and player-, i want the enemy to be drawn to the orientation of the player ... that''s it ... i have the x,y,z positon of both the enemy and the player ... what i need is a function that would rotate the enemy so that when i draw it it would look like as if it was heading for the player -not moving to the right while it''s face is drawn to the back- ... please help me ... "If you don''''t like something, change it. If you can''''t change it, change the way you think about it!" "He who asks is a fool for five minutes, but he who does not ask remains a fool forever." "Imagination is more important than knowledge, for knowledge is limited while imagination embraces the entire world."(Einstein) "The normal teacher tells. The good teacher explains. The superior teacher demonstrates. The great teacher inspires."(William Arthur Ward)

##### Share on other sites
i can tell how much u like math from your signature

i didn''t get what you want exactly, but if u want to place the enemy infront of the player on a distance s.
you should have a vector of position for both. a heading vector for the player and the distance you want to place the enemy in.

player pos = vp;
distance = s;

FindEnemyPos()
{
vph_temp= Normalize(vph);
vph_temp= Scale(vph_temp, s);
}

hope that hits the spot ...

"The Railgun Master"
DaHeR

##### Share on other sites
Thanks daher ...

but maybe i was not clear enough ... what i want is that when i render the enemy -which is a tank- i want it to be drawn as if it's pointed at me ... you know what i mean ...

just imagine that the tank is moving towards me ... i want to rotate it so that it's turret is directed to me ... i just want the function to calculate the amount of degrees i have to rotate it ...

|
|   |  /
|
------------
|
x   |  -
|

where x is the player, and the | and - and / are the enemy ... you see the oriantation ....

thanks ...

Edited by - fixxxer on January 9, 2002 3:23:03 PM

##### Share on other sites
not to sound rude but if you goto gamedev.net''s forums in there math section i am sure someone there can give you some code to find angles between 2 points.. i have some code but its written in another programming language. if you wanna convert it let me now and i can post it. its only a handfull of lines.

http://www.lectersoft.com

##### Share on other sites
//what the hell here it is anyway
the values with the # are floats

function ca(px1#,py1#,px2#,py2#)

dx#=px2#-px1#
dy#=py2#-py1#

if dx#<0 then dx#=dx#*-1 : inc quadflag
if dx# <> 0
angle#=ATAN(dy#/dx#)
else
angle#=ATAN(dy#)
endif

if quadflag=1 then angle# = 180-angle#
if quadflag=2 then angle# = 360-angle#
if quadflag=3 then angle# = 180+angle#

if angle# > 0 then angle# =int(angle# +.50)

endfunction angle#

##### Share on other sites
dfawcett ... you are the greatest ... thanks dude ... you know earth is a great place to live because of people like you ...

thanks again ... i will try that ... but i think it''s what i need because it''s excatly what i thought of ...

thanks man ...

no prob

##### Share on other sites
This is how i convert .. why it doesn''t work ...

float dx = wx_pos - player.wx_pos;
float dy = wy_pos - player.wy_pos;

if (dx < 0)
{
dx = dx * -1;
}

if (dy < 0)
{
dy = dy * -1;
}

if (dx != 0)
cTanksceneroty = atan(dy/dx);
else
cTanksceneroty = atan(dy);

cTanksceneroty = 180-cTanksceneroty;
cTanksceneroty = 360-cTanksceneroty;
cTanksceneroty = 180+cTanksceneroty;

if (cTanksceneroty > 0)
cTanksceneroty = int(cTanksceneroty +.50);

glRotatef(cTanksceneroty,0,1.0f,0);

##### Share on other sites
Maybe there''s a problem with angle units.
I guess atan return the angle in radians, so you shouldn''t
add/substract 180 or 360 but PI or 2*PI, or convert the
result of atan().

And ya also have to make sure the unit is correct when
using glRotate !

##### Share on other sites
i thought about this after i posted it here is the code in use but in darkbasic format

sync on
synce rate 0

an=10
tx#=320
ty#=240
x#=320
y#=240

do
cls

text 1,1,"true angle "+str$(an) text 1,20,"Difference between x''s "+str$(newx#)
text 1,40,"Difference between y''s "+str$(newy#) text 1,60,"My angle "+str$(angle#)

x1#=0+cos(an)*50
y1#=240+sin(an)*50

dx#=x#-mousex()
dy#=y#-mousey()

angle#=int(ca(dx#,dy#))

line x#,y#,mousex(),mousey()

if leftkey()=1 then an=wrapvalue(an-1)
if rightkey()=1 then an=wrapvalue(an+1)

sync
loop

function ca(dx#,dy#)
if dx#<0 then dx#=dx#*-1 : inc quadflag
if dx# <> 0
angle#=ATAN(dy#/dx#)
else
angle#=ATAN(dy#)
endif

if quadflag=1 then angle# = 180-angle#
if quadflag=2 then angle# = 360-angle#
if quadflag=3 then angle# = 180+angle#

if angle# > 0 then angle# =angle# +.50

angle#=wrapvalue(angle#+180)

endfunction angle#

##### Share on other sites
i forgot to mention darkbasic uses degrees not rads sorry for the confusion.

##### Share on other sites
Thanks a lot guys with your help, and some little thinking i was able to figure it out ... you guys rock ...

dfawcett Thanks a lot man for the professional explaination ... WOW! i never thought anyone can be this much helpful and supportive ...

Thanks a million rodzilla and daher ... i appricate everything you wrote

again you guys rock

here is how it finally worked for me ...

float dx = wx_pos - player.wx_pos;
float dy = wy_pos - player.wy_pos;

if (dx < 0)
{
dx = dx * -1;
}

if (dy < 0)
{
dy = dy * -1;
}

{
cTanksceneroty = 180 + cTanksceneroty;
}

{
cTanksceneroty = 90 + cTanksceneroty;
}

{
cTanksceneroty = 270 + cTanksceneroty;
}

{
cTanksceneroty = cTanksceneroty;
}

glRotatef(cTanksceneroty,0,1.0f,0);

##### Share on other sites
sorry for the delay i was busy but i think u got what u want,
thanx 2 ol...

"The Railgun Master"
DaHeR