Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / direction and range detection

Author
Message
RCON
22
Years of Service
User Offline
Joined: 20th Apr 2003
Location:
Posted: 8th May 2003 00:16
Is there a function or formula that will check a object with another and return the angle or range to that object?
Hamish McHaggis
22
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 8th May 2003 00:44 Edited at: 8th May 2003 00:45
To find the angle, I guess you have to use trig. Try this formula...

x_dif#=object position x(obj1)-object position x(obj2)
z_dif#=object position z(obj1)-object position z(obj2)
y_angle#=atan(x_dif#/z_dif#)

To find the distance between the objects you use pythagorus' theorum...

ON 2 DIMENSIONS (a plane)
z^2 = y^2+x^2
or
z = sqrt(y^2+x^2)

ON 3 DIMENSIONS
z^2 = y^2+x^2+w^2
or
z = sqrt(y^2+x^2+w^2)

where...
z = the dist between the two points
w, x and y = the difference (distance) between the x,y and z coordinates of the objects.

Why the hell'd you ask me for crying out loud!?!

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
RCON
22
Years of Service
User Offline
Joined: 20th Apr 2003
Location:
Posted: 8th May 2003 01:59
so it would look like this:

rem get object position differences
x_dif#=object position x(recognizer)-object position x(200)
z_dif#=object position z(recognizer)-object position z(200)

rem angle difference
y_angle#=atan(x_dif#/z_dif#)

rem distance range
z = sqrt(x_dif#^2+z_dif#^2)

(i can't test it until I get home, just wanted to see if this is right.) BTW its a 3d game, but I don't really use elevation.
Hell IVIonkey
22
Years of Service
User Offline
Joined: 1st Apr 2003
Location: Outer Limits
Posted: 8th May 2003 03:07
You're best off using the distance squared formula and skipping the middle steps:

dist_squared = (x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2

Then when you do a test to say if an object less than value away, use the form:

if (value)^2 < dist_squared then do whatever.

Why? Square roots are slow when done in bulk.
RCON
22
Years of Service
User Offline
Joined: 20th Apr 2003
Location:
Posted: 8th May 2003 19:23
ok I have range working good, but I have a question on the angle. I have to admit that I am pretty bad with math, and I want the angle that is returned to be a 0-360 degree number. the angle that I can get returned now is a number between -90 - 0 - 90. for example, if my "hunter" object sees the "hunted" object at -45, the hunted could either be front left or back right to the hunter. So my hunter doesn't know the correct direction to turn to track the hunted object.
Hamish McHaggis
22
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 8th May 2003 19:37
I think the formula atan(x_diff#/z_diff#) will find that angle for you.

Why the hell'd you ask me for crying out loud!?!

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
RCON
22
Years of Service
User Offline
Joined: 20th Apr 2003
Location:
Posted: 8th May 2003 20:16
it does return an angle, but its in the -90 to 90 range, and it would help me a lot if it returned an angle in the 0 to 359 range.
Hamish McHaggis
22
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 8th May 2003 21:23
Try

asin(x_dist#/dist_range#)

or

acos(y_dist#/dist_range#)

Possibly

Why the hell'd you ask me for crying out loud!?!

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
Danmatsuma
22
Years of Service
User Offline
Joined: 2nd Mar 2003
Location: Australia
Posted: 8th May 2003 22:32 Edited at: 8th May 2003 23:10
You want a 360 degree angle between 2 objects?

angle#=wrapvalue(atanfull(x_diff#,z_diff#))



ZX Spectrum 48k Issue 3, Radio shack Tape drive, Rank arena 12" T.V. set.
Hamish McHaggis
22
Years of Service
User Offline
Joined: 13th Dec 2002
Location: Modgnik Detinu
Posted: 8th May 2003 23:33
Ahhh! atanfull, thats why it wasn't working

Why the hell'd you ask me for crying out loud!?!

Athelon XP 1400 Plus - Nvidia Geforce MX400 - 256mb RAM
RCON
22
Years of Service
User Offline
Joined: 20th Apr 2003
Location:
Posted: 9th May 2003 07:08
you guys rock!

Login to post a reply

Server time is: 2025-05-17 09:46:53
Your offset time is: 2025-05-17 09:46:53