I would like to share the rotation matrix method that I created with you because there are not much tutorials about them. and the ones that I found where not very helpfull to me.
so what this method does it rotates a point in 3d space around an other point in 3d space.
rotation is alpha,beta and gamma.
this way you can rotate a 3d object by rotating every cornerpoint of the object around the objects center.
the method:
public class Physics
{
static float new_rotated_x, new_rotated_y, new_rotated_z;
static float last_point_x, last_point_y, last_point_z, last_alpha, last_beta, last_gamma;
public static float RotationMatrix(String pointtype,float point_x,float point_y,float point_z, float center_point_x, float center_point_y, float center_point_z, float alpha, float beta, float gamma)
{
//pointtype: either, "x","y" or "z" depending on the axis you want to return
//point_x,point_y,point_z: the coordinates of the point that rotates - coordinates of the center point
//center_point_x,center_point_y,center_point_z: the the coordinates of the center point (the point revolves around the center point)
//rotation alpha,beta,gamma rotation alpha,beta,gamma
/* 3D Rotation matrix
*
* R(alpha,beta,gamma) = Rz (alpha) Ry (beta) Rx (gamma)
*
* | [cos_a * cos_b] [cos_a * sin_b * sin_g - sin_a * cos_g] [cos_a * sin_b * cos_g + sin_a * sin_g] |
* | [sin_a * cos_b] [sin_a * sin_b * sin_g + cos_a * cos_g] [sin_a * sin_b * cos_g - cos_a * sin_g] |
* | [-sin_b ] [cos_b * sin_g ] [cos_b * cos_g ] |
*
*/
//dont recalculate because x,y and z are already calculated before
if (last_point_x == point_x && last_point_y == point_y && last_point_z == point_z && last_alpha == alpha && last_beta == beta && last_gamma == gamma)
{
}
//recalculate
else
{
double sin_a = Math.sin(alpha);
double sin_b = Math.sin(beta);
double sin_g = Math.sin(gamma);
double cos_a = Math.cos(alpha);
double cos_b = Math.cos(beta);
double cos_g = Math.cos(gamma); double[] PositionMatrix = { point_x, point_y, point_z};
//ReturnMatrix[row][column]
double[][] RotationMatrix = { {cos_a * cos_b, (cos_a * sin_b * sin_g) - (sin_a * cos_g), (cos_a * sin_b * cos_g) + (sin_a * sin_g) },
{sin_a * cos_b, (sin_a * sin_b * sin_g) + (cos_a * cos_g), (sin_a * sin_b * cos_g) - (cos_a * sin_g) },
{-sin_b , cos_b * sin_g , cos_b * cos_g } };
double new_x_calc_1 = RotationMatrix[0][0] * PositionMatrix[0];
double new_x_calc_2 = RotationMatrix[0][1] * PositionMatrix[1];
double new_x_calc_3 = RotationMatrix[0][2] * PositionMatrix[2];
double new_y_calc_1 = RotationMatrix[1][0] * PositionMatrix[0];
double new_y_calc_2 = RotationMatrix[1][1] * PositionMatrix[1];
double new_y_calc_3 = RotationMatrix[1][2] * PositionMatrix[2];
double new_z_calc_1 = RotationMatrix[2][0] * PositionMatrix[0];
double new_z_calc_2 = RotationMatrix[2][1] * PositionMatrix[1];
double new_z_calc_3 = RotationMatrix[2][2] * PositionMatrix[2];
new_rotated_x = (float)(new_x_calc_1 + new_x_calc_2 + new_x_calc_3);
new_rotated_y = (float)(new_y_calc_1 + new_y_calc_2 + new_y_calc_3);
new_rotated_z = (float)(new_z_calc_1 + new_z_calc_2 + new_z_calc_3);
last_point_x = point_x;
last_point_y = point_y;
last_point_z = point_z;
last_alpha = alpha;
last_beta = beta;
last_gamma = gamma;
}
if (pointtype.contains("x"))
{
return(new_rotated_x+center_point_x);
}
else if (pointtype.contains("y"))
{
return(new_rotated_y+center_point_y);
}
else if (pointtype.contains("z"))
{
return(new_rotated_z+center_point_z);
}
return(0);
}
}
and this is how the method is called in an object, in this case a cube:
@Override
public void render()
{
if (killed == true || brender == false)
{
return;
}
rotation_alpha +=0.001;
rotation_beta +=0.01;
rotation_gamma += 0.00;
cx1 = - xsize;
cy1 = - xsize;
cz1 = - xsize;
cx2 = + xsize;
cy2 = - xsize;
cz2 = - xsize;
cx3 = + xsize;
cy3 = + xsize;
cz3 = - xsize;
cx4 = - xsize;
cy4 = + xsize;
cz4 = - xsize;
cx5 = - xsize;
cy5 = - xsize;
cz5 = + xsize;
cx6 = + xsize;
cy6 = - xsize;
cz6 = + xsize;
cx7 = + xsize;
cy7 = + xsize;
cz7 = + xsize;
cx8 = - xsize;
cy8 = + xsize;
cz8 = + xsize;
float newcx1 = Physics.RotationMatrix("x", cx1, cy1, cz1, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy1 = Physics.RotationMatrix("y", cx1, cy1, cz1, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz1 = Physics.RotationMatrix("z", cx1, cy1, cz1, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx2 = Physics.RotationMatrix("x", cx2, cy2, cz2, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy2 = Physics.RotationMatrix("y", cx2, cy2, cz2, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz2 = Physics.RotationMatrix("z", cx2, cy2, cz2, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx3 = Physics.RotationMatrix("x", cx3, cy3, cz3, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy3 = Physics.RotationMatrix("y", cx3, cy3, cz3, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz3 = Physics.RotationMatrix("z", cx3, cy3, cz3, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx4 = Physics.RotationMatrix("x", cx4, cy4, cz4, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy4 = Physics.RotationMatrix("y", cx4, cy4, cz4, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz4 = Physics.RotationMatrix("z", cx4, cy4, cz4, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx5 = Physics.RotationMatrix("x", cx5, cy5, cz5, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy5 = Physics.RotationMatrix("y", cx5, cy5, cz5, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz5 = Physics.RotationMatrix("z", cx5, cy5, cz5, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx6 = Physics.RotationMatrix("x", cx6, cy6, cz6, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy6 = Physics.RotationMatrix("y", cx6, cy6, cz6, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz6 = Physics.RotationMatrix("z", cx6, cy6, cz6, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx7 = Physics.RotationMatrix("x", cx7, cy7, cz7, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy7 = Physics.RotationMatrix("y", cx7, cy7, cz7, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz7 = Physics.RotationMatrix("z", cx7, cy7, cz7, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcx8 = Physics.RotationMatrix("x", cx8, cy8, cz8, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcy8 = Physics.RotationMatrix("y", cx8, cy8, cz8, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
float newcz8 = Physics.RotationMatrix("z", cx8, cy8, cz8, x, y, z, rotation_alpha, rotation_beta, rotation_gamma);
//front quad
Draw.drawRect(newcx1,newcy1,newcz1,newcx2,newcy2,newcz2,newcx6,newcy6,newcz6,newcx5,newcy5,newcz5,red,green,blue,transparancy);
//left quad
Draw.drawRect(newcx4,newcy4,newcz4,newcx1,newcy1,newcz1,newcx5,newcy5,newcz5,newcx8,newcy8,newcz8,red,green,blue,transparancy);
//right quad
Draw.drawRect(newcx2,newcy2,newcz2,newcx3,newcy3,newcz3,newcx7,newcy7,newcz7,newcx6,newcy6,newcz6,red,green,blue,transparancy);
//back quad
Draw.drawRect(newcx4,newcy4,newcz4,newcx3,newcy3,newcz3,newcx7,newcy7,newcz7,newcx8,newcy8,newcz8,red,green,blue,transparancy);
//bottom quad
Draw.drawRect(newcx1,newcy1,newcz1,newcx2,newcy2,newcz2,newcx3,newcy3,newcz3,newcx4,newcy4,newcz4,red,green,blue,transparancy );
//top quad
Draw.drawRect(newcx5,newcy5,newcz5,newcx6,newcy6,newcz6,newcx7,newcy7,newcz7,newcx8,newcy8,newcz8,red,green,blue,transparancy );
}