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.app.SimpleApplication;
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.app.Application;
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);
settings.setResolution(1024,768);
settings.setFrameRate(60);
app.setSettings(settings);
app.start();
}
private BulletAppState bulletAppState;
private RigidBodyControl table_phy;
private RigidBodyControl puck_phy;
private RigidBodyControl pusher1_phy;
private RigidBodyControl pusher2_phy;
Boolean isRunning=true;
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
bulletAppState.getPhysicsSpace().enableDebug(assetManager);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
rootNode.addLight(sun);
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(actionListener,"Pause");
inputManager.addListener(analogListener,"Left", "Right", "Forward", "Backward");
initTable();
initPuck();
initPusher1();
initPusher2();
}
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);
table.addControl(table_phy);
rootNode.attachChild(table);
bulletAppState.getPhysicsSpace().add(table_phy);
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);
pusher1.addControl(pusher1_phy);
rootNode.attachChild(pusher1);
bulletAppState.getPhysicsSpace().add(pusher1_phy);
}
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);
pusher2.addControl(pusher2_phy);
rootNode.attachChild(pusher2);
bulletAppState.getPhysicsSpace().add(pusher2_phy);
}
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);
puck.addControl(puck_phy);
rootNode.attachChild(puck);
bulletAppState.getPhysicsSpace().add(puck_phy);
}
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.");
}
}
};
@Override
public void simpleUpdate(float tpf) {
flyCam.setEnabled(false);
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}