Added Third person cam and input
This commit is contained in:
parent
e70945a5d9
commit
97f718b585
6 changed files with 194 additions and 52 deletions
|
|
@ -38,7 +38,7 @@ public class SpaceGame extends SimplePhysicsGame {
|
||||||
rootNode.attachChild(shipNode);
|
rootNode.attachChild(shipNode);
|
||||||
|
|
||||||
//environment
|
//environment
|
||||||
environment = new Environment(rootNode);
|
environment = new Environment(rootNode, getPhysicsSpace());
|
||||||
rootNode.attachChild(environment);
|
rootNode.attachChild(environment);
|
||||||
|
|
||||||
//light
|
//light
|
||||||
|
|
|
||||||
12
src/sg/env/Environment.java
vendored
12
src/sg/env/Environment.java
vendored
|
|
@ -11,6 +11,8 @@ import com.jme.scene.state.TextureState;
|
||||||
import com.jme.system.DisplaySystem;
|
import com.jme.system.DisplaySystem;
|
||||||
import com.jmex.effects.LensFlare;
|
import com.jmex.effects.LensFlare;
|
||||||
import com.jmex.effects.LensFlareFactory;
|
import com.jmex.effects.LensFlareFactory;
|
||||||
|
import com.jmex.physics.PhysicsSpace;
|
||||||
|
import com.jmex.physics.StaticPhysicsNode;
|
||||||
|
|
||||||
public class Environment extends Node {
|
public class Environment extends Node {
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1L;
|
||||||
|
|
@ -20,13 +22,15 @@ public class Environment extends Node {
|
||||||
* */
|
* */
|
||||||
private Node localNode;
|
private Node localNode;
|
||||||
|
|
||||||
public Environment(Node parentNode){
|
public Environment(Node parentNode, PhysicsSpace physicsSpace){
|
||||||
buildLocalNode();
|
buildLocalNode();
|
||||||
buildEnvoriment();
|
buildEnvoriment(physicsSpace);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void buildEnvoriment(){
|
private void buildEnvoriment(PhysicsSpace physicsSpace){
|
||||||
Node station = SGUtil.loadModel("sg/data/models/stations/station.3ds");
|
StaticPhysicsNode station = physicsSpace.createStaticNode();
|
||||||
|
Node stationModel = SGUtil.loadModel("sg/data/models/stations/station.3ds");
|
||||||
|
station.attachChild(stationModel);
|
||||||
station.setLocalTranslation(new Vector3f( 500, 0, 0));
|
station.setLocalTranslation(new Vector3f( 500, 0, 0));
|
||||||
this.attachChild(station);
|
this.attachChild(station);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,21 +3,43 @@ package sg.input;
|
||||||
import com.jme.input.InputHandler;
|
import com.jme.input.InputHandler;
|
||||||
import com.jme.input.KeyBindingManager;
|
import com.jme.input.KeyBindingManager;
|
||||||
import com.jme.input.KeyInput;
|
import com.jme.input.KeyInput;
|
||||||
|
import com.jme.math.Vector3f;
|
||||||
|
import com.jme.scene.Node;
|
||||||
|
import com.jmex.physics.DynamicPhysicsNode;
|
||||||
|
|
||||||
public abstract class GlobalInputHandler extends InputHandler {
|
public abstract class GlobalInputHandler extends InputHandler {
|
||||||
|
protected final Vector3f appliedForce;
|
||||||
|
protected Node shipNode;
|
||||||
|
protected DynamicPhysicsNode shipPhysNode;
|
||||||
|
|
||||||
public GlobalInputHandler(){
|
public GlobalInputHandler(Node shipNode, DynamicPhysicsNode shipPhysNode){
|
||||||
|
appliedForce = new Vector3f();
|
||||||
|
this.shipNode = shipNode;
|
||||||
|
this.shipPhysNode = shipPhysNode;
|
||||||
|
|
||||||
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
||||||
manager.set("input_first", KeyInput.KEY_F1);
|
manager.set("input_first", KeyInput.KEY_F1);
|
||||||
manager.set("input_third", KeyInput.KEY_F2);
|
manager.set("input_third", KeyInput.KEY_F2);
|
||||||
|
|
||||||
|
manager.set("accelerate", KeyInput.KEY_W);
|
||||||
|
manager.set("break", KeyInput.KEY_S);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void update(float tpf){
|
public void update(float tpf){
|
||||||
super.update(tpf);
|
super.update(tpf);
|
||||||
/*
|
/*
|
||||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "input_first", false ) ) {
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand( "input_first", false ) ) {
|
||||||
this = new FirstPersonHandler(ship, cam);
|
this = new FirstPersonHandler(ship, cam);
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
Vector3f rotation = shipPhysNode.getWorldRotation().getRotationColumn(0);
|
||||||
|
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("accelerate", true ) ) {
|
||||||
|
appliedForce.set(rotation.mult(700)).multLocal(tpf);
|
||||||
|
shipPhysNode.addForce(appliedForce);
|
||||||
|
}
|
||||||
|
else if ( KeyBindingManager.getKeyBindingManager().isValidCommand("break", true ) ) {
|
||||||
|
appliedForce.set(rotation.mult(-700)).multLocal(tpf);
|
||||||
|
shipPhysNode.addForce(appliedForce);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,22 +2,16 @@ package sg.input;
|
||||||
|
|
||||||
import com.jme.input.KeyBindingManager;
|
import com.jme.input.KeyBindingManager;
|
||||||
import com.jme.input.KeyInput;
|
import com.jme.input.KeyInput;
|
||||||
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;
|
import com.jmex.physics.DynamicPhysicsNode;
|
||||||
|
|
||||||
|
|
||||||
public class SGFirstPersonHandler extends GlobalInputHandler{
|
public class SGFirstPersonHandler extends GlobalInputHandler{
|
||||||
private final Vector3f appliedForce = new Vector3f();
|
|
||||||
private Vector3f rotation;
|
|
||||||
private Node ship;
|
|
||||||
private DynamicPhysicsNode shipNode;
|
|
||||||
private Camera cam;
|
private Camera cam;
|
||||||
|
|
||||||
public SGFirstPersonHandler(Node ship, DynamicPhysicsNode shipNode, Camera cam){
|
public SGFirstPersonHandler(Node ship, DynamicPhysicsNode shipPhysNode, Camera cam){
|
||||||
this.ship = ship;
|
super(ship, shipPhysNode);
|
||||||
this.shipNode = shipNode;
|
|
||||||
this.cam = cam;
|
this.cam = cam;
|
||||||
|
|
||||||
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
KeyBindingManager manager = KeyBindingManager.getKeyBindingManager();
|
||||||
|
|
@ -33,15 +27,6 @@ public class SGFirstPersonHandler extends GlobalInputHandler{
|
||||||
|
|
||||||
public void update(float time){
|
public void update(float time){
|
||||||
super.update(time);
|
super.update(time);
|
||||||
rotation = ship.getLocalRotation().getRotationColumn(2);
|
|
||||||
if ( KeyBindingManager.getKeyBindingManager().isValidCommand("forw", true ) ) {
|
|
||||||
appliedForce.set(rotation.mult(-700)).multLocal(time);
|
|
||||||
shipNode.addForce(appliedForce);
|
|
||||||
}
|
|
||||||
else if ( KeyBindingManager.getKeyBindingManager().isValidCommand("backw", true ) ) {
|
|
||||||
appliedForce.set(rotation.mult(700)).multLocal(time);
|
|
||||||
shipNode.addForce(appliedForce);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package sg.input;
|
package sg.input;
|
||||||
|
|
||||||
import com.jme.input.ChaseCamera;
|
import com.jme.input.ChaseCamera;
|
||||||
|
import com.jme.input.MouseInput;
|
||||||
import com.jme.input.RelativeMouse;
|
import com.jme.input.RelativeMouse;
|
||||||
import com.jme.input.action.InputActionEvent;
|
import com.jme.input.action.InputActionEvent;
|
||||||
import com.jme.input.thirdperson.ThirdPersonMouseLook;
|
import com.jme.input.thirdperson.ThirdPersonMouseLook;
|
||||||
|
|
@ -10,17 +11,24 @@ 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.jme.scene.Spatial;
|
import com.jme.scene.Spatial;
|
||||||
|
import com.jmex.physics.DynamicPhysicsNode;
|
||||||
|
|
||||||
|
|
||||||
public class SGThirdPersonHandler extends GlobalInputHandler{
|
public class SGThirdPersonHandler extends GlobalInputHandler{
|
||||||
protected Camera cam;
|
protected Camera cam;
|
||||||
protected Node ship;
|
|
||||||
|
|
||||||
public SGThirdPersonHandler(Camera cam, Spatial ship){
|
public SGThirdPersonHandler(Node ship, DynamicPhysicsNode shipPhysNode, Camera cam){
|
||||||
|
super(ship, shipPhysNode);
|
||||||
|
|
||||||
SGChaseCamera ccam = new SGChaseCamera(cam, ship);
|
SGChaseCamera ccam = new SGChaseCamera(cam, ship);
|
||||||
|
ccam.getMouseLook().setRotateTarget(true);
|
||||||
|
ccam.getMouseLook().setTargetTurnSpeed(FastMath.PI);
|
||||||
this.addToAttachedHandlers(ccam);
|
this.addToAttachedHandlers(ccam);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//********************************************************************************
|
||||||
|
//********************************************************************************
|
||||||
|
|
||||||
class SGChaseCamera extends ChaseCamera{
|
class SGChaseCamera extends ChaseCamera{
|
||||||
public SGChaseCamera(Camera arg0, Spatial arg1) {
|
public SGChaseCamera(Camera arg0, Spatial arg1) {
|
||||||
super(arg0, arg1);
|
super(arg0, arg1);
|
||||||
|
|
@ -42,7 +50,7 @@ public class SGThirdPersonHandler extends GlobalInputHandler{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//********************************************************************************
|
||||||
class SGThirdPersonMouseLook extends ThirdPersonMouseLook{
|
class SGThirdPersonMouseLook extends ThirdPersonMouseLook{
|
||||||
float targetTurnSpeed = 1f;
|
float targetTurnSpeed = 1f;
|
||||||
|
|
||||||
|
|
@ -50,29 +58,148 @@ public class SGThirdPersonHandler extends GlobalInputHandler{
|
||||||
super(mouse, camera, target);
|
super(mouse, camera, target);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void performAction(InputActionEvent event) {
|
public void performAction(InputActionEvent event) {
|
||||||
super.performAction(event);
|
if (!enabled)
|
||||||
|
return;
|
||||||
|
|
||||||
//if (mouse.getLocalTranslation().x != 0 || mouse.getLocalTranslation().y != 0) {
|
float time = 0.01f;
|
||||||
Vector3f camL = camera.getCamera().getLocation();
|
if (lookMouse == -1 || MouseInput.get().isButtonDown(lookMouse)) {
|
||||||
//center coordinates to spatial
|
camera.setLooking(true);
|
||||||
float x = target.getLocalTranslation().x - camL.x;
|
if (mouse.getLocalTranslation().x != 0) {
|
||||||
float z = target.getLocalTranslation().z - camL.z;
|
float amount;
|
||||||
float y = target.getLocalTranslation().y - camL.y;
|
if(invertRotate) {
|
||||||
float c = FastMath.sqrt(x*x + z*z);
|
amount = -time * mouse.getLocalTranslation().x;
|
||||||
|
} else {
|
||||||
|
amount = time * mouse.getLocalTranslation().x;
|
||||||
|
}
|
||||||
|
rotateRight(amount, time);
|
||||||
|
updated = true;
|
||||||
|
} else if (rotateTarget)
|
||||||
|
rotateRight(0, time);
|
||||||
|
if (!lockAscent && mouse.getLocalTranslation().y != 0) {
|
||||||
|
float amount = time * mouse.getLocalTranslation().y;
|
||||||
|
rotateUp(amount);
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
} else camera.setLooking(false);
|
||||||
|
|
||||||
// Tower rotation
|
updateFromJoystick(time);
|
||||||
float angle = FastMath.acos(x/c);
|
|
||||||
if(camL.z < 0 && angle > 0) angle = -Math.abs(angle);
|
int wdelta = MouseInput.get().getWheelDelta();
|
||||||
if(camL.z > 0 && angle < 0) angle = Math.abs(angle);
|
if (wdelta != 0) {
|
||||||
target.getLocalRotation().fromAngleAxis(angle+FastMath.PI, new Vector3f(0,0,1));
|
float amount = time * -wdelta;
|
||||||
|
rollIn(amount);
|
||||||
|
updated = true;
|
||||||
|
}
|
||||||
|
|
||||||
//gun rotation
|
if (updated)
|
||||||
c = FastMath.sqrt(x*x + y*y + z*z);
|
camera.getCamera().onFrameChange();
|
||||||
angle = FastMath.tan(y/c);
|
}
|
||||||
target.getLocalRotation().fromAngleAxis(angle+FastMath.PI/5, new Vector3f(0,1,0));
|
|
||||||
//}
|
private void rollIn(float amount) {
|
||||||
|
camera.getIdealSphereCoords().x = clampRollIn(camera
|
||||||
|
.getIdealSphereCoords().x
|
||||||
|
+ (amount * rollInSpeed));
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void rotateRight(float amount, float time) {
|
||||||
|
Vector3f camPos = camera.getCamera().getLocation();
|
||||||
|
Vector3f targetPos = target.getWorldTranslation();
|
||||||
|
|
||||||
|
float azimuthAccel = (amount * mouseXSpeed);
|
||||||
|
difTemp.set(camPos).subtractLocal(targetPos);
|
||||||
|
|
||||||
|
if (worldUpVec.z == 1) {
|
||||||
|
float y = difTemp.y;
|
||||||
|
difTemp.y = difTemp.z;
|
||||||
|
difTemp.z = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
FastMath.cartesianToSpherical(difTemp, sphereTemp);
|
||||||
|
sphereTemp.y = FastMath.normalize(sphereTemp.y + (azimuthAccel),
|
||||||
|
-FastMath.TWO_PI, FastMath.TWO_PI);
|
||||||
|
FastMath.sphericalToCartesian(sphereTemp, rightTemp);
|
||||||
|
|
||||||
|
if (worldUpVec.z == 1) {
|
||||||
|
float y = rightTemp.y;
|
||||||
|
rightTemp.y = rightTemp.z;
|
||||||
|
rightTemp.z = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
rightTemp.addLocal(targetPos);
|
||||||
|
camPos.set(rightTemp);
|
||||||
|
if (camera.isMaintainAzimuth()) {
|
||||||
|
camera.setForceAzimuthUpdate(true);
|
||||||
|
}
|
||||||
|
if (rotateTarget) {
|
||||||
|
//First figure out the current facing vector.
|
||||||
|
target.getLocalRotation().getRotationColumn(0, rightTemp);
|
||||||
|
|
||||||
|
// get angle between vectors
|
||||||
|
rightTemp.normalizeLocal();
|
||||||
|
difTemp.y = 0;
|
||||||
|
difTemp.negateLocal().normalizeLocal();
|
||||||
|
float angle = rightTemp.angleBetween(difTemp);
|
||||||
|
|
||||||
|
// calc how much angle we'll do
|
||||||
|
float maxAngle = targetTurnSpeed * time;
|
||||||
|
if (angle < 0 && -maxAngle > angle) {
|
||||||
|
angle = -maxAngle;
|
||||||
|
} else if (angle > 0 && maxAngle < angle) {
|
||||||
|
angle = maxAngle;
|
||||||
|
}
|
||||||
|
|
||||||
|
//figure out rotation axis by taking cross product
|
||||||
|
Vector3f rotAxis = rightTemp.crossLocal(difTemp).normalizeLocal();
|
||||||
|
|
||||||
|
// Build a rotation quat and apply current local rotation.
|
||||||
|
Quaternion q = rotTemp;
|
||||||
|
q.fromAngleNormalAxis(angle, rotAxis);
|
||||||
|
q.mult(target.getLocalRotation(), target.getLocalRotation());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void rotateUp(float amount) {
|
||||||
|
if (invertedY)
|
||||||
|
amount *= -1;
|
||||||
|
Vector3f camPos = camera.getCamera().getLocation();
|
||||||
|
Vector3f targetPos = target.getWorldTranslation();
|
||||||
|
|
||||||
|
float thetaAccel = (amount * mouseYSpeed);
|
||||||
|
difTemp.set(camPos).subtractLocal(targetPos).subtractLocal(
|
||||||
|
camera.getTargetOffset());
|
||||||
|
|
||||||
|
if (worldUpVec.z == 1) {
|
||||||
|
float y = difTemp.y;
|
||||||
|
difTemp.y = difTemp.z;
|
||||||
|
difTemp.z = y;
|
||||||
|
}
|
||||||
|
|
||||||
|
FastMath.cartesianToSpherical(difTemp, sphereTemp);
|
||||||
|
camera.getIdealSphereCoords().z = clampUpAngle(sphereTemp.z
|
||||||
|
+ (thetaAccel));
|
||||||
|
}
|
||||||
|
|
||||||
|
private float clampUpAngle(float r) {
|
||||||
|
if (Float.isInfinite(r) || Float.isNaN(r))
|
||||||
|
return r;
|
||||||
|
if (r > maxAscent)
|
||||||
|
r = maxAscent;
|
||||||
|
else if (r < minAscent)
|
||||||
|
r = minAscent;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
|
private float clampRollIn(float r) {
|
||||||
|
if (Float.isInfinite(r) || Float.isNaN(r))
|
||||||
|
return 100f;
|
||||||
|
if (r > maxRollOut)
|
||||||
|
r = maxRollOut;
|
||||||
|
else if (r < minRollOut)
|
||||||
|
r = minRollOut;
|
||||||
|
return r;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ import sg.util.SGUtil;
|
||||||
|
|
||||||
import com.jme.input.InputHandler;
|
import com.jme.input.InputHandler;
|
||||||
import com.jme.light.PointLight;
|
import com.jme.light.PointLight;
|
||||||
|
import com.jme.math.FastMath;
|
||||||
import com.jme.math.Vector3f;
|
import com.jme.math.Vector3f;
|
||||||
import com.jme.renderer.ColorRGBA;
|
import com.jme.renderer.ColorRGBA;
|
||||||
import com.jme.renderer.Renderer;
|
import com.jme.renderer.Renderer;
|
||||||
|
|
@ -49,16 +50,19 @@ public class InGameState extends CameraGameState {
|
||||||
rootNode.attachChild(staticNode);
|
rootNode.attachChild(staticNode);
|
||||||
|
|
||||||
// environment
|
// environment
|
||||||
environment = new Environment(rootNode);
|
environment = new Environment(rootNode, physicsSpace);
|
||||||
rootNode.attachChild(environment);
|
rootNode.attachChild(environment);
|
||||||
|
|
||||||
// ship
|
// ship
|
||||||
Node ship = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
|
DynamicPhysicsNode ship = physicsSpace.createDynamicNode();
|
||||||
|
Node shipModel = SGUtil.loadModel("sg/data/models/ships/G6.3ds");
|
||||||
|
shipModel.getLocalRotation().fromAngleAxis(-90*FastMath.DEG_TO_RAD, Vector3f.UNIT_Y);
|
||||||
|
ship.attachChild(shipModel);
|
||||||
ship.setLocalTranslation(new Vector3f( 0, 0, 40));
|
ship.setLocalTranslation(new Vector3f( 0, 0, 40));
|
||||||
rootNode.attachChild(ship);
|
rootNode.attachChild(ship);
|
||||||
|
|
||||||
// inputs
|
// inputs
|
||||||
input = new SGThirdPersonHandler( cam, ship );
|
input = new SGThirdPersonHandler( ship, ship ,cam );
|
||||||
|
|
||||||
// lights
|
// lights
|
||||||
/** Set up a basic, default light. */
|
/** Set up a basic, default light. */
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue