0

I am currently creating a rubiks cube project for my a level NEA and currently the cube solves itself so I am now focusing on the implementation of the cube. I have got to a stage where i have a cube made up of 26 different cubes to allow me to give each face smooth rotations and nice colours. I have the code for one of the face rotations below. The left face to be exact but you do not need to know that.

        for (int i = 0; i < 90; i++)
        {
            cube[0].rotateX(Math.PI / 360);  // uses math.PI because the rotations are in radians
            cube[1].rotateX(Math.PI / 360);  // each of these commands rotate 1 degree
            cube[2].rotateX(Math.PI / 360);
            cube[9].rotateX(Math.PI / 360);
            cube[10].rotateX(Math.PI / 360);
            cube[11].rotateX(Math.PI / 360);
            cube[17].rotateX(Math.PI / 360);
            cube[18].rotateX(Math.PI / 360);
            cube[19].rotateX(Math.PI / 360);
            Invalidate();   // runs the paint function again to redraw the cube but rotated
        }

This code performs one 90 degree turn of the face but what I would like to know is why doesn't it update the cube with the invalidate command on each iteration of the for loop and instead only does it once after the turn is performed. And is there a way to make it update on every iteration so I can get a nice smooth turn of the face.

Thanks in advance.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • `Math.PI / 360` is actually 0.5° since pi is 180° – MrCodingB Aug 28 '22 at 15:59
  • Also why are you recalculating the angle for every cube. Why not calculate it once and use a for-loop to rotate every cube by `x` degrees. – MrCodingB Aug 28 '22 at 16:00
  • im aware it is 0.5 dgrees in radians but for some reason that function rotates 1 degree not 0.5 –  Aug 29 '22 at 12:08

1 Answers1

0

In case you haven't solved this yet, you need to add short wait times after each Invalidate, so that it is not trying to loop through as fast as it can.

edit:

https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.control.invalidate?view=windowsdesktop-6.0

Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method.

After calling Control.Invalidate(), you should use Control.Update(). Alternatively, you can just use Control.Refresh().

Hopefully that should fix it.

  • ive tried that. doesnt seem to work. just waits all of the times added up then displays the final drawing. –  Sep 06 '22 at 13:26