 # How to 3D cube?

Hi,

I am new here.
I have a game idea I wish to develop but very new to cpp/arduboy development.

I wish to create a 3D cube which can be controlled by the dpad to rotate it around.

How would I go about this? Any pointers will be really helpful.

When you say ‘3D cube’ do you mean like a Rubik’s cube? That would be cool.

No, not rubik’s.
Just a solid cube.

Does this help > Add gravity to an animated graphic

1 Like

Yes, that does help somewhat.
However, I am having a slight difficulty in controlling the spin.
Basically, I wish to spin the cube by 90 degrees based on the dpad button.

Any idea how would I be doing that?

Check if the right/left button is pressed and if it is, increase/decrease the angle (`SpinAng`). But rotating it by 90 degrees will result in the same picture if the sides are the same, you’ll see no change.

Like @drummyfish says, make the `SpinAng` change when the dpad is pressed.

E.g. Something like:

``````void updateCube()
{
// Increase velocity
velocityY += 0.01f;

// Update position
positionX += velocityX;
positionY += velocityY;

if(arduboy.pressed(LEFT_BUTTON))
if(iteration > 0)
--iteration;

if(arduboy.pressed(RIGHT_BUTTON))
if(iteration < 60)
++iteration;
}
``````

(If you don’t need the falling/gravity, strip that part out and just draw the cube where you need it.)

For the 3d part, you just need some trigonometry. In this case just create the cube segment lines in a x y z vector struct and use How to calculate x and y coordinates of a rotated 3D cube in JavaScript? - Stack Overflow and change the angle in those formulas to rotate.

Sorry I don’t know where the code of this any more:

But see also the far more advanced: