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 / Converting a direction vector to into degrees

Author
Message
Syncaidius
20
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 1st Sep 2010 14:55 Edited at: 1st Sep 2010 14:56
I've been stuck with this for a bit. I've been trying to think up a way to make a function that converts 3 direction values into 3 angle (degrees) values.

Say I have directional values of:


How would I convert those into values between 0 and 360 degrees?

I'm not so good when it comes to converting angle values/directions, hence why I am stuck.

Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 1st Sep 2010 15:05
Well, the Y angle is quite easy...

AngY#=ATANFULL(X,Z)

Then find out the distance of the vector, then there should be some way of using ATANFULL to work out the other angles, like ATANFULL(D,Y) where D is the distance of the XZ vector. I'm not sure if you need or could even work out Z angles, that's more like twisting the vector, and probably wouldn't affect things.

If this is for, say a rocket pointing in the right direction, that it's travelling in - then I would rotate it on Y to suit that AngY#, then pitch it up on X, maybe best done by rotating limb 0 instead of the object - but anyhoo that should have it pointing the right way. The Z angle as I said is like an extra foot that you might not need unless you want to spin the rocket on Z, which might be quite cool.

Health, Ammo, and bacon and eggs!
Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 1st Sep 2010 15:08
Ohh, one little thing I forgot to add. You could just cheat. If your moving an object by a stored amount - I'm guessing you are. You have the same old Z rotation problems, but you can simply work out the new position of the object, point the object towards that, then position the object, and it'll follow it's next location.

Health, Ammo, and bacon and eggs!
baxslash
Valued Member
Bronze Codemaster
18
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Sep 2010 15:18 Edited at: 1st Sep 2010 15:19
Quote: "You could just cheat. If your moving an object by a stored amount - I'm guessing you are. You have the same old Z rotation problems, but you can simply work out the new position of the object, point the object towards that, then position the object, and it'll follow it's next location."

That's more or less what I was thinking.

Here's a quick sub-routine where x#,y#,z# are your direction vectors and ax#,ay#,az# are your angles:


Might be flawed as I haven't tested it...

EDIT: Wasn't deleting the dummy object!

Neuro Fuzzy
17
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 1st Sep 2010 16:04 Edited at: 1st Sep 2010 16:04
I would say the above way is definitely the easiest, but because I'm interested, here's a solution without "point object". If you want to use the x and y angles with a z angle of zero to rotate something facing forward on the z axis towards a point, then, through various wishy washy, you get:
sin(yang)*cos(xang)=x
-sin(xang)=y
cos(yang)*cos(xang)=z

from which you can say
xang=asin(-y)
yang=sin(yang)*cos(xang)/(cos(yang)*cos(xang))=sin(yang)/cos(yang)~=atanfull(x,z).
so...
get_angles:
ax#=asin(-y#)
ay#=atanfull(x#,z#)
az#=0
return

Syncaidius
20
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 1st Sep 2010 16:47 Edited at: 1st Sep 2010 16:50
Thanks for the replies so far. I'm starting to get the idea but, this is still a little confusing

Okay so, if I had to use a command where the only way to rotate an object was by "pointing" it in a direction with a command such as SET OBJECT DIRECTION obj, 0.0, 0.0, 0.5, how would I calculate that as 3 angles?

So far what I've learnt from reading up around the internet is that an XYZ direction of 0.0, 0.0, 1.0 would be pointing forward from the camera's starting point 0,0,-1.0 would be pointing backwards. The same goes for up and down with -1 and 1 for Y and left and right for X, I'm just giving myself a headache with converting those to an angle.

I know I could do it the easy way as you guys mentioned, I'm just interested in how the hard way works, and I'm not so good with the whole trigonometry thing right now.

P.S:



Van B
Moderator
22
Years of Service
User Offline
Joined: 8th Oct 2002
Location: Sunnyvale
Posted: 1st Sep 2010 17:04
Quote: "Okay so, if I had to use a command where the only way to rotate an object was by "pointing" it in a direction with a command such as SET OBJECT DIRECTION obj, 0.0, 0.0, 0.5, how would I calculate that as 3 angles?"


Well, once you call that 'command', or rather POINT OBJECT, then the object itself holds the angles in OBJECT ANGLE X(Obj), Y and Z as well.

