Rotate model with mouse

I have model in vertical position.

I need to rotate model turn about axis, when mouse move left or right. (by left right axis)

My try…

  1. get camera
  2. create quaternion.
  3. rotate.

Quaternion localRotation = new Quaternion(-0.173f, camRotationY, -0.002364515f, 0.9830f);

characterModel.setLocalRotation(localRotation);

Work if i move mouse horizontal.
Not work if i move mose by X Z

How to rotate model only by mouse Y axis

1 Like

I nee convet vector to qiaterinion?

1 Like

I think, looking at your trial, that you have no clue what a quaternion is. It is just simply a compact form to store a rotation. The values of a quaternion have no meaning in the sense you think. I would read the math for dummies if I were you, the link is in the head of this forum…
try fromAngles method of Quaternion object and place there the y rotation in radians…

1 Like

There is some other way

  1. take the coordinates of the rotation of the mouse along the Y axis
  2. Degree Rotor
  3. model by model for this degree
1 Like

Its just maths really…

https://www.mathsisfun.com/geometry/radians.html

1 Like

But vector in 3D space.

  1. get cam direction (vector) and projection of the vector onto the plane (new vector)
  2. rotate
  3. get cam direction (vector) and projection of the vector onto the plane (new vector) 2
  4. get angle
  5. rotate model

I did not get this way…

1 Like
  1. translate vector 3d to simple vector
  2. rotate
  3. get new vector 3d translate to simple vector
  4. get angle

vector1 (z1,x1)
vector2(z2,x2)

Vmod = z1x1 + z2x2

V1 = sqrt(z1z1 + x1x1)

V2 = sqrt(z2z2 + x2x2)

cos = Vmod/V1*V1

cos to radians

1 Like

http://javadoc.jmonkeyengine.org/com/jme3/math/Quaternion.html#fromAngles-float-float-float-

Or…

http://javadoc.jmonkeyengine.org/com/jme3/scene/Spatial.html#rotate-float-float-float-

1 Like

I have varibles:

camRotation.getX()
camRotation.getY()
camRotation.getZ()
camRotation.getW()
            
camDir.getX();
camDir.getY()
camDir.getZ()
            
camLeft.getX()
camLeft.getY()
camLeft.getZ()

can i rotate model with this variables?

        characterModel.rotate(0.0f, camRotationY, 0.0f);
1 Like

Quaternion values are magic. Do not try to interpret meaning from them.
Quaternion values are magic. Do not try to interpret meaning from them.
Quaternion values are magic. Do not try to interpret meaning from them.
Quaternion values are magic. Do not try to interpret meaning from them.
Quaternion values are magic. Do not try to interpret meaning from them.

Repeat as needed.

But since you don’t know where the javadoc is… I’m not sure I want to be your google.

1 Like

Method

/**
 * Retun radian angle between two 2 vectors (get from Vector 3d)
 * @param x1
 * @param y1
 * @param x2
 * @param y2
 * @return 
 */
public float getRotation(float x1,float y1,float x2,float y2){
    float Vmod = x1*y1 + x2*y2;
    float V1   = (float) Math.sqrt(x1*x1 + y1*y1);
    float V2   = (float) Math.sqrt(x2*x2 + y2*y2);
    
    float cos  =  Vmod/V1*V2;
    
    float radian = (float) (cos *  Math.PI / 180);
    
    return radian;
}
1 Like

mySpatial =…

float moustMovedLeftRight = …

mySpatial.rotate(0, moustMovedLeftRight * turnSpeed, 0);

1 Like

I need get angles for character model from camera rotation and direction.

float moustMovedLeftRight = …

angle… but witch formula

1 Like

float mouseMovedLeftRight = mousePosition.x - lastMousePosition.x

I think one of two things are happening:

  1. you are making this WAY more complicated than it needs to be.
  2. you have not provided enough information.

I know for a fact that number (2) is true. I don’t know about number (1), though.

1 Like

I have variables this is camera vectors.

and

camLeftOld = camLeft;

        camRotation = new Quaternion(camRotationX, camRotationY, camRotationZ, camRotationW);
        camDir      = new Vector3f(camDirectionX, camDirectionY, camDirectionZ);
        camLeft     = new Vector3f(camLeftX, camLeftY, camLeftZ);

and this

characterModel.rotate(0.0f, camRotationY, 0.0f);

1 Like

not mouse move

character model rotation by camera (mouse)

if i oldMouse.x - newMuse.x = number

i need angle between vectors

1 Like

I think

float[] camAngles = camRotation.toAngles(null);
            
            float rotationY = camAngles[1];

            //localRotation.multL
            
            characterModel.rotate(0.0f, rotationY, 0.0f);
1 Like

We don’t know what you are trying to do or why you have to go through 20 strange steps to do it.

Maybe you are trying to reuse too much. It seems really strange to me to use camera movement to rotate your spatial when you could just hook it up directly and avoid the 20 different math steps between you and the mouse.

1 Like

i need rotate model with camera left right move.

rotate variables:

  1. Quaternion
  2. angles
1 Like

Work this
float[] camAngles = camRotation.toAngles(null);

        float rotationY = camAngles[1];
        
        float rotateCharacter = rotationYOld - rotationY;
        
        rotationYOld = rotationY;
        

        //Quaternion localRotation = new Quaternion(-0.173f, rotationY, -0.002364515f, 0.9830f);

        //localRotation.multL
        
        
        characterModel.rotate(0.0f, rotateCharacter, 0.0f);

But character must be rotate with camera direction

1 Like