Gamestudio Links
Zorro Links
Newest Posts
WFO Training with parallel cores Zorro64
by Martin_HH. 02/24/26 19:51
Zorro version 3.0 prerelease!
by TipmyPip. 02/24/26 17:09
ZorroGPT
by TipmyPip. 02/23/26 21:52
Camera always moves upwards?
by clonman. 02/21/26 09:29
Sam Foster Sound | Experienced Game Composer for Hire
by titanicpiano14. 02/19/26 13:22
AUM Magazine
Latest Screens
Dorifto samurai
Shadow 2
Rocker`s Revenge
Stug 3 Stormartillery
Who's Online Now
5 registered members (Martin_HH, TipmyPip, AndrewAMD, Grant, USER0328), 5,287 guests, and 2 spiders.
Key: Admin, Global Mod, Mod
Newest Members
alx, ApprenticeInMuc, PatrickH90, USER0328, Sfrdragon
19199 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 2 1 2
Angle between 2 entities #137712
06/23/07 09:57
06/23/07 09:57
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline OP
Serious User
vlau  Offline OP
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
Can someone tell me how can I calculate the angle (0-359)
between 2 entities? Here is what I've done so far :

Code:

dx = my.x - you.x;
dy = my.y - you.y;

angle = fatan(dy,dx);




The angle always return +/- 90 only.

Thanks.

Re: Angle between 2 entities [Re: vlau] #137713
06/23/07 10:42
06/23/07 10:42
Joined: Mar 2007
Posts: 776
Poor village - Poland ;)
tompo Offline
User
tompo  Offline
User

Joined: Mar 2007
Posts: 776
Poor village - Poland ;)
vec_set(temp,you.x);
vec_sub(temp,my.x);
vec_to_angle(temp_angle,temp);

in this case temp_angle = angle to turn to look at second entity so it's different between entitys.
This is what you want?


Never say never.
Re: Angle between 2 entities [Re: tompo] #137714
06/23/07 13:29
06/23/07 13:29
Joined: Mar 2006
Posts: 3,538
WA, Australia
J
JibbSmart Offline
Expert
JibbSmart  Offline
Expert
J

Joined: Mar 2006
Posts: 3,538
WA, Australia
and if you insist on using ur own trigonometry you need to write an atan2 function which'll return between +-180 (unless you have the latest A7 private-beta).

it just so happens i have one in my back-pocket (changed to work with c-script but untested):
Code:
function atan2(var x,var y)
{
if(y==0)
{
return (90*sign(x));
}
return(atan(x/y) - (180 * (y<0)));
}



julz


Formerly known as JulzMighty.
I made KarBOOM!
Re: Angle between 2 entities [Re: tompo] #137715
06/23/07 13:42
06/23/07 13:42
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline OP
Serious User
vlau  Offline OP
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
Nope, thanks. The 'you' entity will move around the centre of 'my'
entity at runtime, I want to calculate the angle (0-359 degrees) of
the 'you' entity, which will return 0 degree when 'you' face at 'my'
and 180 degree when 'you' is behind 'my' entity.

Hope that clear.

Re: Angle between 2 entities [Re: JibbSmart] #137716
06/23/07 13:42
06/23/07 13:42
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline
Expert
Slin  Offline
Expert

Joined: May 2005
Posts: 2,713
Lübeck
this is what I use:

Code:

vec_set(temp,Node_pos);
vec_sub(temp,my.x);
vec_to_angle(myang,temp);
myang.pan = ang(myang.pan);





Last edited by Slin; 06/23/07 13:43.
Re: Angle between 2 entities [Re: Slin] #137717
06/23/07 14:02
06/23/07 14:02
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline OP
Serious User
vlau  Offline OP
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
Hi Slin,

Your code looks very close, but still return negative
value when the 'you' entity move to the right side of
'my'. Can you help me a little bit more to solve this
problem? I want it to return 0 to 359 degree.

I'm sorry but I'm too tired right now after eating some
pills.....zzzzzzz.

Re: Angle between 2 entities [Re: vlau] #137718
06/23/07 14:15
06/23/07 14:15
Joined: Mar 2007
Posts: 776
Poor village - Poland ;)
tompo Offline
User
tompo  Offline
User

Joined: Mar 2007
Posts: 776
Poor village - Poland ;)
so maybe use abs to avoid negative values?


Never say never.
Re: Angle between 2 entities [Re: tompo] #137719
06/23/07 14:21
06/23/07 14:21
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline OP
Serious User
vlau  Offline OP
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
Ok, I think I got it :

Code:

vec_diff(temp,you.x,my.x);
vec_to_angle(angle,temp);
angle %= 360;



Do you think so? if not please comment.
Thank you all.

Re: Angle between 2 entities [Re: vlau] #137720
06/23/07 14:57
06/23/07 14:57
Joined: May 2005
Posts: 2,713
Lübeck
Slin Offline
Expert
Slin  Offline
Expert

Joined: May 2005
Posts: 2,713
Lübeck
What Tompo wrote should exactly be what you are looking for, isn´t it?
It is the same as that what I wrote just without the ang(); wich causes the -180/180 range.

Re: Angle between 2 entities [Re: Slin] #137721
06/23/07 15:10
06/23/07 15:10
Joined: Aug 2005
Posts: 1,558
HK
V
vlau Offline OP
Serious User
vlau  Offline OP
Serious User
V

Joined: Aug 2005
Posts: 1,558
HK
Ah yes, I'm just thinking you guys will improve my
previous code using fatan(...) Well, sorry for that.

Page 1 of 2 1 2

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1