diff --git a/assets/Scenes/TestLevel.j3o b/assets/Scenes/TestLevel.j3o index 65061fd..fb1b727 100644 Binary files a/assets/Scenes/TestLevel.j3o and b/assets/Scenes/TestLevel.j3o differ diff --git a/build/classes/mygame/Main.class b/build/classes/mygame/Main.class index 98689ab..ae7fb9f 100644 Binary files a/build/classes/mygame/Main.class and b/build/classes/mygame/Main.class differ diff --git a/src/mygame/camera/CustomChaseCamera.java b/src/mygame/camera/CustomChaseCamera.java index 478fa7c..e69d42e 100644 --- a/src/mygame/camera/CustomChaseCamera.java +++ b/src/mygame/camera/CustomChaseCamera.java @@ -58,6 +58,6 @@ public class CustomChaseCamera extends ChaseCamera{ } } distance = targetDistance; - System.out.println(targetDistance); + //System.out.println(targetDistance); } } diff --git a/src/mygame/control/PhysicsControl.java b/src/mygame/control/PhysicsControl.java index c6febb6..53ba587 100644 --- a/src/mygame/control/PhysicsControl.java +++ b/src/mygame/control/PhysicsControl.java @@ -12,6 +12,7 @@ import com.jme3.renderer.RenderManager; import com.jme3.renderer.ViewPort; import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue.ShadowMode; +import com.jme3.scene.Geometry; import com.jme3.scene.Node; import com.jme3.scene.Spatial; import com.jme3.scene.control.AbstractControl; @@ -23,14 +24,22 @@ import static mygame.appstate.RunLevel.world; public class PhysicsControl extends AbstractControl implements Savable, Cloneable { + final static float FRICTION = 0.4f; + float jumpSpd = 0.1f; float vspd = 0.0f; + float hspd = 0.0f; //Determines movement along the Z axis. float gravity = -0.25f; + float acceleration = 80f; //80 units/s acceleration. + float maxSpd = 10f; //Default max speed is 10. + float modelHeight = 2.5f; float walkOffTime = 0.25f; //How long you can jump after becoming airborne. float airTime = 0.0f; //Amount of time in air. + + Geometry standingOn; public PhysicsControl(float jumpSpd, float gravity, float modelHeight){ this.jumpSpd=jumpSpd; @@ -62,8 +71,21 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl } else { vspd=0; airTime=0; + if (standingOn!=null && standingOn.getUserData("friction")!=null) { + if (hspd<0) { + hspd=Math.min(hspd+(float)standingOn.getUserData("friction"),0); + } else { + hspd=Math.max(hspd-(float)standingOn.getUserData("friction"),0); + } + } else { + if (hspd<0) { + hspd=Math.min(hspd+FRICTION,0); + } else { + hspd=Math.max(hspd-FRICTION,0); + } + } } - spatial.move(0,vspd,0); + spatial.move(0,vspd,hspd*tpf); } @Override @@ -112,6 +134,52 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl return vspd; } + void addHorizontalSpeed(float spd) { + hspd += spd; + } + void setHorizontalSpeed(float spd) { + hspd = spd; + } + + void setAcceleration(float value) { + acceleration = value; + } + void setMaxSpd(float value) { + maxSpd = value; + } + + float getAcceleration() { + return acceleration; + } + float getMaxSpd() { + return maxSpd; + } + + /*** + * + * @param positive Use false if we are accelerating in the -Z direction. (Left). Use true to say we are accelerating to the right. + * @param tpf + */ + void accelerate(boolean right, float tpf) { + if (right) { + if (hspd+acceleration*tpf-maxSpd) { + hspd -= acceleration*tpf; + } else { + hspd = -maxSpd; + } + } + } + + float getHorizontalSpeed() { + return hspd; + } + boolean isOnGround() { if (vspd>0) { //System.out.println(vspd); @@ -137,6 +205,7 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl //System.out.println(newResults.getClosestCollision()); if (newResults.getClosestCollision().getDistance()<=(modelHeight/2)+0.1-vspd) { spatial.setLocalTranslation(newResults.getClosestCollision().getContactPoint()); + standingOn = newResults.getClosestCollision().getGeometry(); return true; } else { return false; diff --git a/src/mygame/control/PlayableCharacter.java b/src/mygame/control/PlayableCharacter.java index 60ec6a5..252494d 100644 --- a/src/mygame/control/PlayableCharacter.java +++ b/src/mygame/control/PlayableCharacter.java @@ -52,7 +52,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone AnimControl control; PhysicsControl physics; - Vector3f walkDirection; + Vector3f walkDirection = Vector3f.ZERO.clone(); float lastActionPerformed = 0.0f; static final float FREECAMERATIME = 0.5f; @@ -115,7 +115,15 @@ 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)); + //main.getCamera().setLocation(spatial.getLocalTranslation().add(-20,7f,0)); + if (physics.getHorizontalSpeed()!=0.0f) { + SmoothMoveWalk(walkDirection, tpf); + //Message msg = new PlayerPositionMessage(spatial.getLocalTranslation()); + //main.client.send(msg); + } else { + channel.setAnim("stand"); + channel.setLoopMode(LoopMode.DontLoop); + } if (moving) { if (!(this instanceof NetworkPlayableCharacter)) { cameraTransition+=tpf*4; @@ -129,22 +137,10 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone moving=false; - if (this instanceof NetworkPlayableCharacter) { - walkDirection = getWalkDirection((Vector3f)spatial.getUserData("lastCamDir"),(Vector3f)spatial.getUserData("lastCamLeftDir")); - } else { - walkDirection = getWalkDirection(main.getCamera().getDirection(),main.getCamera().getLeft()); - } + walkDirection = getWalkDirection(tpf); /*if (this instanceof NetworkPlayableCharacter) { System.out.println(" 2:"+getWalkDirection()+"Moving:"+moving+"/"+strafingLeft+"/"+strafingRight+"/"+walkingBackward+"/"+walkingForward); }*/ - if (moving) { - SmoothMoveWalk(walkDirection, tpf); - //Message msg = new PlayerPositionMessage(spatial.getLocalTranslation()); - //main.client.send(msg); - } else { - 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))); @@ -269,18 +265,18 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone public void onAnimChange(AnimControl control, AnimChannel channel, String animName) { } - public Vector3f getWalkDirection(Vector3f camDir, Vector3f camLeftDir) { - camDir.y=0; camDir.normalizeLocal(); - camLeftDir.y=0; camLeftDir.normalizeLocal(); - - Vector3f walkDirection = new Vector3f(0,0,0); + public Vector3f getWalkDirection(float tpf) { if (strafingLeft) { - walkDirection.addLocal(0,0,-1); + this.walkDirection.setZ(-1); + //physics.setHorizontalSpeed(-speed); + physics.accelerate(false, tpf); moving=true; } if (strafingRight) { - walkDirection.addLocal(0,0,1); + this.walkDirection.setZ(1); + //physics.setHorizontalSpeed(speed); + physics.accelerate(true, tpf); moving=true; }