java - Why is my rotation matrix giving an extra translation? -


a couple weeks ago decided make own simple 3d game engine. i've followed couple tutorials online, , looked @ many videos, , far, i've gotten pretty engine. have 1 problem. when rotate or scale object, seems center top-left corner of screen instead of real center. example, when rotate object, rotates around left edge of screen, , when scale it, scales towards corner of screen or away corner of screen. here's code:

mesh class

package com.cub.cubefront.mesh;  import com.cub.cubefront.math.matrix4f; import com.cub.cubefront.math.vector3f;  public class mesh {      private vector3f[] verts;     private vector3f center;      private double rotx = 0;     private double roty = 0;     private double rotz = 0;      private double scalex = 1;     private double scaley = 1;     private double scalez = 1;      private double translatex = 0;     private double translatey = 0;     private double translatez = 0;  public mesh(int arg0) {     verts = new vector3f[arg0]; }  public vector3f getvertexat(int arg0) {     return verts[arg0 - 1]; }  public vector3f[] getverticies() {     return verts; }  public int getverticiescount() {     return verts.length; }  public void setvertexat(int arg0, vector3f arg1) {     verts[arg0 - 1] = arg1; }  public void setrotationpoint(vector3f arg0) {     this.center = arg0; }  public vector3f getrotationpoint() {     return center; }  public vector3f getcenter() {     int arg0 = verts.length;     double centerx = 0;     double centery = 0;     double centerz = 0;     (int = 0; < arg0; i++) {         centerx += verts[i].getx();         centery += verts[i].gety();         centerz += verts[i].getz();     }     return new vector3f((float)(centerx / arg0), (float)(centery / arg0), (float)(centerz / arg0)); }  public void rotatex(double arg0) {     this.rotx += math.toradians(arg0); }  public void setrotationx(double arg0) {     this.rotx = math.toradians(arg0); }  public matrix4f getxrotationasmatrix() {     return new matrix4f(new double[][] { // yz rotation matrix (x)         { 1, 0, 0, 0 },         { 0, math.cos(rotx), math.sin(rotx), 0 },         { 0, -math.sin(rotx), math.cos(rotx), 0 },         { 0, 0, 0, 1 }     }); }  public matrix4f getzrotationasmatrix() {     return new matrix4f(new double[][] { // xy rotation matrix (z)         { math.cos(rotz), -math.sin(rotz), 0, 0 },         { math.sin(rotz), math.cos(rotz), 0, 0 },         { 0, 0, 1, 0 },         { 0, 0, 0, 1 }     }); }  public void rotatey(double arg0) {     this.roty += math.toradians(arg0); }  public void setrotationy(double arg0) {     this.roty = math.toradians(arg0); }  public matrix4f getyrotationasmatrix() {     return new matrix4f(new double[][] { // xz rotation matrix (y)         { math.cos(roty), 0, math.sin(roty), 0 },         { 0, 1, 0, 0 },         { -math.sin(roty), 0, math.cos(roty), 0},         { 0, 0, 0, 1 }     }); }  public void setrotationz(double arg0) {     this.rotz = math.toradians(arg0); }  public void rotatez(double arg0) {     this.rotz += math.toradians(arg0); }  public matrix4f getrotation() {     return getzrotationasmatrix().multiply(getxrotationasmatrix()).multiply(getyrotationasmatrix()); }  public void setscalex(double arg0) {     this.scalex = arg0; }  public void scalex(double arg0) {     this.scalex += arg0; }  public double getscalex() {     return scalex; }  public void setscaley(double arg0) {     this.scaley = arg0; }  public void scaley(double arg0) {     this.scaley += arg0; }  public double getscaley() {     return scaley; }  public void setscalez(double arg0) {     this.scalez = arg0; }  public void scalez(double arg0) {     this.scalez += arg0; }  public double getscalez() {     return scalez; }  public void setscale(double arg0) {     setscalex(arg0);     setscaley(arg0);     setscalez(arg0); }  public void setscale(double[][] arg0) {     this.scalex = arg0[0][0];     this.scaley = arg0[1][1];     this.scalez = arg0[2][2]; }  public void setscale(matrix4f arg0) {     this.scalex = arg0.getvalueat(0, 0);     this.scaley = arg0.getvalueat(1, 1);     this.scalez = arg0.getvalueat(2, 2); }  public void scale(double arg0) {     scalex(arg0);     scaley(arg0);     scalez(arg0); }  public matrix4f getscale() {     return new matrix4f(new double[][] {             { getscalex(), 0, 0, 0 },             { 0, getscaley(), 0, 0 },             { 0, 0, getscalez(), 0 },             { 0, 0, 0, 1 }     }); }  public void translatex(double arg0) {     this.translatex += arg0; }  public void translatey(double arg0) {     this.translatey += arg0; }  public void translatez(double arg0) {     this.translatez += arg0; }  public matrix4f gettranslation() {     return new matrix4f(new double[][] {         { 1, 0, 0, translatex },         { 0, 1, 0, translatey },         { 0, 0, 1, translatez },         { 0, 0, 0, 1 }     }); }  } 

scene class

package com.cub.cubefront;  import com.cub.cubefront.graphics.bitmap; import com.cub.cubefront.input.inputhandler; import com.cub.cubefront.math.matrix4f; import com.cub.cubefront.math.vector2f; import com.cub.cubefront.math.vector3f; import com.cub.cubefront.mesh.camera; import com.cub.cubefront.mesh.mesh;  public class scene extends bitmap {  private camera defaultcam;  public inputhandler input = new inputhandler();  public scene() {     super(mainengine.getwidth(), mainengine.getheight());     defaultcam = new camera(); }  public void update() { }  public void start() { }  public void render() { }  public void render(mesh arg0) {     matrix4f trans = arg0.getrotation().multiply(arg0.getscale().multiply(arg0.gettranslation()));     (int = 1; < arg0.getverticiescount()+1; i++) { // depth: manipulate x , y z         vector3f v1 = trans.transform(arg0.getvertexat(i));         (int n = 1; n < i; n++) {             vector3f v2 = trans.transform(arg0.getvertexat(n));             drawline(                 new vector2f((v1.getx()), (v1.gety())),                 new vector2f((v2.getx()), (v2.gety()))             );         }     } }  public void clear() {     (int = 0; < pixels.length; i++) {         pixels[i] = 0;     } }  public camera getcamera() {     return defaultcam; }  public void setcamera(camera defaultcam) {     this.defaultcam = defaultcam; } 

}

matrix4f class

package com.cub.cubefront.math;  public class matrix4f {  private double[][] values;  public matrix4f() {     values = new double[4][4];     setvalues(); }  public matrix4f(double[][] arg0) {     setvalues(arg0); }  private matrix4f setvalues() {     values[0][0] = 1;  values[0][1] = 0;  values[0][2] = 0;  values[0][3] = 0;     values[1][0] = 0;  values[1][1] = 1;  values[1][2] = 0;  values[1][3] = 0;     values[2][0] = 0;  values[2][1] = 0;  values[2][2] = 1;  values[2][3] = 0;     values[3][0] = 0;  values[3][1] = 0;  values[3][2] = 0;  values[3][3] = 1;     return this; }  public matrix4f multiply(matrix4f arg0) {     double res[][] = new double[4][4];     (int = 0; < 4; i++) {         (int j = 0; j < 4; j++) {             (int k = 0; k < 4; k++) {                 res[i][j] += values[i][k] * arg0.getvalueat(k, j);             }         }     }      return new matrix4f(res); }  public double[][] getvalues() {     return values; }  public double getvalueat(int arg0, int arg1) {     return values[arg0][arg1]; }  public void setvalues(double[][] arg0) {     this.values = arg0; }  public void setvalueat(int arg0, int arg1, double arg2) {     values[arg0][arg1] = arg2; }  public vector3f transform(vector3f arg0) {     return new vector3f(         (float)(arg0.getx() * values[0][0] + arg0.gety() * values[1][0] + arg0.getz() * values[2][0]),         (float)(arg0.getx() * values[0][1] + arg0.gety() * values[1][1] + arg0.getz() * values[2][1]),         (float)(arg0.gety() * values[0][2] + arg0.gety() * values[1][2] + arg0.getz() * values[2][2])     ); }  } 

please @ these pictures if still don't know mean.

picture #1:

still triangle

picture #2:

triangle rotated 30 degrees

do see how rotates around corner instead of center? effort in trying help!

this happens when make transformations without reseting object origin before affecting it. remember matrix order matters. rotations take place relative origin, if object not on origin rotate oddly.

try rotating after translating (0,0,0). apply desired translation triangle (that translate origin, rotate, return original position).

hope helps.

edit

in general if want rotate polygon arround fixed point should translate polygon in such way desired point in (0,0,0). procede rotation , return original position.

say have triangle vertex (1,1), (3,1) , (2,3). if want rotate around center of triangle (which (2,2)) should:

  1. substract (2,2) each vertex (making (2,2) origin)
  2. then rotate desired angle
  3. add (2,2) each vertex, returning original position.

this same principles apply 3 dimensions.


Comments

Popular posts from this blog

wordpress - (T_ENDFOREACH) php error -

Export Excel workseet into txt file using vba - (text and numbers with formulas) -

Using django-mptt to get only the categories that have items -