Implement Custom Chase Camera for platforming gameplay.
This commit is contained in:
parent
c81e5bd0a0
commit
7a55f871bd
@ -28,6 +28,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import static mygame.Main.main;
|
import static mygame.Main.main;
|
||||||
|
import mygame.camera.CustomChaseCamera;
|
||||||
import mygame.control.NetworkPlayableCharacter;
|
import mygame.control.NetworkPlayableCharacter;
|
||||||
import mygame.control.PhysicsControl;
|
import mygame.control.PhysicsControl;
|
||||||
import mygame.control.PlayableCharacter;
|
import mygame.control.PlayableCharacter;
|
||||||
@ -134,8 +135,11 @@ public class RunLevel extends BaseAppState
|
|||||||
|
|
||||||
//System.out.println("In here.,");
|
//System.out.println("In here.,");
|
||||||
|
|
||||||
ChaseCamera chaseCam = new ChaseCamera(this.app.getCamera(), player, inputManager);
|
CustomChaseCamera chaseCam = new CustomChaseCamera(this.app.getCamera(), player, inputManager);
|
||||||
chaseCam.setLookAtOffset(new Vector3f(0,2.5f,0));
|
chaseCam.setLookAtOffset(new Vector3f(0,2.5f,0));
|
||||||
|
chaseCam.setDefaultHorizontalRotation((float)Math.PI);
|
||||||
|
chaseCam.setSmoothMotion(false);
|
||||||
|
chaseCam.setTrailingEnabled(false);
|
||||||
//this.app.getCamera().setFrustumPerspective(this.app.getCamera().getFr, aspect, near, far);
|
//this.app.getCamera().setFrustumPerspective(this.app.getCamera().getFr, aspect, near, far);
|
||||||
//this.app.getCamera().setFrustumPerspective(90, 16f/9, 0, 2000f);
|
//this.app.getCamera().setFrustumPerspective(90, 16f/9, 0, 2000f);
|
||||||
//float fov = 50;
|
//float fov = 50;
|
||||||
|
63
src/mygame/camera/CustomChaseCamera.java
Normal file
63
src/mygame/camera/CustomChaseCamera.java
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
/*
|
||||||
|
* To change this license header, choose License Headers in Project Properties.
|
||||||
|
* To change this template file, choose Tools | Templates
|
||||||
|
* and open the template in the editor.
|
||||||
|
*/
|
||||||
|
package mygame.camera;
|
||||||
|
|
||||||
|
import com.jme3.input.ChaseCamera;
|
||||||
|
import com.jme3.input.InputManager;
|
||||||
|
import com.jme3.math.FastMath;
|
||||||
|
import com.jme3.math.Vector3f;
|
||||||
|
import com.jme3.renderer.Camera;
|
||||||
|
import com.jme3.scene.Spatial;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author sigon
|
||||||
|
*/
|
||||||
|
public class CustomChaseCamera extends ChaseCamera{
|
||||||
|
|
||||||
|
public boolean canZoom = true;
|
||||||
|
|
||||||
|
public CustomChaseCamera(Camera cam, Spatial target, InputManager inputManager) {
|
||||||
|
super(cam, target, inputManager);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHorizontalRotation(float newRotation) {
|
||||||
|
targetRotation = newRotation;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
public float getRotation() {
|
||||||
|
return targetRotation;
|
||||||
|
}*/
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void updateCamera(float tpf) {
|
||||||
|
super.updateCamera(tpf);
|
||||||
|
targetRotation = (float)(targetRotation % (2*Math.PI));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void zoomCamera(float value) {
|
||||||
|
if (!canZoom || !enabled) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
zooming = false;
|
||||||
|
targetDistance += value * zoomSensitivity;
|
||||||
|
if (targetDistance > maxDistance) {
|
||||||
|
targetDistance = maxDistance;
|
||||||
|
}
|
||||||
|
if (targetDistance < minDistance) {
|
||||||
|
targetDistance = minDistance;
|
||||||
|
}
|
||||||
|
if (veryCloseRotation) {
|
||||||
|
if ((targetVRotation < minVerticalRotation) && (targetDistance > (minDistance + 1.0f))) {
|
||||||
|
targetVRotation = minVerticalRotation;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
distance = targetDistance;
|
||||||
|
System.out.println(targetDistance);
|
||||||
|
}
|
||||||
|
}
|
@ -8,6 +8,7 @@ import com.jme3.collision.CollisionResults;
|
|||||||
import com.jme3.export.JmeExporter;
|
import com.jme3.export.JmeExporter;
|
||||||
import com.jme3.export.JmeImporter;
|
import com.jme3.export.JmeImporter;
|
||||||
import com.jme3.export.Savable;
|
import com.jme3.export.Savable;
|
||||||
|
import com.jme3.input.ChaseCamera;
|
||||||
import com.jme3.input.KeyInput;
|
import com.jme3.input.KeyInput;
|
||||||
import com.jme3.input.controls.ActionListener;
|
import com.jme3.input.controls.ActionListener;
|
||||||
import com.jme3.input.controls.AnalogListener;
|
import com.jme3.input.controls.AnalogListener;
|
||||||
@ -27,6 +28,7 @@ import com.jme3.scene.control.Control;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import static mygame.Main.level;
|
import static mygame.Main.level;
|
||||||
import static mygame.Main.main;
|
import static mygame.Main.main;
|
||||||
|
import mygame.camera.CustomChaseCamera;
|
||||||
import mygame.server.ServerMain.PlayerActionMessage;
|
import mygame.server.ServerMain.PlayerActionMessage;
|
||||||
import mygame.server.ServerMain.PlayerPositionMessage;
|
import mygame.server.ServerMain.PlayerPositionMessage;
|
||||||
import mygame.server.ServerMain.ServerMessage;
|
import mygame.server.ServerMain.ServerMessage;
|
||||||
@ -52,6 +54,11 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
PhysicsControl physics;
|
PhysicsControl physics;
|
||||||
Vector3f walkDirection;
|
Vector3f walkDirection;
|
||||||
|
|
||||||
|
float lastActionPerformed = 0.0f;
|
||||||
|
static final float FREECAMERATIME = 0.5f;
|
||||||
|
float cameraTransition = 0.0f;
|
||||||
|
float oldRotation = 0.0f;
|
||||||
|
|
||||||
// empty serialization constructor
|
// empty serialization constructor
|
||||||
|
|
||||||
/** This method is called when the control is added to the spatial,
|
/** This method is called when the control is added to the spatial,
|
||||||
@ -87,8 +94,8 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
main.getInputManager().addMapping("StrafeLeft", new KeyTrigger(KeyInput.KEY_A));
|
main.getInputManager().addMapping("StrafeLeft", new KeyTrigger(KeyInput.KEY_A));
|
||||||
main.getInputManager().addMapping("StrafeRight", new KeyTrigger(KeyInput.KEY_D));
|
main.getInputManager().addMapping("StrafeRight", new KeyTrigger(KeyInput.KEY_D));
|
||||||
main.getInputManager().addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
|
main.getInputManager().addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
|
||||||
main.getInputManager().addListener(this, "WalkForward");
|
//main.getInputManager().addListener(this, "WalkForward");
|
||||||
main.getInputManager().addListener(this, "WalkBackward");
|
//main.getInputManager().addListener(this, "WalkBackward");
|
||||||
main.getInputManager().addListener(this, "StrafeRight");
|
main.getInputManager().addListener(this, "StrafeRight");
|
||||||
main.getInputManager().addListener(this, "StrafeLeft");
|
main.getInputManager().addListener(this, "StrafeLeft");
|
||||||
main.getInputManager().addListener(this, "Jump");
|
main.getInputManager().addListener(this, "Jump");
|
||||||
@ -108,7 +115,13 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
/*if (this instanceof NetworkPlayableCharacter) {
|
/*if (this instanceof NetworkPlayableCharacter) {
|
||||||
System.out.println("1:"+getWalkDirection()+"Moving:"+moving+"/"+strafingLeft+"/"+strafingRight+"/"+walkingBackward+"/"+walkingForward);
|
System.out.println("1:"+getWalkDirection()+"Moving:"+moving+"/"+strafingLeft+"/"+strafingRight+"/"+walkingBackward+"/"+walkingForward);
|
||||||
}*/
|
}*/
|
||||||
|
main.getCamera().setLocation(spatial.getLocalTranslation().add(-20,7f,0));
|
||||||
if (moving) {
|
if (moving) {
|
||||||
|
if (!(this instanceof NetworkPlayableCharacter)) {
|
||||||
|
cameraTransition+=tpf*4;
|
||||||
|
spatial.getControl(CustomChaseCamera.class).setHorizontalRotation(FastMath.interpolateLinear(cameraTransition, (float)(oldRotation), (float)Math.PI));
|
||||||
|
lastActionPerformed = 0.0f;
|
||||||
|
}
|
||||||
if (!channel.getAnimationName().equalsIgnoreCase("Walk")) {
|
if (!channel.getAnimationName().equalsIgnoreCase("Walk")) {
|
||||||
channel.setAnim("Walk");
|
channel.setAnim("Walk");
|
||||||
channel.setLoopMode(LoopMode.Loop);
|
channel.setLoopMode(LoopMode.Loop);
|
||||||
@ -132,8 +145,16 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
channel.setAnim("stand");
|
channel.setAnim("stand");
|
||||||
channel.setLoopMode(LoopMode.DontLoop);
|
channel.setLoopMode(LoopMode.DontLoop);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
if (!(this instanceof NetworkPlayableCharacter)) {
|
||||||
|
//System.out.println(spatial.getControl(CustomChaseCamera.class).getHorizontalRotation()+","+(spatial.getControl(CustomChaseCamera.class).getHorizontalRotation()%(2*Math.PI)));
|
||||||
|
oldRotation = spatial.getControl(CustomChaseCamera.class).getHorizontalRotation();
|
||||||
|
cameraTransition = 0.0f;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!(this instanceof NetworkPlayableCharacter)) {
|
||||||
|
lastActionPerformed+=tpf;
|
||||||
}
|
}
|
||||||
//isOnGround();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void SmoothMoveWalk(Vector3f walkDirection, float tpf) {
|
private void SmoothMoveWalk(Vector3f walkDirection, float tpf) {
|
||||||
@ -174,6 +195,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onAction(String name, boolean isPressed, float tpf) {
|
public void onAction(String name, boolean isPressed, float tpf) {
|
||||||
|
lastActionPerformed = 0.0f;
|
||||||
switch (name) {
|
switch (name) {
|
||||||
case "StrafeLeft":{
|
case "StrafeLeft":{
|
||||||
current_time = 0.0f;
|
current_time = 0.0f;
|
||||||
@ -195,7 +217,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
main.client.send(action);
|
main.client.send(action);
|
||||||
}
|
}
|
||||||
}break;
|
}break;
|
||||||
case "WalkBackward":{
|
/*case "WalkBackward":{
|
||||||
current_time = 0.0f;
|
current_time = 0.0f;
|
||||||
prevRot = spatial.getLocalRotation();
|
prevRot = spatial.getLocalRotation();
|
||||||
walkingBackward = isPressed;
|
walkingBackward = isPressed;
|
||||||
@ -214,7 +236,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
PlayerActionMessage action = new PlayerActionMessage(name,Boolean.toString(isPressed),main.client.getId(),spatial.getLocalTranslation(),spatial.getLocalRotation(),main.getCamera().getDirection(),main.getCamera().getLeft());
|
PlayerActionMessage action = new PlayerActionMessage(name,Boolean.toString(isPressed),main.client.getId(),spatial.getLocalTranslation(),spatial.getLocalRotation(),main.getCamera().getDirection(),main.getCamera().getLeft());
|
||||||
main.client.send(action);
|
main.client.send(action);
|
||||||
}
|
}
|
||||||
}break;
|
}break;*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -254,22 +276,22 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
|
|||||||
Vector3f walkDirection = new Vector3f(0,0,0);
|
Vector3f walkDirection = new Vector3f(0,0,0);
|
||||||
|
|
||||||
if (strafingLeft) {
|
if (strafingLeft) {
|
||||||
walkDirection.addLocal(camLeftDir);
|
walkDirection.addLocal(0,0,-1);
|
||||||
moving=true;
|
moving=true;
|
||||||
}
|
}
|
||||||
if (strafingRight) {
|
if (strafingRight) {
|
||||||
walkDirection.addLocal(camLeftDir.negate());
|
walkDirection.addLocal(0,0,1);
|
||||||
moving=true;
|
moving=true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (walkingForward) {
|
/*if (walkingForward) {
|
||||||
walkDirection.addLocal(camDir);
|
walkDirection.addLocal(camDir);
|
||||||
moving=true;
|
moving=true;
|
||||||
}
|
}
|
||||||
if (walkingBackward) {
|
if (walkingBackward) {
|
||||||
walkDirection.addLocal(camDir.negate());
|
walkDirection.addLocal(camDir.negate());
|
||||||
moving=true;
|
moving=true;
|
||||||
}
|
} */
|
||||||
return walkDirection;
|
return walkDirection;
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user