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.

Code Snippets / Knowing your angles

Author
Message
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 1st Dec 2002 18:36
Just posting a small function for anyone having trouble calculating there angles.




...and a little demo.





Hope it's useful
"Earth is the cradle of humanity,
but one cannot live in a cradle forever"
Konstantin E. Tsiolkovsky (1911)
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 1st Dec 2002 18:40
Nuts! the function did'nt copy properly, lets try again....

function GetAngle(cx#,cy#,x#,y#)
rem --- cx#, cy# = center point
rem --- x#, y# = point around center

a# = (y# - cy#)
b# = (x# - cx#)

if b# = 0.0
if a# <= 0.0 then ang# = 0.0 else ang# = 180.0
else
ang# = atan(a#/b#)
if b# >= 0.0 then inc ang#,90.0 else inc ang#,270.0
endif

endfunction ang#

"Earth is the cradle of humanity,
but one cannot live in a cradle forever"
Konstantin E. Tsiolkovsky (1911)
hexGEAR
22
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 1st Dec 2002 21:06
do they apply for 3d if i use z instead of y in the code?

your birth was a blessing, sent to live and die on earth as a lesson, we each have a star all you have to do is find it, once you do, everyone who sees it will be blinded - DMX
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 1st Dec 2002 21:19
Though I have'nt tried it in 3D, it should be OK to pass Z co-ords into the cy# and y# parameters.

"Earth is the cradle of humanity,
but one cannot live in a cradle forever"
Konstantin E. Tsiolkovsky (1911)
haggisman
22
Years of Service
User Offline
Joined: 26th Aug 2002
Location: United Kingdom
Posted: 1st Dec 2002 21:49
good example

Specs:- 1GHZ athlon, Radeon8500, 192mb ram, winxp
hexGEAR
22
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 1st Dec 2002 21:53
thanx man, i'm gonna be using this for another game!

your birth was a blessing, sent to live and die on earth as a lesson, we each have a star all you have to do is find it, once you do, everyone who sees it will be blinded - DMX
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 1st Dec 2002 22:28
Thankyou haggisman.

Hexgear I've just experimented in 3D, and discoverd the result appears to be inverted, so you may want to change a line in the function from:

a# = (y# - cy#)

to

a# = (cy# - y#)

...but that's up to you

"Earth is the cradle of humanity,
but one cannot live in a cradle forever"
Konstantin E. Tsiolkovsky (1911)
Symunona
21
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 8th Dec 2002 20:27
Much much much thx! I couldnt understand, why the point object doesnt works, but now i can elminate it! thx again!
hexGEAR
22
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 2nd Feb 2003 04:20
sorry to dig this up but i found a totally new way of calculating line of sight (like the dark basic command), i got so frustrated using the DB command because my enemy a.i. kept on looking through trees and buildings like they wherent there, no matter the value of width and accuracy for the line of sight command.

However i used your formula above and made a kinda 3D version, it works way better than the DB line of sight command, the only catch is that it's afected by object personal collision boxes (and not static boxes). it requires one single object so i'll upload it in RGT and place a link here to the code snippet!

this could be very helpful in making your enemy A.I.

your birth was a blessing, sent to live and die on earth as a lesson, we each have a star all you have to do is find it, once you do, everyone who sees it will be blinded - DMX
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 2nd Feb 2003 04:55
It's somewhat embarrasing to admit this, but after re-reading this subject (and re-reading the manual) I have only just found another way it could be done.

Replace the old function with this new one in the above example, it will show the same result:



This is much smaller, and I think fractionly faster. I had totaly overlooked the AtanFull() command.

(hangs head in shame and sits facing the corner)

However, for 3D use you still may have to swap y# and cy# around.

Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.
hexGEAR
22
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Naytonia
Posted: 2nd Feb 2003 05:19
wow, ain't your fault, never even knew that command existed! but hey, it's still good to know how to calculate the angle btw two points using raw code instead of an unknown function

your birth was a blessing, sent to live and die on earth as a lesson, we each have a star all you have to do is find it, once you do, everyone who sees it will be blinded - DMX
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 2nd Feb 2003 06:10
What's equaly embarrasing is it took me nearly two months to realise this

True, it is handy to know the raw code version, especaily if you're translating a program from one language to another and you didn't know how the AtanFull() worked. The old function will easily convert to C++ for example.

Thanks for pointing that out, makes me feel a bit better

Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.
TheCyborg
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Denmark
Posted: 10th Feb 2003 12:09
You don't have to swap y# and cy#...
Just use the ABS() command to always make it positive.
e.g.
ang# = 180.0 - atanfull(x#-cx#,ABS(y#-cy#))

If you get the number -165 ABS will convert it to (+)165.

TheCyborg Development.
http://TheCyborg.Amok.dk
The Ultimate Source To DarkBASIC Programming.
Easily Confused
22
Years of Service
User Offline
Joined: 22nd Oct 2002
Location: U.K. Earth. (turn right at Venus)
Posted: 11th Feb 2003 00:59
True, that will also work, but after thinking about it, perhaps it would be better to offer a choice of calculation modes, either using 2D screen co-ords or using 3D plain co-ords. Handy if your program requires both.



...that should finalise things, I hope

Programming anything is an art, and you can't rush art.
Unless your name is Bob Ross, then you can do it in thirty minutes.

Login to post a reply

Server time is: 2024-11-24 04:25:53
Your offset time is: 2024-11-24 04:25:53