now we can steer the ship
This commit is contained in:
parent
0935259b6c
commit
47c43078f0
3 changed files with 182 additions and 219 deletions
|
|
@ -1,28 +1,12 @@
|
||||||
package sg;
|
package sg;
|
||||||
|
|
||||||
import java.util.logging.Level;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import sg.env.Environment;
|
import sg.env.Environment;
|
||||||
|
import sg.input.FirstPersonHandler;
|
||||||
import sg.util.SGUtil;
|
import sg.util.SGUtil;
|
||||||
|
|
||||||
import com.jme.bounding.BoundingBox;
|
|
||||||
import com.jme.scene.Node;
|
import com.jme.scene.Node;
|
||||||
import com.jme.scene.shape.Sphere;
|
|
||||||
import com.jme.scene.state.TextureState;
|
|
||||||
import com.jme.system.DisplaySystem;
|
|
||||||
import com.jme.image.Texture;
|
|
||||||
import com.jme.input.ChaseCamera;
|
|
||||||
import com.jme.input.InputHandler;
|
|
||||||
import com.jme.input.KeyInput;
|
|
||||||
import com.jme.input.action.InputAction;
|
|
||||||
import com.jme.input.action.InputActionEvent;
|
|
||||||
import com.jme.math.Quaternion;
|
|
||||||
import com.jme.math.Vector3f;
|
import com.jme.math.Vector3f;
|
||||||
import com.jme.util.TextureManager;
|
|
||||||
import com.jmex.physics.DynamicPhysicsNode;
|
import com.jmex.physics.DynamicPhysicsNode;
|
||||||
import com.jmex.physics.PhysicsSpace;
|
|
||||||
import com.jmex.physics.PhysicsUpdateCallback;
|
|
||||||
import com.jmex.physics.StaticPhysicsNode;
|
import com.jmex.physics.StaticPhysicsNode;
|
||||||
import com.jmex.physics.util.SimplePhysicsGame;
|
import com.jmex.physics.util.SimplePhysicsGame;
|
||||||
|
|
||||||
|
|
@ -30,7 +14,9 @@ public class SpaceGame extends SimplePhysicsGame {
|
||||||
private StaticPhysicsNode staticNode;
|
private StaticPhysicsNode staticNode;
|
||||||
private DynamicPhysicsNode dynamicNode;
|
private DynamicPhysicsNode dynamicNode;
|
||||||
private Environment environment;
|
private Environment environment;
|
||||||
private InputHandler physicsStepInputHandler;
|
private Node ship;
|
||||||
|
private DynamicPhysicsNode shipNode;
|
||||||
|
private FirstPersonHandler firstPersonHandler;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//Logger.getLogger("").setLevel( Level.WARNING );
|
//Logger.getLogger("").setLevel( Level.WARNING );
|
||||||
|
|
@ -48,91 +34,27 @@ public class SpaceGame extends SimplePhysicsGame {
|
||||||
rootNode.attachChild(staticNode);
|
rootNode.attachChild(staticNode);
|
||||||
dynamicNode = getPhysicsSpace().createDynamicNode();
|
dynamicNode = getPhysicsSpace().createDynamicNode();
|
||||||
rootNode.attachChild(dynamicNode);
|
rootNode.attachChild(dynamicNode);
|
||||||
|
shipNode = getPhysicsSpace().createDynamicNode();
|
||||||
|
rootNode.attachChild(shipNode);
|
||||||
|
|
||||||
//environment
|
//environment
|
||||||
environment = new Environment();
|
environment = new Environment(rootNode);
|
||||||
rootNode.attachChild(environment);
|
rootNode.attachChild(environment);
|
||||||
|
|
||||||
//light
|
//light
|
||||||
//TODO
|
//TODO
|
||||||
|
|
||||||
//controll
|
//ship
|
||||||
physicsStepInputHandler = new InputHandler();
|
ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
|
||||||
getPhysicsSpace().addToUpdateCallbacks( new PhysicsUpdateCallback() {
|
|
||||||
public void beforeStep( PhysicsSpace space, float time ) {
|
|
||||||
physicsStepInputHandler.update( time );
|
|
||||||
}
|
|
||||||
public void afterStep( PhysicsSpace space, float time ) {
|
|
||||||
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
physicsStepInputHandler.addAction( new MyInputAction(700, MyInputAction.FORWARD),
|
|
||||||
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_HOME, InputHandler.AXIS_NONE, true );
|
|
||||||
physicsStepInputHandler.addAction( new MyInputAction(700, MyInputAction.BACK),
|
|
||||||
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_END, InputHandler.AXIS_NONE, true );
|
|
||||||
physicsStepInputHandler.addAction( new MyInputAction(700, MyInputAction.LEFT),
|
|
||||||
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_PGUP, InputHandler.AXIS_NONE, true );
|
|
||||||
physicsStepInputHandler.addAction( new MyInputAction(700, MyInputAction.RIGHT),
|
|
||||||
InputHandler.DEVICE_KEYBOARD, KeyInput.KEY_PGDN, InputHandler.AXIS_NONE, true );
|
|
||||||
|
|
||||||
//sphere
|
|
||||||
/*
|
|
||||||
Sphere s = new Sphere("Sphere", 10, 10, 25);
|
|
||||||
s.setLocalTranslation(new Vector3f(0,0,-40));
|
|
||||||
s.setModelBound(new BoundingBox());
|
|
||||||
s.updateModelBound();
|
|
||||||
Texture texture = TextureManager.loadTexture(
|
|
||||||
SpaceGame.class.getClassLoader().getResource(
|
|
||||||
"jmetest/data/images/Monkey.jpg"),
|
|
||||||
Texture.MinificationFilter.BilinearNearestMipMap,
|
|
||||||
Texture.MagnificationFilter.Bilinear);
|
|
||||||
TextureState ts = display.getRenderer().createTextureState();
|
|
||||||
ts.setEnabled(true);
|
|
||||||
ts.setTexture(texture);
|
|
||||||
s.setRenderState(ts);
|
|
||||||
dynamicNode.attachChild(s);
|
|
||||||
*/
|
|
||||||
|
|
||||||
Node ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
|
|
||||||
ship.setLocalTranslation(new Vector3f(0,0,-40));
|
ship.setLocalTranslation(new Vector3f(0,0,-40));
|
||||||
dynamicNode.attachChild(ship);
|
shipNode.attachChild(ship);
|
||||||
|
|
||||||
//ChaseCamera ccam = new ChaseCamera( cam, ship);
|
//controll
|
||||||
//input = ccam;
|
firstPersonHandler = new FirstPersonHandler(ship, shipNode, cam);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void simpleUpdate(){
|
public void simpleUpdate(){
|
||||||
environment.update();
|
environment.update();
|
||||||
}
|
firstPersonHandler.update(0.1f);
|
||||||
|
|
||||||
private class MyInputAction extends InputAction {
|
|
||||||
public static final int FORWARD = 1;
|
|
||||||
public static final int BACK = 2;
|
|
||||||
public static final int LEFT = 3;
|
|
||||||
public static final int RIGHT = 4;
|
|
||||||
private final float force;
|
|
||||||
private final Vector3f appliedForce = new Vector3f();
|
|
||||||
private Vector3f rotation;
|
|
||||||
private final int direction;
|
|
||||||
|
|
||||||
public MyInputAction(float force, int direction) {
|
|
||||||
this.force = force;
|
|
||||||
this.direction = direction;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void performAction( InputActionEvent evt ) {
|
|
||||||
rotation = dynamicNode.getLocalRotation().getRotationColumn(2);
|
|
||||||
switch(direction){
|
|
||||||
case 1:
|
|
||||||
case 2:
|
|
||||||
appliedForce.set(rotation.mult(force)).multLocal( evt.getTime() );
|
|
||||||
dynamicNode.addForce(appliedForce);
|
|
||||||
break;
|
|
||||||
case 3:
|
|
||||||
break;
|
|
||||||
case 4:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -24,13 +24,9 @@ import com.jmex.game.state.GameStateManager;
|
||||||
|
|
||||||
public class SpaceGame2 extends BaseGame {
|
public class SpaceGame2 extends BaseGame {
|
||||||
private static final Logger logger = Logger.getLogger(SpaceGame2.class.getName());
|
private static final Logger logger = Logger.getLogger(SpaceGame2.class.getName());
|
||||||
|
|
||||||
public static final String TITLE = "SpaceGame";
|
public static final String TITLE = "SpaceGame";
|
||||||
|
protected float tpf; //Time per frame
|
||||||
/** Time per frame */
|
protected Timer timer; //High resolution timer for jME.
|
||||||
protected float tpf;
|
|
||||||
/** High resolution timer for jME. */
|
|
||||||
protected Timer timer;
|
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpaceGame2 game = new SpaceGame2();
|
SpaceGame2 game = new SpaceGame2();
|
||||||
|
|
@ -41,7 +37,6 @@ public class SpaceGame2 extends BaseGame {
|
||||||
/**
|
/**
|
||||||
* Creates display, sets up camera, and binds keys. Called in
|
* Creates display, sets up camera, and binds keys. Called in
|
||||||
* BaseGame.start() directly after the dialog box.
|
* BaseGame.start() directly after the dialog box.
|
||||||
*
|
|
||||||
* @see AbstractGame#initSystem()
|
* @see AbstractGame#initSystem()
|
||||||
*/
|
*/
|
||||||
protected void initSystem() throws JmeException {
|
protected void initSystem() throws JmeException {
|
||||||
|
|
@ -74,27 +69,21 @@ public class SpaceGame2 extends BaseGame {
|
||||||
|
|
||||||
|
|
||||||
} catch ( JmeException e ) {
|
} catch ( JmeException e ) {
|
||||||
/**
|
//If the displaysystem can't be initialized correctly, exit instantly.
|
||||||
* If the displaysystem can't be initialized correctly, exit
|
|
||||||
* instantly.
|
|
||||||
*/
|
|
||||||
logger.log(Level.SEVERE, "Could not create displaySystem", e);
|
logger.log(Level.SEVERE, "Could not create displaySystem", e);
|
||||||
System.exit( 1 );
|
System.exit( 1 );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set a black background. */
|
display.getRenderer().setBackgroundColor( ColorRGBA.black.clone() ); //Set a black background.
|
||||||
display.getRenderer().setBackgroundColor( ColorRGBA.black.clone() );
|
|
||||||
|
|
||||||
/** Get a high resolution timer for FPS updates. */
|
timer = Timer.getTimer(); //Get a high resolution timer for FPS updates.
|
||||||
timer = Timer.getTimer();
|
|
||||||
|
|
||||||
GameStateManager.create();
|
GameStateManager.create();
|
||||||
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
||||||
manager.set("mem_report", KeyInput.KEY_TAB);
|
manager.set("mem_report", KeyInput.KEY_TAB);
|
||||||
manager.set("exit", KeyInput.KEY_ESCAPE);
|
manager.set("exit", KeyInput.KEY_ESCAPE);
|
||||||
|
|
||||||
/** Sets the title of our display. */
|
display.setTitle( TITLE ); //Sets the title of our display.
|
||||||
display.setTitle( TITLE );
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -109,19 +98,15 @@ public class SpaceGame2 extends BaseGame {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void update(float interpolation) {
|
protected void update(float interpolation) {
|
||||||
/** Recalculate the framerate. */
|
timer.update(); //Recalculate the framerate.
|
||||||
timer.update();
|
tpf = timer.getTimePerFrame(); //Update tpf to time per frame according to the Timer.
|
||||||
/** Update tpf to time per frame according to the Timer. */
|
|
||||||
tpf = timer.getTimePerFrame();
|
|
||||||
|
|
||||||
// Execute updateQueue item
|
// Execute updateQueue item
|
||||||
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();
|
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();
|
||||||
|
|
||||||
GameStateManager.getInstance().update(tpf);
|
GameStateManager.getInstance().update(tpf);
|
||||||
|
|
||||||
|
|
||||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand(
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("mem_report", false ) ) {
|
||||||
"mem_report", false ) ) {
|
|
||||||
long totMem = Runtime.getRuntime().totalMemory();
|
long totMem = Runtime.getRuntime().totalMemory();
|
||||||
long freeMem = Runtime.getRuntime().freeMemory();
|
long freeMem = Runtime.getRuntime().freeMemory();
|
||||||
long maxMem = Runtime.getRuntime().maxMemory();
|
long maxMem = Runtime.getRuntime().maxMemory();
|
||||||
|
|
@ -131,27 +116,22 @@ public class SpaceGame2 extends BaseGame {
|
||||||
logger.info("Free memory: "+(freeMem>>10)+" kb");
|
logger.info("Free memory: "+(freeMem>>10)+" kb");
|
||||||
logger.info("Max memory: "+(maxMem>>10)+" kb");
|
logger.info("Max memory: "+(maxMem>>10)+" kb");
|
||||||
}
|
}
|
||||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "exit",
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "exit", false ) ) {
|
||||||
false ) ) {
|
|
||||||
|
|
||||||
finish();
|
finish();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Clears stats, the buffers and renders bounds and normals if on.
|
* Clears stats, the buffers and renders bounds and normals if on.
|
||||||
*
|
|
||||||
* @param interpolation unused in this implementation
|
* @param interpolation unused in this implementation
|
||||||
* @see AbstractGame#render(float interpolation)
|
* @see AbstractGame#render(float interpolation)
|
||||||
*/
|
*/
|
||||||
protected void render( float interpolation ) {
|
protected void render( float interpolation ) {
|
||||||
Renderer r = display.getRenderer();
|
Renderer r = display.getRenderer();
|
||||||
/** Clears the previously rendered information. */
|
r.clearBuffers(); //Clears the previously rendered information.
|
||||||
r.clearBuffers();
|
|
||||||
|
|
||||||
// Execute renderQueue item
|
// Execute renderQueue item
|
||||||
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).execute();
|
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).execute();
|
||||||
|
|
||||||
GameStateManager.getInstance().render(tpf);
|
GameStateManager.getInstance().render(tpf);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -162,12 +142,10 @@ public class SpaceGame2 extends BaseGame {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Cleans up the keyboard.
|
* Cleans up the keyboard.
|
||||||
*
|
|
||||||
* @see AbstractGame#cleanup()
|
* @see AbstractGame#cleanup()
|
||||||
*/
|
*/
|
||||||
protected void cleanup() {
|
protected void cleanup() {
|
||||||
logger.info( "Cleaning up resources." );
|
logger.info( "Cleaning up resources." );
|
||||||
|
|
||||||
TextureManager.doTextureCleanup();
|
TextureManager.doTextureCleanup();
|
||||||
if (display != null && display.getRenderer() != null)
|
if (display != null && display.getRenderer() != null)
|
||||||
display.getRenderer().cleanup();
|
display.getRenderer().cleanup();
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,75 @@
|
||||||
package sg.input;
|
package sg.input;
|
||||||
|
|
||||||
|
import com.jme.input.KeyBindingManager;
|
||||||
|
import com.jme.input.KeyInput;
|
||||||
|
import com.jme.math.FastMath;
|
||||||
|
import com.jme.math.Quaternion;
|
||||||
|
import com.jme.math.Vector3f;
|
||||||
import com.jme.renderer.Camera;
|
import com.jme.renderer.Camera;
|
||||||
import com.jme.scene.Node;
|
import com.jme.scene.Node;
|
||||||
|
import com.jmex.physics.DynamicPhysicsNode;
|
||||||
|
|
||||||
|
|
||||||
public class FirstPersonHandler extends GlobalInputHandler{
|
public class FirstPersonHandler extends GlobalInputHandler{
|
||||||
|
private final Vector3f appliedForce = new Vector3f();
|
||||||
|
private Vector3f rotation;
|
||||||
|
private Quaternion qRot = new Quaternion();
|
||||||
|
private float xAngle, yAngle, zAngle;
|
||||||
|
private Node ship;
|
||||||
|
private DynamicPhysicsNode shipNode;
|
||||||
|
private Camera cam;
|
||||||
|
|
||||||
public FirstPersonHandler(Node ship, Camera cam){
|
public FirstPersonHandler(Node ship, DynamicPhysicsNode shipNode, Camera cam){
|
||||||
|
this.ship = ship;
|
||||||
|
this.shipNode = shipNode;
|
||||||
|
this.cam = cam;
|
||||||
|
|
||||||
|
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
||||||
|
manager.set("yaw", KeyInput.KEY_K);
|
||||||
|
manager.set("yaw2", KeyInput.KEY_I);
|
||||||
|
manager.set("roll", KeyInput.KEY_U);
|
||||||
|
manager.set("roll2", KeyInput.KEY_O);
|
||||||
|
manager.set("pitch", KeyInput.KEY_J);
|
||||||
|
manager.set("pitch2", KeyInput.KEY_L);
|
||||||
|
manager.set("forw", KeyInput.KEY_2);
|
||||||
|
manager.set("backw", KeyInput.KEY_1);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(float time){
|
||||||
|
super.update(time);
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("yaw", true ) ) {
|
||||||
|
xAngle = (xAngle + 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("yaw2", true ) ) {
|
||||||
|
xAngle = (xAngle - 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("roll", true ) ) {
|
||||||
|
yAngle = (yAngle + 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("roll2", true ) ) {
|
||||||
|
yAngle = (yAngle - 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("pitch", true ) ) {
|
||||||
|
zAngle = (zAngle + 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("pitch2", true ) ) {
|
||||||
|
zAngle = (zAngle - 0.05f) % (2*FastMath.PI);
|
||||||
|
ship.setLocalRotation(qRot.fromAngles(xAngle, yAngle, zAngle));
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("forw", true ) ) {
|
||||||
|
rotation = ship.getLocalRotation().getRotationColumn(2);
|
||||||
|
appliedForce.set(rotation.mult(-700)).multLocal(time);
|
||||||
|
shipNode.addForce(appliedForce);
|
||||||
|
}
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("backw", true ) ) {
|
||||||
|
rotation = ship.getLocalRotation().getRotationColumn(2);
|
||||||
|
appliedForce.set(rotation.mult(700)).multLocal(time);
|
||||||
|
shipNode.addForce(appliedForce);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue