Own GameController

Hi again  :smiley:



I have another question regarding GameControllers!



I want to use my own GameController (which works on the GameState Tutorial) in any other File.




Node boxNode = new Node("MonkeyBoxNode");
        boxNode.attachChild(sphere);
        rootNode.attachChild(boxNode);
        rootNode.addController(new BalanceBoardController(boxNode));



So I add a Node, attach the child to it and then I want to controll it using my BalanceBoardController....

package gamecontrols;
 
import com.jme.input.controls.GameControl;
import com.jme.input.controls.GameControlManager;
import com.jme.input.controls.binding.BalanceBoardBindingKeys;
import com.jme.input.controls.binding.KeyboardBinding;
import com.jme.input.controls.binding.MouseButtonBinding;
import com.jme.scene.Controller;
import com.jme.scene.Node;
import static gamecontrols.CubeController.CubeAction.*;
import static com.jme.input.KeyInput.*;
import static com.jme.input.controls.binding.MouseButtonBinding.*;
import com.jme.input.controls.binding.BalanceBoardBinding;
import gamecontrols.WiiRemoteProxy;
 
public class BalanceBoardController extends Controller{
   
   public      WiiRemoteProxy         balanceBoard;
   private static final long serialVersionUID = 1L;
   enum CubeAction {LEFT, RIGHT, UP, DOWN, EXIT, X_AXIS, Y_AXIS};
 
    private final static float SPEED = 2F;
    private final Node node;
    private final GameControlManager manager;
    private float x_axis = 0F;
    private float y_axis = 0F;
   
    public CubeController(Node node) {
        this.node = node;
        this.manager = new GameControlManager();
       
        //create all actions
        for (CubeAction action : CubeAction.values()) {
            manager.addControl(action.name());
        }
                      
        //bind keys
        bindKey(EXIT, KEY_X);
        bindKey(UP, KEY_UP);
        bindKey(DOWN, KEY_DOWN);
        bindKey(LEFT, KEY_LEFT);
        bindKey(RIGHT, KEY_RIGHT);
 
        //bind mouse buttons
        bindMouseButton(LEFT, LEFT_BUTTON);
        bindMouseButton(RIGHT, RIGHT_BUTTON);
               
        balanceBoard = new WiiRemoteProxy();
        //BalanceBoard binding
           bindBalanceBoardKeys(RIGHT, BalanceBoardBindingKeys.AXIS_RIGHT);
           bindBalanceBoardKeys(DOWN, BalanceBoardBindingKeys.AXIS_DOWN);
           bindBalanceBoardKeys(LEFT, BalanceBoardBindingKeys.AXIS_LEFT);
           bindBalanceBoardKeys(UP, BalanceBoardBindingKeys.AXIS_UP);
    }
    
    private void bindKey(CubeAction action, int... keys) {
        final GameControl control = manager.getControl(action.name());
        for (int key : keys) {
          control.addBinding(new KeyboardBinding(key));
        }
    }
   
    private void bindBalanceBoardAxis(CubeAction action, int axis){
        final GameControl control = manager.getControl(action.name());
        control.addBinding(new BalanceBoardBinding(balanceBoard, axis));
    }
   
    private void bindBalanceBoardKeys(CubeAction action, int axis){
       final GameControl control = manager.getControl(action.name());
        control.addBinding(new BalanceBoardBindingKeys(balanceBoard, axis));
    }
    
    private void bindMouseButton(CubeAction action, int mouseButton) {
        final GameControl control = manager.getControl(action.name());
        control.addBinding(new MouseButtonBinding(mouseButton));
    }
 
  
    private float value(CubeAction action) {
        return manager.getControl(action.name()).getValue();
    }
 
    @Override
    public void update(float time) {
        if (value(EXIT) > 0) {
            System.exit(0); //OK, this is just a demo...
        }
        else{      
        x_axis += SPEED * time * (value(RIGHT) - value(LEFT));
        y_axis += SPEED * time * (value(UP) - value(DOWN));
       
   
        node.getLocalTranslation().set(x_axis, y_axis, 0);
        }
    }

}



But nothing happends...

When I use it with my CubeGameState it works.... Any ideas why it doesnt work global?

package gametest;
 

import gamecontrols.WiiRemoteProxy;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.jme.bounding.BoundingBox;
import com.jme.bounding.BoundingSphere;
import com.jme.image.Texture;
import com.jme.light.PointLight;
import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;
import com.jme.scene.shape.Box;
import com.jme.scene.state.LightState;
import com.jme.scene.state.MaterialState;
import com.jme.scene.state.TextureState;
import com.jme.system.DisplaySystem;
import com.jme.util.TextureManager;
import com.jme.util.export.binary.BinaryImporter;
import com.jmex.game.state.BasicGameState;
import com.jmex.model.converters.FormatConverter;
import com.jmex.model.converters.ObjToJme;
 
/**
 *
 * @author Gronau
 */
