Struggling to get the movement working of the "pusher2" object

I am trying to get the puck just to move with the wasd keys, i know the vectors are not right. but I don’t know how to get the pusher2 object recognised within the “onAnalog”. I tried declaring it outside. Within initPusher2, for pusher 2 I get “local variable hides a field”. I am quite new to the jmonkeyengine. I’ve been using the Wiki’s to learn from, and the jmonkeyengine 3.0 Beginners guide. But i seem to be missing the point. I’m sorry if I have phrased this badly, feel free to tell me how I can improve.

package mygame;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.Spatial;
import com.jme3.light.DirectionalLight;
import com.jme3.math.Vector3f;
import com.jme3.math.Vector2f;
import com.jme3.input.controls.*;
import com.jme3.input.*;
import com.jme3.renderer.Camera;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.scene.shape.Box;
import com.jme3.system.AppSettings;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.util.CollisionShapeFactory;
import com.jme3.scene.Node;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.KeyTrigger;

 * test
 * @author mentosss
public class Main extends SimpleApplication  {

    public static void main(String[] args) {
        Main app = new Main();
        AppSettings settings = new AppSettings(true);
    private BulletAppState bulletAppState;
    private RigidBodyControl    table_phy;
    private RigidBodyControl    puck_phy;
    private RigidBodyControl    pusher1_phy;
    private RigidBodyControl    pusher2_phy;
    Boolean isRunning=true;
    public void simpleInitApp() {
        bulletAppState = new BulletAppState();
        DirectionalLight sun = new DirectionalLight();
        sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
        cam.setLocation(new Vector3f(0.0f, -2.0f, 7.0f));
        inputManager.addMapping("Forward", new KeyTrigger(KeyInput.KEY_W));
        inputManager.addMapping("Backward", new KeyTrigger(KeyInput.KEY_S));
        inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
        inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
        inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
        inputManager.addListener(analogListener,"Left", "Right", "Forward", "Backward");
    public void initTable() {
        Spatial table = (Node)assetManager.loadModel("Models/airTable/airTable.j3o");
        table.setLocalTranslation(0.0f, -5.0f, -2.0f);
        CollisionShape tableShape = CollisionShapeFactory.createMeshShape((Node) table);
        table_phy = new RigidBodyControl(tableShape,0.0f);
        bulletAppState.getPhysicsSpace().setGravity(new Vector3f(0,-9.81f, 0));
    public void initPusher1() {
        Spatial pusher1 = assetManager.loadModel("Models/pusher/pusher.j3o");
        pusher1.setLocalTranslation(0.0f, -4.5f, -2.0f);
        CollisionShape pusher1Shape = CollisionShapeFactory.createMeshShape((Node) pusher1);
        pusher1_phy = new RigidBodyControl(pusher1Shape,0.00f);
     public void initPusher2() {
        Spatial pusher2 = assetManager.loadModel("Models/pusher/pusher.j3o");
        pusher2.setLocalTranslation(0.0f, -4.5f, 2.0f);
        CollisionShape pusher2Shape = CollisionShapeFactory.createMeshShape((Node) pusher2);
        pusher2_phy = new RigidBodyControl(pusher2Shape,0.0f);
    public void initPuck(){
        Spatial puck = assetManager.loadModel("Models/puck/puck.j3o");
        puck.setLocalTranslation(1.0f, -3.0f, 0.0f);
        CollisionShape puckShape = CollisionShapeFactory.createMeshShape((Node) puck);
        puck_phy = new RigidBodyControl(puckShape, 0.05f);
    private ActionListener actionListener = new ActionListener() {
    public void onAction(String name, boolean keyPressed, float tpf) {
      if (name.equals("Pause") && !keyPressed) {
        isRunning = !isRunning;
     private AnalogListener analogListener = new AnalogListener() {
    public void onAnalog(String name, float value, float tpf) {
      if (isRunning) {
        if (name.equals("Forward")) {
          Vector3f v = pusher2.getLocalTranslation();
          pusher2.setLocalTranslation(v.x + value*speed, v.y, v.z);
        if (name.equals("Backward")) {
          Vector3f v = pusher2.getLocalTranslation();
          pusher2.setLocalTranslation(v.x + value*speed, v.y, v.z);
        if (name.equals("Right")) {
          Vector3f v = pusher2.getLocalTranslation();
          pusher2.setLocalTranslation(v.x + value*speed, v.y, v.z);
        if (name.equals("Left")) {
          Vector3f v = pusher2.getLocalTranslation();
          pusher2.setLocalTranslation(v.x - value*speed, v.y, v.z);
      } else {
        System.out.println("Press P to unpause.");

    public void simpleUpdate(float tpf) {

    public void simpleRender(RenderManager rm) {
        //TODO: add render code

I think you are suffering from a basic java problem.
Encapsulation has your pusher variable only known in the method where it is created and not outside of it.
Make it an object variable like you did for “table_phy” for example and it will allow you to compile.
I would spend a couple days making sure you got the basics of java before playing with physics in a 3D engine or you’ll go nuts :).

Also, ideally, to really use the physical engine, you need to add an impulse to the object instead of setting it’s location as you do. The way you do it, it will probably not end up going through the table, but it won’t effect other objects correctly. SetPhysicsLocation will work better and can be used for initialization for example, but when you push it, it should be “impulsed” not “located”.

1 Like

Thank you very much for the help. I did try to use

private Spatial pusher2;

Crap ofc! within initPusher2 i left Spatial in front of

pusher2 = assetManager.loadModel("Models/pusher/pusher.j3o");

Which meant i was declaring it again. it works! wooo :slight_smile: Now I will try using SetPhysicsLocation, as the objects are going through each other. Thank you! :slight_smile:

Sorry to ask another question, Is there a good example anywhere for using SetPhysicsLocation? As for some reason it doesn’t recognise it. Am I missing an import?

Keep the javadoc open always… or at least one click away.

Thanks man, I do have the correct thing imported. But Vector3f gets the error “cannot find symbol”

pusher2.setPhysicsLocation(Vector3f(0.0f, -4.5f, 2.0f));

If i do

pusher2.setPhysicsLocation(new Vector3f(0.0f, -4.5f, 2.0f));

then setPhysicsLocation gets “cannot find symbol” . Am i misunderstanding how to use it?

Ah, I didn’t realize you were a Java beginner. Sorry.

The first case doesn’t work because Vector3f() is not a method name… it’s a constructor so you have to use it with “new” as in your second example. I recommend going through some Java tutorials before getting too far into 3D game programming.

setPhysicsLocation() is a method on the rigid body control… which you would have to get from the spatial. You might try going through the JME tutorials.