I am trying 2 create a basic 2d game. I create 2 BetterCharacterControls to represent 2 players. The Floor is a box geometry with a RigidBodyControl. both keep on bouncing for some reason.
Main.java
[java]package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
/**
-
test
-
@author normenhansen
*/
public class Main extends SimpleApplication {public static void main(String[] args) {
Main app = new Main();
app.start();
}@Override
public void simpleInitApp() {
flyCam.setMoveSpeed(50);
GameRunningAppState playState=new GameRunningAppState();
stateManager.attach(playState);
}@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]
GameRunningAppState
[java]/*
- To change this template, choose Tools | Templates
- and open the template in the editor.
*/
package mygame;
import com.jme3.app.Application;
import com.jme3.app.SimpleApplication;
import com.jme3.app.state.AbstractAppState;
import com.jme3.app.state.AppStateManager;
import com.jme3.asset.AssetManager;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.BetterCharacterControl;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.input.InputManager;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.Trigger;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.Camera;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node;
import com.jme3.scene.shape.Box;
import net.java.games.input.Component;
/**
*
-
@author oscar
*/
public class GameRunningAppState extends AbstractAppState {private AssetManager assetManager;
private Node rootNode;
private SimpleApplication app;
private Camera cam;
private InputManager inputManager;
private InputListener inputListener;
private BulletAppState bulletAppState;
private AppStateManager stateManager;Node nodeStage=new Node();
private Node nodeP1=new Node();
private Node nodeP2=new Node();private BetterCharacterControl phyP1;
private BetterCharacterControl phyP2;private Geometry geoStage;
private final static String MAPPING_P1_LEFT = “P1: Move Left”;
private final static String MAPPING_P1_RIGHT = “P1: Move Right”;
private final static String MAPPING_P1_UP = “P1: Jump”;
private final static Trigger TRIGGER_P2_LEFT = new KeyTrigger(KeyInput.KEY_LEFT);
private final static Trigger TRIGGER_P2_RIGHT = new KeyTrigger(KeyInput.KEY_RIGHT);
private final static Trigger TRIGGER_P2_UP = new KeyTrigger(KeyInput.KEY_UP);
private final static String MAPPING_P2_LEFT = “P2: Move Left”;
private final static String MAPPING_P2_RIGHT = “P2: Move Right”;
private final static String MAPPING_P2_UP = “P2: Jump”;@Override
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
//TODO: initialize your AppState, e.g. attach spatials to rootNode
//this is called on the OpenGL thread after the AppState has been attached
this.app = (SimpleApplication) app;
this.cam = this.app.getCamera();
this.rootNode = this.app.getRootNode();
this.assetManager = this.app.getAssetManager();
this.inputManager = this.app.getInputManager();
this.stateManager=this.app.getStateManager();
//this.bulletAppState=this.stateManager.getState(BulletAppState.class);
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);rootNode.attachChild(nodeStage); initStage(); initPlayers(); bulletAppState.setDebugEnabled(true);
}
private void initStage(){
Box meshStage=new Box(50.0f,1.0f,8.0f);
geoStage=new Geometry(“Stage”,meshStage);
Material matStage=new Material(assetManager,“Common/MatDefs/Misc/Unshaded.j3md”);
matStage.setColor(“Color”, ColorRGBA.White);
geoStage.setMaterial(matStage);
nodeStage.attachChild(geoStage);
nodeStage.move(0, -10.0f, -50.0f);
RigidBodyControl phyStage=new RigidBodyControl(0.0f);
geoStage.addControl(phyStage);
bulletAppState.getPhysicsSpace().add(phyStage);
}private void initPlayers(){
Box meshPlayer=new Box(4.0f,6.0f,2.0f); Geometry geoP1=new Geometry("Player 1",meshPlayer); Geometry geoP2=new Geometry("Player 2",meshPlayer); Material matP1=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md"); Material matP2=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md"); matP1.setColor("Color", ColorRGBA.Blue); matP2.setColor("Color", ColorRGBA.Red); geoP1.setMaterial(matP1); geoP2.setMaterial(matP2); nodeP1=new Node(); nodeP2=new Node(); nodeStage.attachChild(nodeP1); nodeStage.attachChild(nodeP2); nodeP1.attachChild(geoP1); nodeP2.attachChild(geoP2); nodeP1.move(-10.0f, 10f, 0f); nodeP2.move(10.0f, 10f, 0f); phyP1=new BetterCharacterControl(2.0f,1.0f,2.0f); phyP2=new BetterCharacterControl(2.0f,1.0f,2.0f); geoP1.addControl(phyP1); geoP2.addControl(phyP2); bulletAppState.getPhysicsSpace().add(phyP1); bulletAppState.getPhysicsSpace().add(phyP2);
}
@Override
public void update(float tpf) {
//TODO: implement behavior during runtime}
@Override
public void cleanup() {
super.cleanup();
//TODO: clean up what you initialized in the initialize method,
//e.g. remove all spatials from rootNode
//this is called on the OpenGL thread after the AppState has been detached
}
}[/java]