public class CubeGameState extends BasicGameState {
 
 
   public      WiiRemoteProxy         remote;
    private static final Logger logger = Logger
     .getLogger(CubeGameState.class.getName());
   
    public CubeGameState() {
        super("cubeGameState");
     // Point to a URL of my model
        URL model=CubeGameState.class.getClassLoader().getResource("jmetest/data/model/maggie.obj");

        // Create something to convert .obj format to .jme
        FormatConverter converter=new ObjToJme();
        // Point the converter to where it will find the .mtl file from
        converter.setProperty("mtllib",model);

        // This byte array will hold my .jme file
        ByteArrayOutputStream BO=new ByteArrayOutputStream();
        try {
            // Use the format converter to convert .obj to .jme
            converter.convert(model.openStream(), BO);
            Node maggie=(Node)BinaryImporter.getInstance().load(new ByteArrayInputStream(BO.toByteArray()));
            // shrink this baby down some
            maggie.setLocalScale(.1f);
            maggie.setModelBound(new BoundingSphere());
            maggie.updateModelBound();
            // Put her on the scene graph
            Node boxNode = new Node("MonkeyBoxNode");
            boxNode.attachChild(maggie);
            rootNode.attachChild(boxNode);
            rootNode.addController(new BalanceBoardController(boxNode));
                      
        
           
        } catch (IOException e) {   // Just in case anything happens
            logger.logp(Level.SEVERE, this.getClass().toString(),
                    "simpleInitGame()", "Exception", e);
            System.exit(0);
        }
     
    }
}



Thanks for the help ;)
RapidM

Is it always remaining 0.0f or something else?

It doesn't even really connect to the board. So I guess the value is 0.0f.



It just prints out: SUCCESFULLY CONNECTED BB



but disconnects after a short time by itself. That did not happen with the CubeGameController.

There it printed out: "SUCCESFULLY CONNECTED BB" and then "BATTERY: " and only after pressing the power bottom: "DISCONNECTED BB"



Maybe the code of the BB might help?


package gamecontrols;

import wiiremotej.*;
import wiiremotej.event.*;
import com.jme.math.Vector3f;

public class WiiRemoteProxy extends BalanceBoardAdapter implements BalanceBoardListener{

   public Vector3f         mcl_wiiTx = new Vector3f();
   private BalanceBoard    remote = null;
   private static double   totalmass = 0;
   private static boolean   connected = false;
   
   private static int BSL = 43;
   private static int BSW = 24;
   
   public float center_x = 0;
   public float center_y = 0;
   
   
   public void findWii(){
   
      remote = null;
      
      try
      {
      
      remote = WiiRemoteJ.connectToBalanceBoard("00233199ddc0");
      remote.addBalanceBoardListener(this);
        //requesting for status
       remote.setLEDIlluminated(true);
       remote.requestStatus();
      
      }
       catch(Exception e)
       {
          e.printStackTrace();
       }
   }
   
   public WiiRemoteProxy()
   {
      findWii();
      connected = true;
      System.out.println("SUCCESFULLY CONNECTED BB");
   }   
   
   public void buttonInputReceived(BBButtonEvent evt)
   {
      if(evt.isPressed()) {
         remote.disconnect();
         System.out.println("DISCONNECTED BB");
      }
   }
      
   public void statusReported(BBStatusEvent evt)
   {
      //Print out the level of battery
      System.out.println("Battery: " + evt.getBatteryLevel());
   }
      
   public void massInputReceived(BBMassEvent evt)
   {
      totalmass = evt.getTotalMass();
      
      //Print out the level of battery
      System.out.println("Total weight: " + totalmass);
      
      double massRightTop = evt.getMass(MassConstants.TOP, MassConstants.RIGHT);
       double massLeftTop = evt.getMass(MassConstants.TOP, MassConstants.LEFT);
       double massRightBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.RIGHT);
       double massLeftBottom = evt.getMass(MassConstants.BOTTOM, MassConstants.LEFT);   
                
   //if bellow 20kg
   if (totalmass > 20)
   {// deltas are +/- how much kg in each direction
        float horizontalDelta = (float)(massLeftTop + massLeftBottom) / (float)(massRightTop + massRightBottom);
        float verticalDelta = (float)(massLeftTop + massRightTop) / (float)(massLeftBottom + massRightBottom);
       
        //LEFT and RIGHT
        center_x = ((float)(horizontalDelta - 1) / (float) (horizontalDelta + 1)) * (float) (-BSL / 2);
        //UP and DOWN
        center_y = ((float)(verticalDelta - 1) / (float) (verticalDelta + 1)) * (float) (-BSW / 2);
   
     } else {
        center_x = 0;
        center_y = 0;
     }
   }
   
   public double getHorizontal(){
       return center_x;
      }   
   
   public double getVertical(){
       return center_y;
      }   
}



Thanks, RapidM

Not sure…I would suggest going through and determining differences between the two sets of code to determine what's causing the trouble.

Are you sure the update method in the Controller is being called; in BasicGameState rootNode.updateGeometricState() is already being called (in the update method) this is what actually drives the controller updates…