Merge pull request #2 from sigonasr2/custom_camera

Custom camera
pull/3/head
sigonasr2 4 years ago committed by GitHub
commit dea04b59cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      .gitignore
  2. 6
      src/mygame/appstate/RunLevel.java
  3. 63
      src/mygame/camera/CustomChaseCamera.java
  4. 40
      src/mygame/control/PlayableCharacter.java

2
.gitignore vendored

@ -1 +1,3 @@
/dist/
/assets/Models/model5/model5.j3o
/assets/Models/model5/model5.j3odata

@ -28,6 +28,7 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static mygame.Main.main;
import mygame.camera.CustomChaseCamera;
import mygame.control.NetworkPlayableCharacter;
import mygame.control.PhysicsControl;
import mygame.control.PlayableCharacter;
@ -134,8 +135,11 @@ public class RunLevel extends BaseAppState
//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.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(90, 16f/9, 0, 2000f);
//float fov = 50;

@ -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.JmeImporter;
import com.jme3.export.Savable;
import com.jme3.input.ChaseCamera;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
@ -27,6 +28,7 @@ import com.jme3.scene.control.Control;
import java.io.IOException;
import static mygame.Main.level;
import static mygame.Main.main;
import mygame.camera.CustomChaseCamera;
import mygame.server.ServerMain.PlayerActionMessage;
import mygame.server.ServerMain.PlayerPositionMessage;
import mygame.server.ServerMain.ServerMessage;
@ -52,6 +54,11 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
PhysicsControl physics;
Vector3f walkDirection;
float lastActionPerformed = 0.0f;
static final float FREECAMERATIME = 0.5f;
float cameraTransition = 0.0f;
float oldRotation = 0.0f;
// empty serialization constructor
/** 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("StrafeRight", new KeyTrigger(KeyInput.KEY_D));
main.getInputManager().addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
main.getInputManager().addListener(this, "WalkForward");
main.getInputManager().addListener(this, "WalkBackward");
//main.getInputManager().addListener(this, "WalkForward");
//main.getInputManager().addListener(this, "WalkBackward");
main.getInputManager().addListener(this, "StrafeRight");
main.getInputManager().addListener(this, "StrafeLeft");
main.getInputManager().addListener(this, "Jump");
@ -108,7 +115,13 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
/*if (this instanceof NetworkPlayableCharacter) {
System.out.println("1:"+getWalkDirection()+"Moving:"+moving+"/"+strafingLeft+"/"+strafingRight+"/"+walkingBackward+"/"+walkingForward);
}*/
main.getCamera().setLocation(spatial.getLocalTranslation().add(-20,7f,0));
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")) {
channel.setAnim("Walk");
channel.setLoopMode(LoopMode.Loop);
@ -132,8 +145,16 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
channel.setAnim("stand");
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) {
@ -174,6 +195,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
@Override
public void onAction(String name, boolean isPressed, float tpf) {
lastActionPerformed = 0.0f;
switch (name) {
case "StrafeLeft":{
current_time = 0.0f;
@ -195,7 +217,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
main.client.send(action);
}
}break;
case "WalkBackward":{
/*case "WalkBackward":{
current_time = 0.0f;
prevRot = spatial.getLocalRotation();
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());
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);
if (strafingLeft) {
walkDirection.addLocal(camLeftDir);
walkDirection.addLocal(0,0,-1);
moving=true;
}
if (strafingRight) {
walkDirection.addLocal(camLeftDir.negate());
walkDirection.addLocal(0,0,1);
moving=true;
}
if (walkingForward) {
/*if (walkingForward) {
walkDirection.addLocal(camDir);
moving=true;
}
if (walkingBackward) {
walkDirection.addLocal(camDir.negate());
moving=true;
}
} */
return walkDirection;
}
}
Loading…
Cancel
Save