# Rotation With Mouse

Hi community

I want to create a rotation with mouse, this code allow calculate angles

``````
private Vector2f calcularAnguloX()
{
Vector2f posicionMouse = inputManager.getCursorPosition();

Vector3f click3d = cam.getWorldCoordinates(posicionMouse, 0f).clone();
Vector3f vectorPuntero = click3d.subtract(pocicionCerca); //Vector click mouse
Vector3f vectorProyXZ = new Vector3f(vectorPuntero.x, 0, vectorPuntero.z); //Projection vector1
Vector3f vectorProyYZ = new Vector3f(0, vectorPuntero.y, vectorPuntero.z); //Projection vector2

Vector3f vectorCentroCam = cam.getWorldCoordinates(
new Vector2f(
(this.getCamera().getWidth() / 2),
(this.getCamera().getHeight() / 2)), 0f).clone(); //vector camera

Vector3f vectorCentro = vectorCentroCam.subtract(pocicionCerca).normalize();

float angleAxeY = vectorCentro.angleBetween(vectorProyXZ.normalize()); //calculate angle in y
float angleAxeX = vectorCentro.angleBetween(vectorProyYZ.normalize()); //calculate angle in x

//        if (posicionMouse.getX() > (this.getCamera().getWidth() / 2))
//        {
//        }
if (posicionMouse.getY() > (this.getCamera().getHeight() / 2))
{
angleAxeX = (float) ((2*Math.PI) - angleAxeX); // angle for rotation in Y... this value is aplicate a quaternion in unitary vector in Y

}
System.out.println("posicion y "+posicionMouse.getY());
return new Vector2f(angleAxeY, angleAxeX);
}
``````

the result is here video

the angle is very small why?
Thanks

It took me a long time to decipher your math. You may be getting a lot of error rate by trying to find the angle between two world coordinates that lie right on the near plane. Teeny tiny values.

There is most certainly a better way to do what you are trying to do but it’s hard to tell what’s on purpose and what’s on accident. Can you take a step back and explain what you are really trying to accomplish and what the values are used for?

As a general rule, angleBetween() is almost never needed. I’ve certainly never seen it used except cases where it was unnecessary.

do rotation with keyboard first
make arrows do what you want, then you can easily convert it into mouse rotation

try use spatial.rotate(x, y, z); actionlistener and analoglistener that will give you “value” which can be used to represent those axes

you really need to go through all tutorials… you are doing simple 1 + 1 by using unnecessary math… you then should look at flybycamera used in those tutorials… you can adapt it to what you want

Thanks… hi pspeed

The idea is to generate a rotation of the object by simply moving the mouse. If I move the mause in containing the coordinates of Y (up, down) rotate the object in that direction and the same on the X axis

I’ll try to do what you tell me Ascaria
Thanks