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 Professional Discussion / What would be the formula for calculating an angle...

Author
Message
orv
22
Years of Service
User Offline
Joined: 9th Sep 2003
Location: Chicago, IL
Posted: 6th Oct 2003 16:39
if anyone could help with a formula for the following:

angle = lineAngle(x1,z1,x2,z2)

angle would be in degrees.

imagine you looking down from above at a line/road that goes from
x1,z1 to x2,z2.

based on the 3d world of DBP, I want to know the angle.

Any takers?!
APEXnow
Retired Moderator
23
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 6th Oct 2003 16:56
orv, you'll need to play with this a little but it goes something like this.

See source button, again, I just did this from my head but give it a whirl anyway.


APEXnow
Retired Moderator
23
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 6th Oct 2003 16:59
Oops, small typo, try this.


JamesBlond
23
Years of Service
User Offline
Joined: 27th Aug 2002
Location: Germany
Posted: 6th Oct 2003 17:19
angle#=atanfull(x2-x1,z2-z1)

Smile and be happy, things could be worse!
So I smiled and was happy, and things got worse...
APEXnow
Retired Moderator
23
Years of Service
User Offline
Joined: 15th Apr 2003
Location: On a park bench
Posted: 6th Oct 2003 18:31
I didn't even know that function existed LOL


orv
22
Years of Service
User Offline
Joined: 9th Sep 2003
Location: Chicago, IL
Posted: 7th Oct 2003 04:28
@JamesBlond

cool little bit of code, athough it breaks when the angle of the line is 180 or greater. Fortunately for me, I don't need any angles beyond that (I think)

thanks guys for the help!
orv
22
Years of Service
User Offline
Joined: 9th Sep 2003
Location: Chicago, IL
Posted: 7th Oct 2003 04:29
oops!

forgot to mention that in the post above, i included a test program (click Source btn) to validate the function.
Bulleyes
23
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Cyberjaya, Malaysia
Posted: 7th Oct 2003 05:38
@ORV

it doesn't break when the angle goes beyond 180, negative angles are just another way of representing angles in trigonometry. Positive angles are calculated on counter clockwise, and negative angles are clockwise. For instance, Another way of saying 350 degrees is -10 degress.

Does that make sense to you?

Bad Nose Entertainment - Where games are forged from the flames of talent and passion.

http://www.badnose.com/
Bulleyes
23
Years of Service
User Offline
Joined: 3rd Nov 2002
Location: Cyberjaya, Malaysia
Posted: 7th Oct 2003 05:40
Oh, BTW, you always clamp the angle between 0 and 359 degress by using WRAPVALUE().

Bad Nose Entertainment - Where games are forged from the flames of talent and passion.

http://www.badnose.com/
nuclear glory
22
Years of Service
User Offline
Joined: 2nd Oct 2003
Location:
Posted: 7th Oct 2003 06:01 Edited at: 7th Oct 2003 06:02
Another way would be to use vector math. You can define two 3D vectors.

This might not be as useful for top-down flat 2D stuff, the above posts would be better, but for getting angles in 3D:

(this is just basically pseudo code to demonstrate the math)
(these are 3D vectors btw)

vec1 = line1_end - line1_start
vec2 = line2_end - line2_start

Then you would normalize these vectors.

vec1 = vec1 / vec1_length
vec2 = vec2 / vec2_length

Then you take the Dot Product of the two normals, which gives you the cos(Angle) between them.

cosAngle# = DotProduct(vec1,vec2)

Now, to get our final angle, we take the arc cosine value like this:

Angle# = aCos(cosAngle#)

---
This example gives the absolute degrees between two vectors. And could be used in the above question with the first vector pointing "forward" in the world. Then that would give you the degrees from this forward vector.

Someone might have use for this anyhow
DrakeX
23
Years of Service
User Offline
Joined: 26th Aug 2002
Location:
Posted: 7th Oct 2003 07:09
i personally think the ATANFULL() is easier

stop looking at me!
nuclear glory
22
Years of Service
User Offline
Joined: 2nd Oct 2003
Location:
Posted: 7th Oct 2003 07:13 Edited at: 7th Oct 2003 07:18
Yes, this is true.

But, can it calculate the angle across all 3 dimensions at the same time?

I'm only seeing the two axis (x/z) with atanfull above. I know, he only requested two axis in the original post. But that's cool
orv
22
Years of Service
User Offline
Joined: 9th Sep 2003
Location: Chicago, IL
Posted: 7th Oct 2003 17:26
@Bulleyes

the WRAPVALUE() function was just what the doctor ordered!

thanks!

Login to post a reply

Server time is: 2026-07-25 07:02:23
Your offset time is: 2026-07-25 07:02:23