Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualTheChubu

Posted 01 February 2013 - 12:51 PM

Gah, I'm still having issues. Translation works ok but rotations work awkwardly. Like the camera isn't rotating on itself but around some point elsewhere. And it seems I can only rotate around a limited arc, like 60ºC left and 60ºC right.

 

This is my lookAt function. I use DefaultMatrixFloat for both matrices and vectors. eyePos, target and such are treated as column vectors with 3 elements.

 

 

    public static DefaultMatrixFloat lookAt ( DefaultMatrixFloat eyePos, DefaultMatrixFloat target,  DefaultMatrixFloat up)
    {
 
        DefaultMatrixFloat zAxis = UtilsMath.substractMatrix(target,eyePos);
        UtilsMath.normalize3f(zAxis);
        
        DefaultMatrixFloat xAxis = UtilsMath.crossProduct3f(up, zAxis);
        UtilsMath.normalize3f(xAxis);
        
        DefaultMatrixFloat yAxis = UtilsMath.crossProduct3f(zAxis, xAxis);
        UtilsMath.normalize3f(yAxis);
//       This is emulating GLM's lookAt function. Reverses cross products. Has the same issues.
//        DefaultMatrixFloat zAxis = UtilsMath.substractMatrix(target,eyePos);
//        UtilsMath.normalize3f(zAxis);
//        
//        DefaultMatrixFloat xAxis = UtilsMath.crossProduct3f(zAxis,up);
//        UtilsMath.normalize3f(xAxis);
//        
//        DefaultMatrixFloat yAxis = UtilsMath.crossProduct3f(xAxis,zAxis);
//        UtilsMath.normalize3f(yAxis);       
        //4x4 matrix.
        DefaultMatrixFloat     transform = new DefaultMatrixFloat(4,4);
        transform.setIdentity();
 
        //setValueAt takes (row,column,value) as parameters.
        //getValueAt takes (row,column) and returns the float in that position.
 
        transform.setValueAt(0, 0, xAxis.getValueAt(0, 0)); 
        transform.setValueAt(0, 1, yAxis.getValueAt(0, 0)); 
        transform.setValueAt(0, 2, zAxis.getValueAt(0, 0)); 
        transform.setValueAt(1, 0, xAxis.getValueAt(1, 0)); 
        transform.setValueAt(1, 1, yAxis.getValueAt(1, 0)); 
        transform.setValueAt(1, 2, zAxis.getValueAt(1, 0)); 
        transform.setValueAt(2, 0, xAxis.getValueAt(2, 0));
        transform.setValueAt(2, 1, yAxis.getValueAt(2, 0));
        transform.setValueAt(2, 2, zAxis.getValueAt(2, 0));
        transform.setValueAt(0, 3, - dotProduct3f(xAxis, eyePos));
        transform.setValueAt(1, 3, - dotProduct3f(yAxis, eyePos));
        transform.setValueAt(2, 3, - dotProduct3f(zAxis, eyePos));
//      Also tried doing the dotProduct in the last row like GLM's implementation but it warps everything.
//      transform.setValueAt(3, 0, - dotProduct3f(xAxis, eyePos));
//        transform.setValueAt(3, 1, - dotProduct3f(yAxis, eyePos));
//        transform.setValueAt(3,2, - dotProduct3f(zAxis, eyePos));
        
        return transform;
        
    }

And this is how I get input from the user:

    public void pollInput ( ZGLClass glObject )
    {
        float posOff = 5.0f;
        float viewOff = 0.2f;
        
        //eyePos,target,up are 3 element column vectors.
        //up vector is defined as (0,1,0).
        
        //Translations. Affect both eyePos and target vectors by the same offset.
        if (Keyboard.isKeyDown(Keyboard.KEY_S))
        {
            zPos += posOff;
            zView += posOff;
            this.eyePos.setValueAt(2, 0, zPos);
            this.target.setValueAt(2, 0, zView);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_W))
        {
            zPos -= posOff;
            zView -= posOff;
            this.eyePos.setValueAt(2, 0, zPos);
            this.target.setValueAt(2, 0, zView);
        }
        //Looking right/left only modify the target vector, not the position of the eye.
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
        {
            xView += viewOff;
            this.target.setValueAt(0, 0, xView);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
        {
            xView -= viewOff;
            this.target.setValueAt(0, 0, xView);
        }
        //"rotationMatrix" is actually the view matrix. I just hijacked the variable for now.
        //updateViewMatrix updates the related "rottionMatrix" uniform where the viewMatrix is saved.
        glObject.rotationMatrix = UtilsMath.lookAt(eyePos, target, up);
        glObject.updateViewMatrix();

        }

There are more ifs for translations along X (xPos) and Y (yPos), and up/down rotation, they work the same. Adding an offset and setting it up on the eyePos/target vector.

 

