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;
|
||||
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import sg.env.Environment;
|
||||
import sg.input.FirstPersonHandler;
|
||||
import sg.util.SGUtil;
|
||||
|
||||
import com.jme.bounding.BoundingBox;
|
||||
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.util.TextureManager;
|
||||
import com.jmex.physics.DynamicPhysicsNode;
|
||||
import com.jmex.physics.PhysicsSpace;
|
||||
import com.jmex.physics.PhysicsUpdateCallback;
|
||||
import com.jmex.physics.StaticPhysicsNode;
|
||||
import com.jmex.physics.util.SimplePhysicsGame;
|
||||
|
||||
|
|
@ -30,7 +14,9 @@ public class SpaceGame extends SimplePhysicsGame {
|
|||
private StaticPhysicsNode staticNode;
|
||||
private DynamicPhysicsNode dynamicNode;
|
||||
private Environment environment;
|
||||
private InputHandler physicsStepInputHandler;
|
||||
private Node ship;
|
||||
private DynamicPhysicsNode shipNode;
|
||||
private FirstPersonHandler firstPersonHandler;
|
||||
|
||||
public static void main(String[] args) {
|
||||
//Logger.getLogger("").setLevel( Level.WARNING );
|
||||
|
|
@ -48,91 +34,27 @@ public class SpaceGame extends SimplePhysicsGame {
|
|||
rootNode.attachChild(staticNode);
|
||||
dynamicNode = getPhysicsSpace().createDynamicNode();
|
||||
rootNode.attachChild(dynamicNode);
|
||||
shipNode = getPhysicsSpace().createDynamicNode();
|
||||
rootNode.attachChild(shipNode);
|
||||
|
||||
//environment
|
||||
environment = new Environment();
|
||||
environment = new Environment(rootNode);
|
||||
rootNode.attachChild(environment);
|
||||
|
||||
//light
|
||||
//TODO
|
||||
|
||||
//controll
|
||||
physicsStepInputHandler = new InputHandler();
|
||||
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
|
||||
ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
|
||||
ship.setLocalTranslation(new Vector3f(0,0,-40));
|
||||
dynamicNode.attachChild(ship);
|
||||
shipNode.attachChild(ship);
|
||||
|
||||
//ChaseCamera ccam = new ChaseCamera( cam, ship);
|
||||
//input = ccam;
|
||||
//controll
|
||||
firstPersonHandler = new FirstPersonHandler(ship, shipNode, cam);
|
||||
}
|
||||
|
||||
public void simpleUpdate(){
|
||||
environment.update();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
firstPersonHandler.update(0.1f);
|
||||
}
|
||||
}
|
||||
|
|
@ -24,13 +24,9 @@ import com.jmex.game.state.GameStateManager;
|
|||
|
||||
public class SpaceGame2 extends BaseGame {
|
||||
private static final Logger logger = Logger.getLogger(SpaceGame2.class.getName());
|
||||
|
||||
public static final String TITLE = "SpaceGame";
|
||||
|
||||
/** Time per frame */
|
||||
protected float tpf;
|
||||
/** High resolution timer for jME. */
|
||||
protected Timer timer;
|
||||
protected float tpf; //Time per frame
|
||||
protected Timer timer; //High resolution timer for jME.
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpaceGame2 game = new SpaceGame2();
|
||||
|
|
@ -41,7 +37,6 @@ public class SpaceGame2 extends BaseGame {
|
|||
/**
|
||||
* Creates display, sets up camera, and binds keys. Called in
|
||||
* BaseGame.start() directly after the dialog box.
|
||||
*
|
||||
* @see AbstractGame#initSystem()
|
||||
*/
|
||||
protected void initSystem() throws JmeException {
|
||||
|
|
@ -74,27 +69,21 @@ public class SpaceGame2 extends BaseGame {
|
|||
|
||||
|
||||
} 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);
|
||||
System.exit( 1 );
|
||||
}
|
||||
|
||||
/** Set a black background. */
|
||||
display.getRenderer().setBackgroundColor( ColorRGBA.black.clone() );
|
||||
display.getRenderer().setBackgroundColor( ColorRGBA.black.clone() ); //Set a black background.
|
||||
|
||||
/** Get a high resolution timer for FPS updates. */
|
||||
timer = Timer.getTimer();
|
||||
timer = Timer.getTimer(); //Get a high resolution timer for FPS updates.
|
||||
|
||||
GameStateManager.create();
|
||||
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
||||
manager.set("mem_report", KeyInput.KEY_TAB);
|
||||
manager.set("exit", KeyInput.KEY_ESCAPE);
|
||||
|
||||
/** Sets the title of our display. */
|
||||
display.setTitle( TITLE );
|
||||
display.setTitle( TITLE ); //Sets the title of our display.
|
||||
|
||||
}
|
||||
|
||||
|
|
@ -109,19 +98,15 @@ public class SpaceGame2 extends BaseGame {
|
|||
|
||||
@Override
|
||||
protected void update(float interpolation) {
|
||||
/** Recalculate the framerate. */
|
||||
timer.update();
|
||||
/** Update tpf to time per frame according to the Timer. */
|
||||
tpf = timer.getTimePerFrame();
|
||||
timer.update(); //Recalculate the framerate.
|
||||
tpf = timer.getTimePerFrame(); //Update tpf to time per frame according to the Timer.
|
||||
|
||||
// Execute updateQueue item
|
||||
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.UPDATE).execute();
|
||||
|
||||
GameStateManager.getInstance().update(tpf);
|
||||
|
||||
|
||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand(
|
||||
"mem_report", false ) ) {
|
||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("mem_report", false ) ) {
|
||||
long totMem = Runtime.getRuntime().totalMemory();
|
||||
long freeMem = Runtime.getRuntime().freeMemory();
|
||||
long maxMem = Runtime.getRuntime().maxMemory();
|
||||
|
|
@ -131,27 +116,22 @@ public class SpaceGame2 extends BaseGame {
|
|||
logger.info("Free memory: "+(freeMem>>10)+" kb");
|
||||
logger.info("Max memory: "+(maxMem>>10)+" kb");
|
||||
}
|
||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "exit",
|
||||
false ) ) {
|
||||
|
||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "exit", false ) ) {
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clears stats, the buffers and renders bounds and normals if on.
|
||||
*
|
||||
* @param interpolation unused in this implementation
|
||||
* @see AbstractGame#render(float interpolation)
|
||||
*/
|
||||
protected void render( float interpolation ) {
|
||||
Renderer r = display.getRenderer();
|
||||
/** Clears the previously rendered information. */
|
||||
r.clearBuffers();
|
||||
r.clearBuffers(); //Clears the previously rendered information.
|
||||
|
||||
// Execute renderQueue item
|
||||
GameTaskQueueManager.getManager().getQueue(GameTaskQueue.RENDER).execute();
|
||||
|
||||
GameStateManager.getInstance().render(tpf);
|
||||
}
|
||||
|
||||
|
|
@ -162,12 +142,10 @@ public class SpaceGame2 extends BaseGame {
|
|||
|
||||
/**
|
||||
* Cleans up the keyboard.
|
||||
*
|
||||
* @see AbstractGame#cleanup()
|
||||
*/
|
||||
protected void cleanup() {
|
||||
logger.info( "Cleaning up resources." );
|
||||
|
||||
TextureManager.doTextureCleanup();
|
||||
if (display != null && display.getRenderer() != null)
|
||||
display.getRenderer().cleanup();
|
||||
|
|
|
|||
|
|
@ -1,12 +1,75 @@
|
|||
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.scene.Node;
|
||||
import com.jmex.physics.DynamicPhysicsNode;
|
||||
|
||||
|
||||
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