I've been trying to make a rubik's cube project and it succeeds nicely . The only problem(or something like that) is that i need to optimize my code for easier understanding and more flexibility. Here's my code to optimize:
[...]
int main()
{
[...]
while (!glfwWindowShouldClose(window))
{
[...]
cubes.DrawCubes(view, projection);
chk:
if (rSpeed > 90)
{
rSpeed = 90;
}
if (rSpeed < 1)
{
rSpeed = 1;
}
if (90 % rSpeed != 0)
{
if (inc)
rSpeed++;
else if (dec)
rSpeed--;
goto chk;
}
if (cubes.Rot_speed != rSpeed)
{
cubes.SetCubeRotationSpeed(rSpeed);
}
if (RotateX)
{
if (rot_x < 90 / rSpeed)
{
cubes.Rotate(0);
rot_x++;
cubes.Rotating = true;
RotateY = false;
RotateZ = false;
}
else
{
rot_x = 0;
RotateX = false;
cubes.Rotating = false;
}
}
if (RotateY)
{
if (rot_y < 90 / rSpeed)
{
cubes.Rotate(1);
rot_y++;
cubes.Rotating = true;
RotateX = false;
RotateZ = false;
}
else
{
rot_y = 0;
RotateY = false;
cubes.Rotating = false;
}
}
if (RotateZ)
{
if (rot_z < 90 / rSpeed)
{
cubes.Rotate(2);
rot_z++;
cubes.Rotating = true;
RotateY = false;
RotateX = false;
}
else
{
rot_z = 0;
RotateZ = false;
cubes.Rotating = false;
}
}
[...]
}
}
where rSpeed and cubes.Rot_speed refers to angles for rotation speeds
On function cubes.Rotate()
:
void x_Cubes::Rotate(int axis)
{
if (axis == 0)
{
for (auto& mx : m_cubes)
{
if (mx.cellx == m_HCube.cellx)
mx.RotateX();
}
}
else if (axis == 1)
{
for (auto& mx : m_cubes)
{
if (mx.celly == m_HCube.celly)
mx.RotateY();
}
}
else if (axis == 2)
{
for (auto& mx : m_cubes)
{
if (mx.cellz == m_HCube.cellz)
mx.RotateZ();
}
}
}
m_cubes
is just a vector of cube data
And the last piece of code :
void _Rotate(glm::mat4& mat, float ang_x, float ang_y, float ang_z)
{
glm::mat4 transformX = glm::mat4(1.0f);
glm::mat4 transformY = glm::mat4(1.0f);
glm::mat4 transformZ = glm::mat4(1.0f);
transformX = glm::rotate(transformX, glm::radians(ang_x), glm::vec3(1.0f, 0.0f, 0.0f));
transformY = glm::rotate(transformY, glm::radians(ang_y), glm::vec3(0.0f, 1.0f, 0.0f));
transformZ = glm::rotate(transformZ, glm::radians(ang_z), glm::vec3(0.0f, 0.0f, 1.0f));
mat = transformX * transformY * transformZ * mat;
}
void Cube::RotateX()
{
rot_angle_X++;
if (rot_angle_X < (90.0f / rot_speed))
_Rotate(model, rot_speed, 0.0f, 0.0f);
else
{
rot_angle_X = 0.0f;
_Rotate(model, rot_speed, 0.0f, 0.0f);
}
if (rot_angle_X == 90.0f || rot_angle_X == 0.0f)
SwapCellVals(0);
}
void Cube::RotateY()
{
rot_angle_Y++;
if (rot_angle_Y < (90.0f / rot_speed))
_Rotate(model, 0.0f, rot_speed, 0.0f);
else
{
rot_angle_Y = 0.0f;
_Rotate(model, 0.0f, rot_speed, 0.0f);
}
if (rot_angle_Y == 90.0f || rot_angle_Y == 0.0f)
SwapCellVals(1);
}
void Cube::RotateZ()
{
rot_angle_Z++;
if (rot_angle_Z < (90.0f / rot_speed))
_Rotate(model, 0.0f, 0.0f, rot_speed);
else
{
rot_angle_Z = 0.0f;
_Rotate(model, 0.0f, 0.0f, rot_speed);
}
if (rot_angle_Z == 90.0f || rot_angle_Z == 0.0f)
SwapCellVals(2);
}
Here, x_Cubes
is a class that has a vector for storing data of class Cube
. The code is too long for rotation and the Booleans are too many for a rotation function. I need to reduce the codes so that i only need to call the function on the main block and flags are only in the rotate function of Cube
. Im using shaders for the drawing actually so i dont know how to actually show the animations properly by reducing the codes. Any help would be appreciated. Thanks in advance!!