0

I started using jMonekyEngine and it's easy way of interacting with Swing GUI. Following their tutorial here http://jmonkeyengine.org/wiki/doku.php/jme3:advanced:swing_canvas

It all works and I get everything loaded,however I'm having trouble modifying things.

According to their tutorial, the constant update and happens here:

public void simpleUpdate(float tpf) {
    geom.rotate(0, 2 * tpf, 0);
}

(this is an example from the tutorial on rotating objects). what i'm trying to do is just increasing and decreasing the speed of rotation (by changing the 2 or tpf with a variable which gets update inside an ActionListener in the Swing gui.

However, since in their tutorial they stated that the swing gui is to be created inside the main method, I have to create a variable which is static in order to change it.

static float rotate = 0.0f;

it gets modified inside the main method, but when trying to use it like so:

public void simpleUpdate(float tpf) {
    geom.rotate(0, rotate * tpf, 0);
}

it remains constant to the initial value. I tried creating a GUI class to build the gui (extends JPanel) and using getters and setters, but still not go.. Any help would be appreciated! Thanks!

EDIT: Here's how I change the rotate value:

JButton faster = new JButton("Faster");
faster.addActionListener(new ActionListener() {

    @Override
    public void actionPerformed(ActionEvent arg0) {
        rotate +=0.1f;
    }
});

inside the main method. rotate is a static field.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
La bla bla
  • 8,558
  • 13
  • 60
  • 109
  • 1
    I don't see where you are assigning a new value... Have to have a `rotate = (something)` to actually change the value – Jared May 06 '12 at 00:34
  • ofcourse, this happens inside the button addActionListener inside the gui. edited my question to show this – La bla bla May 06 '12 at 00:35
  • 1
    @Lablabla, unless you're drawing in the event thread, maybe you should declare `rotate` `volatile`, since action handlers fire in the event thread. – Mike Samuel May 06 '12 at 00:40
  • didn't know this modifier, however it didn't work. tried private volatile static float rotate = 0.0f; – La bla bla May 06 '12 at 00:43
  • @Lablabla have you tried looking at this in a debugger to try and trace down what might be happening? Just looking at your code that was provided I don't see why it wouldn't work. Also the other thing that I'm curious of is what `tpf` is set to. If it's ever 0 then it would give the appearance of no movement since anything * zero is still zero – Jared May 06 '12 at 00:54
  • @Jared I'll give it a shot in the debugger. tpf isn't zero, if given a value * tpf (as in the first code snippet) it rotates. – La bla bla May 06 '12 at 01:00

1 Answers1

1

This is working for me

http://test.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_main_event_loop http://test.jmonkeyengine.org/wiki/doku.php/jme3:beginner:hello_input_system?s[]=input

Is your action listener really triggering the event on click? maybe you have a problem there and not in the rotate variable. Note that I'm not using swing on this example..

import com.jme3.app.SimpleApplication;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;

/** Sample 4 - how to trigger repeating actions from the main update loop.
 * In this example, we make the player character rotate. */
public class HelloLoop extends SimpleApplication {

    public static void main(String[] args){
        HelloLoop app = new HelloLoop();
        app.start();
    }

    protected Geometry player;

    @Override
    public void simpleInitApp() {

        Box b = new Box(Vector3f.ZERO, 1, 1, 1);
        player = new Geometry("blue cube", b);
        Material mat = new Material(assetManager,
          "Common/MatDefs/Misc/Unshaded.j3md");
        mat.setColor("Color", ColorRGBA.Blue);
        player.setMaterial(mat);
        rootNode.attachChild(player);

        initKeys();
    }

    /* This is the update loop */
    @Override
    public void simpleUpdate(float tpf) {
        // make the player rotate
        player.rotate(0, val*tpf, 0); 
    }
    float val = 2f;
    private void initKeys() {
        // Adds the "u" key to the command "coordsUp"
        inputManager.addMapping("sum",  new KeyTrigger(KeyInput.KEY_ADD));
        inputManager.addMapping("rest",  new KeyTrigger(KeyInput.KEY_SUBTRACT));

        inputManager.addListener(al, new String[]{"sum", "rest"});
    }
      private ActionListener al = new ActionListener() {
        public void onAction(String name, boolean keyPressed, float tpf) {
          if (name.equals("sum") ) {
              val++;
          }else if (name.equals("rest")){
              val--;
          }
        }
      };
}
porfiriopartida
  • 1,546
  • 9
  • 17