# [Solved] Spatial moving at strange angles

'Ello! I’ve got quite an annoying problem. And the problem is that a certain spatial doesn’t move straight… At least, after rotating a bit it starts to move at strange angles. Look, here’s the onAnalog method that moves and rotates the player (the mentioned spatial) according to the input:

[java]

if (name.equals(“Down”))

{

float playerRotation = getRootNode().getChild(“Player”).getLocalRotation().toAngleAxis(new Vector3f(0, 1, 0));

float playerCurrentMaximumSpeed = ((Creature) getRootNode().getChild(“Player”)).getCurrentMaximumSpeed();

getRootNode().getChild(“Player”).move(value * (float) Math.cos(playerRotation) * playerCurrentMaximumSpeed, 0.0f, value * (float) Math.sin(playerRotation) * playerCurrentMaximumSpeed);

} // end if

if (name.equals(“Up”))

{

float playerRotation = getRootNode().getChild(“Player”).getLocalRotation().toAngleAxis(new Vector3f(0, 1, 0));

float playerCurrentMaximumSpeed = ((Creature) getRootNode().getChild(“Player”)).getCurrentMaximumSpeed();

getRootNode().getChild(“Player”).move(-value * (float) Math.cos(playerRotation) * playerCurrentMaximumSpeed, 0.0f, -value * (float) Math.sin(playerRotation) * playerCurrentMaximumSpeed);

} // end if

if (name.equals(“TurnRight”))

{

getRootNode().getChild(“Player”).rotate(0.0f, -value * getSettings().getSetting(“mouse-sensitivity”).toFloat(), 0.0f);

} // end if

if (name.equals(“TurnLeft”))

{

getRootNode().getChild(“Player”).rotate(0.0f, value * getSettings().getSetting(“mouse-sensitivity”).toFloat(), 0.0f);

} // end if

[/java]

The most irritating thing is that I’ve been having exactly the same problem even when I was using pure OpenGL.

Lucas

@glaucomardano said:
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:scenegraph_for_dummies
https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:math_for_dummies

Well, I didn't find anything there that could help me to solve my problem.

:facepalm:

For one thing, toAngleAxis() does not work like you are using it:

1 Like
@pspeed said:
For one thing, toAngleAxis() does not work like you are using it:

So, how do I use it properly?
The axis is specified as a parameter, and it returns angle in radians. So what's wrong? Does it alter the rotation quaternion?

``` getRootNode().getChild(“Player”).move(value * (float) Math.cos(playerRotation) * playerCurrentMaximumSpeed, 0.0f, value * (float) Math.sin(playerRotation) * playerCurrentMaximumSpeed); ```

what is that supposed to do?

@nehon said:
`
getRootNode().getChild("Player").move(value * (float) Math.cos(playerRotation) * playerCurrentMaximumSpeed, 0.0f, value * (float) Math.sin(playerRotation) * playerCurrentMaximumSpeed);
`
what is that supposed to do?

It moves the player according to its rotation. It calculates (at least, should calculate) the appropriate x and z movement values.

hmmm ok,

why don’t you use the direction of the spatial?

try this

[java]

Node player = getRootNode().getChild(“Player”)

Vector3f dir= player.getWorldRotation().getRotationColumn(2).normalizeLocal(); // this returns the direction of the spatial

player.move( dir.mult(value).multLocal(playerCurrentMaximumSpeed));

[/java]

Using cos and sin maybe drive you to a gimbal lock situation at some point messing up the direction (that’s only a supposition, though, my Math skill is not the best in the world…)

1 Like
@nehon said:
hmmm ok,
why don't you use the direction of the spatial?
try this
[java]
Node player = getRootNode().getChild("Player")
Vector3f dir= player.getWorldRotation().getRotationColumn(2).normalizeLocal(); // this returns the direction of the spatial
player.move( dir.mult(value).multLocal(playerCurrentMaximumSpeed));
[/java]

Oh, that's it! That solved the problem. Many thanks.
@lucaseasedup said:
So, how do I use it properly?
The axis is specified as a parameter, and it returns angle in radians. So what's wrong? Does it alter the rotation quaternion?

Did you even read the javadoc? The vector supplied as the axis is for STORING the axis of rotation... not specifying it. That method is giving you the axis and the rotation that fully represents that rotation. You cannot supply an arbitrary axis because it doesn't make sense.