1
votes

Hi I am attempting to build OBB's into my 3d java game using lwjgl. Currently I am just attempting to rotate the OBB around using matrix4f's and testing it by rendering the points. So, when I render it, with its xyx=0,0,0 and its angle on the x axis =1 it will rotate fine. But when i move the y axis up say y=5 the rotation will no longer go around the center.

I tried fixing this with translation but It doesnt work. Im also wondering if there is a way to access opengl's push/pop and rotate methods to get those variables for my points because opengl rotate does it perfectly.

This is my OBB class:

public OBB(float x, float y, float z, float angleX, float angleY, float angleZ, float sizeX, float sizeY, float sizeZ){ this.x=x; this.y=y; this.z=z;

    this.angleX=angleX;
    this.angleY=angleY;
    this.angleZ=angleZ;

    this.sizeX=sizeX;
    this.sizeY=sizeY;
    this.sizeZ=sizeZ;

    posUBR = new Vector3f(x-sizeX,y+sizeY,z+sizeZ);//UpperBackRight
    posUBL = new Vector3f(x-sizeX,y+sizeY,z-sizeZ);//UpperBackLeft
    posUFL = new Vector3f(x+sizeX,y+sizeY,z-sizeZ);//UpperForLeft
    posUFR = new Vector3f(x+sizeX,y+sizeY,z+sizeZ);//UpperForRight

    posLBR = new Vector3f(x-sizeX,y-sizeY,z+sizeZ);//LowerBackRight
    posLBL = new Vector3f(x-sizeX,y-sizeY,z-sizeZ);//LowerBackLeft
    posLFL = new Vector3f(x+sizeX,y-sizeY,z-sizeZ);//LowerForLeft
    posLFR = new Vector3f(x+sizeX,y-sizeY,z+sizeZ);//LowerForRight

    posUBR=rotMat(posUBR);
    posUBL=rotMat(posUBL);
    posUFL=rotMat(posUFL);
    posUFR=rotMat(posUFR);

    posLBR=rotMat(posLBR);
    posLBL=rotMat(posLBL);
    posLFL=rotMat(posLFL);
    posLFR=rotMat(posLFR);

}

This is my rotation method:

public Vector3f rotMatrix(Vector3f point) { Matrix4f rotationMatrix = new Matrix4f();

       rotationMatrix.m00 = point.x;
       rotationMatrix.m10 = point.y;
       rotationMatrix.m20 = point.z;       

       rotationMatrix.translate(new Vector3f(-x,-y,-z));

       rotationMatrix.rotate(angleX,new Vector3f(1,0,0));
       rotationMatrix.rotate(angleY,new Vector3f(0,1,0));
       rotationMatrix.rotate(angleZ,new Vector3f(0,0,1));

       rotationMatrix.translate(new Vector3f(x,y,-z));


       return new Vector3f(rotationMatrix.m00, rotationMatrix.m10, rotationMatrix.m20);
}

public void rotate(){
    posUBR=rotMatrix(posUBR);
    posUBL=rotMatrix(posUBL);
    posUFL=rotMatrix(posUFL);
    posUFR=rotMatrix(posUFR);

    posLBR=rotMatrix(posLBR);
    posLBL=rotMatrix(posLBL);
    posLFL=rotMatrix(posLFL);
    posLFR=rotMatrix(posLFR);
}

My render function is a bit long to put in here but it basically renders a cube.

1
Sorry, I feel so dumb right now, all I needed to do was run this in my rotate function: public void setToOrigin(){ posUBR = new Vector3f(0-sizeX,0+sizeY,0+sizeZ); posUBL = new Vector3f(0-sizeX,0+sizeY,0-sizeZ); posUFL = new Vector3f(0+sizeX,0+sizeY,0-sizeZ); posUFR = new Vector3f(0+sizeX,0+sizeY,0+sizeZ); posLBR = new Vector3f(0-sizeX,0-sizeY,0+sizeZ); posLBL = new Vector3f(0-sizeX,0-sizeY,0-sizeZ); posLFL = new Vector3f(0+sizeX,0-sizeY,0-sizeZ); posLFR = new Vector3f(0+sizeX,0-sizeY,0+sizeZ); }HelpMehIDonKnowLinearAlgebra

1 Answers

0
votes

Sorry all I needed to do was this set to origin function:

public void setToOrigin(){
    posUBR = new Vector3f(0-sizeX,0+sizeY,0+sizeZ);
    posUBL = new Vector3f(0-sizeX,0+sizeY,0-sizeZ);
    posUFL = new Vector3f(0+sizeX,0+sizeY,0-sizeZ);
    posUFR = new Vector3f(0+sizeX,0+sizeY,0+sizeZ);

    posLBR = new Vector3f(0-sizeX,0-sizeY,0+sizeZ);
    posLBL = new Vector3f(0-sizeX,0-sizeY,0-sizeZ);
    posLFL = new Vector3f(0+sizeX,0-sizeY,0-sizeZ);
    posLFR = new Vector3f(0+sizeX,0-sizeY,0+sizeZ);
}