How can you update the screen in a loop?

Hello! So I am trying to design a Rubik’s cube in jMonkey, and I wanted to add a feature to randomize the cube. I made and attached the input manager, and I have a function that looks like

long start = System.currentTimeMillis();

long update = System.currentTimeMillis();
Random rand = new Random();
while(update-start<5*1000){
    update = System.currentTimeMillis();
    if(angSum>max){
        int randNum = rand.nextInt(6)+1;
        rotate.onAction("Rotate"+randNum, false, tpf);
        angSum = 0;
    }
    update(tpf); 
}`

Where rotate is an ActionListener that handles when a user wants to rotate a specific way “Rotate1”, “Rotate2”, etc. and update is the standard update loop that rotates a side of the cube. When this is run the screen freezes, so is there any way I can update the screen from within this loop? Or is there a better way of achieving randomization?
Thank you!

In a JME application, the update loop is provided for you. Put your update code in an app state or custom control or (in very simple applications) the simpleUpdate() method of the application itself. Otherwise all the other updating (needed to keep your screen from freezing) will never happen.

If you haven’t already, you may want to work your way through the JME tutorials in sequence.

3 Likes

I always wanted to do a virtual Rubik’s Cube. That said, look at the tutorials. That said, this is almost a beginner project to learn JME. So, the basic loop is:

  1. Input (Optional) - set a state in your CubeControl.
  2. In CubeControl.update(tpf), check your state and make a move. i.e., set up slerp(q, f) and move it over time.
  3. Wait for the engine to draw the updated cube. Or, move the plate (set of cubes) over time.
1 Like

Well, I don’t know where that guy went and I don’t know if he saw my/our reply(ies) but, I finally made my Rubiks Cube app. I had to make a block in Blender (unwrapping a cube), I used my space ship controls for rotations, I figured out how to rotate a 3d matrix that tracks the location of the cubes. So fun!

1 Like