I have no idea where I'm going wrong.


#1TheChubu

Posted 01 February 2013 - 12:37 PM

Gah, I'm still having issues. Translation works ok but rotations work awkwardly. Like the camera isn't rotating on itself but around some point elsewhere. And it seems I can only rotate around a limited arc, like 60ºC left and 60ºC right.

 

This is my lookAt function. I use DefaultMatrixFloat for both matrices and vectors. eyePos, target and such are treated as column vectors with 3 elements.

 

    public static DefaultMatrixFloat lookAt ( DefaultMatrixFloat eyePos, DefaultMatrixFloat target,  DefaultMatrixFloat up)
    {
 
        DefaultMatrixFloat zAxis = UtilsMath.substractMatrix(target,eyePos);
        UtilsMath.normalize3f(zAxis);
        
        DefaultMatrixFloat xAxis = UtilsMath.crossProduct3f(up, zAxis);
        UtilsMath.normalize3f(xAxis);
        
        DefaultMatrixFloat yAxis = UtilsMath.crossProduct3f(zAxis, xAxis);
        UtilsMath.normalize3f(yAxis);
//       This is emulating GLM's lookAt function. Reverses cross products. Has the same issues.
//        DefaultMatrixFloat zAxis = UtilsMath.substractMatrix(target,eyePos);
//        UtilsMath.normalize3f(zAxis);
//        
//        DefaultMatrixFloat xAxis = UtilsMath.crossProduct3f(zAxis,up);
//        UtilsMath.normalize3f(xAxis);
//        
//        DefaultMatrixFloat yAxis = UtilsMath.crossProduct3f(xAxis,zAxis);
//        UtilsMath.normalize3f(yAxis);       
        //4x4 matrix.
        DefaultMatrixFloat     transform = new DefaultMatrixFloat(4,4);
        transform.setIdentity();
 
        //setValueAt takes (row,column,value) as parameters.
        //getValueAt takes (row,column) and returns the float in that position.
 
        transform.setValueAt(0, 0, xAxis.getValueAt(0, 0)); 
        transform.setValueAt(0, 1, yAxis.getValueAt(0, 0)); 
        transform.setValueAt(0, 2, zAxis.getValueAt(0, 0)); 
        transform.setValueAt(1, 0, xAxis.getValueAt(1, 0)); 
        transform.setValueAt(1, 1, yAxis.getValueAt(1, 0)); 
        transform.setValueAt(1, 2, zAxis.getValueAt(1, 0)); 
        transform.setValueAt(2, 0, xAxis.getValueAt(2, 0));
        transform.setValueAt(2, 1, yAxis.getValueAt(2, 0));
        transform.setValueAt(2, 2, zAxis.getValueAt(2, 0));
        transform.setValueAt(0, 3, - dotProduct3f(xAxis, eyePos));
        transform.setValueAt(1, 3, - dotProduct3f(yAxis, eyePos));
        transform.setValueAt(2, 3, - dotProduct3f(zAxis, eyePos));
//      Also tried doing the dotProduct in the last row like GLM's implementation but it warps everything.
//      transform.setValueAt(3, 0, - dotProduct3f(xAxis, eyePos));
//        transform.setValueAt(3, 1, - dotProduct3f(yAxis, eyePos));
//        transform.setValueAt(3,2, - dotProduct3f(zAxis, eyePos));
        
        return transform;
        
    }
And this is how I get input from the user:
    public void pollInput ( ZGLClass glObject )
    {
        float posOff = 5.0f;
        float viewOff = 0.2f;
        
        //eyePos,target,up are 3 element column vectors.
        //up vector is defined as (0,1,0).
        
        //Translations. Affect both eyePos and target vectors by the same offset.
        if (Keyboard.isKeyDown(Keyboard.KEY_S))
        {
            zPos += posOff;
            zView += posOff;
            this.eyePos.setValueAt(2, 0, zPos);
            this.target.setValueAt(2, 0, zView);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_W))
        {
            zPos -= posOff;
            zView -= posOff;
            this.eyePos.setValueAt(2, 0, zPos);
            this.target.setValueAt(2, 0, zView);
        }
        //Looking right/left only modify the target vector, not the position of the eye.
        if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT))
        {
            xView += viewOff;
            this.target.setValueAt(0, 0, xView);
        }
        if (Keyboard.isKeyDown(Keyboard.KEY_LEFT))
        {
            xView -= viewOff;
            this.target.setValueAt(0, 0, xView);
        }

        glObject.rotationMatrix = UtilsMath.lookAt(eyePos, target, up);
        glObject.updateViewMatrix();

        }
There are more ifs for translations along X (xPos) and Y (yPos), and up/down rotation, they work the same. Adding an offset and setting it up on the eyePos/target vector.

 

I have no idea where I'm going wrong.


PARTNERS