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 / Calculating new coordinates after moving in 3D space

Author
Message
Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 18th Dec 2010 12:37
This might have been asked before but I couldn't find any previous threads that answer this question properly.

I want to say: if we move forward by n distance in the direction we are facing what will be our new position?

I want to do this without using any commands, just using mathematics.

Currently I'm using Pythagoras and have the following formulae:
movementAlongX = distance * sin(angleY)
movementAlongZ = distance * cos(angleY)
movementAlongY=-distance * sin(angleX)

The problem with this is that movementAlongX and movementAlongZ are not dependent on the X angle e.g. if we are facing upwards we shouldn't move along the X or Z axis at all.

I haven't studied mathematics enough to know what the solution to this problem is and surprisingly I couldn't find the answer online.

Sven B
21
Years of Service
User Offline
Joined: 5th Jan 2005
Location: Belgium
Posted: 18th Dec 2010 12:51 Edited at: 18th Dec 2010 12:51
Something like this should do the trick:



Cheers!
Sven B

Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 24th Dec 2010 19:08 Edited at: 24th Dec 2010 19:08
Thanks - that works if the Z angle is always 0 but what if I want to take that into account too?

Matty H
17
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 24th Dec 2010 20:35
I don't think there is a simple solution using euler angles and trig functions. The problem is that the axis become equivilent to each other under certain conditions, causing gimble lock.

You may need to look into quaternions and matrices, I think anyway

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 24th Dec 2010 21:55 Edited at: 24th Dec 2010 21:57
You don't need to worry about quaternions. If you want to calculate everything out with matrices... well, you could do that, but it would be a waste of time!

The solution is this:
xmovement=dist*(cos(x)*sin(y)*cos(z)+sin(x)*sin(z))
ymovement=dist*(cos(x)*sin(y)*sin(z)-sin(x)*cos(z))
zmovement=dist*(cos(x)*cos(y))

You get that result by rotating first around the x axis, then the y axis, then the z axis, using rotation matrices. Then you multiply that by a forward column matrix/vector: [[0][0][1]].


However... I think thats a waste of time. What I usually do if I need to do what you're talking about, is add a limb to the object, hide it, and offset it by 0,0,1. Then, you take the limb position minus the object position, and multiply that vector by dist - you get the same results as in the above equations, and Its probably faster (uhh, actually it probably doesn't make much of a difference... I guess I'm not too sure).

[edit]
errm, It's a waste of time to use those equations if you have to multiply everything out yourself. Uhh... but I guess since you wouldn't have to derive anything, it isn't. Do whatever you wan't I guess, either solution workx xD

Michael P
20
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 25th Dec 2010 00:15
Thanks, that seems to work perfectly!

Matty H
17
Years of Service
User Offline
Joined: 7th Oct 2008
Location: England
Posted: 25th Dec 2010 00:35
Quote: "The solution is this:
xmovement=dist*(cos(x)*sin(y)*cos(z)+sin(x)*sin(z))
ymovement=dist*(cos(x)*sin(y)*sin(z)-sin(x)*cos(z))
zmovement=dist*(cos(x)*cos(y))"


Cool, so are you saying this always works?
I don't really understand 'gimble lock' much but I did read that you can't always rotate around all three axis using euler angles.
Something about if you rotate by 90 degrees about two axis then they become equivilent to the third axis

If that equation works for all combinations of angles then I have completely got the wrong end of the stick when it comes to euler angles. Either way, I'm keeping that equation as it will come in very useful, thanks

Neuro Fuzzy
19
Years of Service
User Offline
Joined: 11th Jun 2007
Location:
Posted: 25th Dec 2010 01:02
You only have to worry about gimbal lock if you're interpolating between values. Here, you have the euler angles - they stay the same, and they're not compared with any other set of angles, so you don't have to worry about any of that loss of freedom stuff. You're not changing any angle.

uhh anyways that equation isn't all that useful. What I would do, like I said, is...

1. create my object
2. create a limb mesh
3. add the limb to my object
4. hide the limb
5. offset the limb by <x,y,z>

when you want to move the object forwards (uhh, without using the "move object" command), take the limb position minus the object's position, and then add a multiple of that back to the object's position. (that multiple would be distance)

Login to post a reply

Server time is: 2026-07-21 20:35:19
Your offset time is: 2026-07-21 20:35:19