Might be worth explaining what you need it to do, there's a distinct easy method to do this which might be fine for your needs - otherwise the trig has to come out. There's no avoiding it sometimes, but DBPro does have a good few cheats. For instance, to work backwards and calculate the vector from an objects angles, just store the position, move the object forward by 1.0, then compare the positions and you have the movement vector, just remember to move it back again.

I think your right about the orientations, however what you need to remember is that with DBPro you are in eular angles, it's hellish, complicated - most people would turn to EZRotate to make things easier, some people prefer free flight rotation, some rotate objects an axis at a time, some (like me) prefer to rotate limbs. There's lots of options, some of them don't use a single bit of trig.

Health, Ammo, and bacon and eggs!
Syncaidius
20
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 1st Sep 2010 18:21
Ok, I'll try to explain a bit more. I've been using DGDK with an audio library that only has one function for setting the orientation of the listener. That function only takes values for the XYZ direction between -1.0 and 1.0.

I've already used the object trick to get the direction vector thats required for the function by:
Moving the object to the same position as the camera
Setting the object angles to the same as the camera
Moving the object foward by 1.0
Subtracting the camera position from the object position, which leaves me with my direction vector

The above works well but, what I probably badly explained earlier is that I'm trying to figure out the harder alternative, which doesn't involve using an object to calculate the direction vector.

Maybe the answer is probably starring me in the face, I don't know...

baxslash
Valued Member
Bronze Codemaster
18
Years of Service
User Offline
Joined: 26th Dec 2006
Location: Duffield
Posted: 1st Sep 2010 18:33
Quote: "The above works well but, what I probably badly explained earlier is that I'm trying to figure out the harder alternative, which doesn't involve using an object to calculate the direction vector.

Maybe the answer is probably starring me in the face, I don't know..."

I think the answer is not necessary... the question is: Why?

Why if the easy way works well are you trying to work out the hard way? If it's just for fun then I applaud you but it seems so unnecessary.

dark coder
22
Years of Service
User Offline
Joined: 6th Oct 2002
Location: Japan
Posted: 1st Sep 2010 19:54
Neuro Fuzzy has already given you the exact mathematical solution to this, given a unit direction vector.

Sixty Squares
18
Years of Service
User Offline
Joined: 7th Jun 2006
Location: Somewhere in the world
Posted: 1st Sep 2010 19:56 Edited at: 1st Sep 2010 19:59
You could always try the Dot Product between two vectors to get the angle. Your first vector could be the direction you presume to be 0 degrees
and your second vector could be the one you have now. Let's call these two vectors a and b.

The dot product of ab is: ab = |a||b|cos(angle between)

|a| is the length of vector a, and |b| is the length of vector b. But let's not worry about that. If you normalize both vectors, the length of each will be 1. So the equation is just:

dot product of a and b = cos(angle between)

So, inverse cosine( dot product of a and b ) is the angle you're looking for.

In short:
acos(ab)=angle

It's untested, but it should work...

EDIT: WAIT, this might not work on a 3D vector! You can try it out and see though...


Guns, cinematics, stealth, items and more!
Sven B
20
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 1st Sep 2010 23:55 Edited at: 1st Sep 2010 23:55
Quote: "EDIT: WAIT, this might not work on a 3D vector! You can try it out and see though..."


It does, it is often used for 3D vectors. It always works.
The dot product is also used for things like projection on a vector or something along those lines. It is very useful for finding the coordinates of a vector in arbitrary axises.

I'm not sure about the term in English, but when you project (calculate the dot product) of a unit vector on each axis vector, we call it the direction cosines of a vector (own translation, didn't look it up).

Everything else is just as you said .

Cheers!
Sven B

Syncaidius
20
Years of Service
User Offline
Joined: 22nd Mar 2005
Location: United Kingdom
Posted: 8th Sep 2010 16:54 Edited at: 8th Sep 2010 16:55
Thanks for the help guys. After giving myself a few internet maths lessons with this sort of stuff, combined with your help, i've got it figured and doing what I wanted it to do.

Neuro Fuzzy had already handed the answer to me on a plate, it just looked complete jibberish to me at the time.

Green Gandalf
VIP Member
20
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 8th Sep 2010 17:23
Quote: "Neuro Fuzzy had already handed the answer to me on a plate, it just looked complete jibberish to me at the time."


Maths is like that at times - but it's always worth getting to grips with it in the end.

Login to post a reply

Server time is: 2025-05-11 01:57:42
Your offset time is: 2025-05-11 01:57:42