Update physics / acceleration to handle horizontal speeds + friction surfaces.

pull/3/head
Joshua Sigona 4 years ago
parent dea04b59cf
commit 4ff13ec965
  1. BIN
      assets/Scenes/TestLevel.j3o
  2. BIN
      build/classes/mygame/Main.class
  3. 2
      src/mygame/camera/CustomChaseCamera.java
  4. 71
      src/mygame/control/PhysicsControl.java
  5. 40
      src/mygame/control/PlayableCharacter.java

Binary file not shown.

Binary file not shown.

@ -58,6 +58,6 @@ public class CustomChaseCamera extends ChaseCamera{
} }
} }
distance = targetDistance; distance = targetDistance;
System.out.println(targetDistance); //System.out.println(targetDistance);
} }
} }

@ -12,6 +12,7 @@ import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort; import com.jme3.renderer.ViewPort;
import com.jme3.renderer.queue.RenderQueue; import com.jme3.renderer.queue.RenderQueue;
import com.jme3.renderer.queue.RenderQueue.ShadowMode; import com.jme3.renderer.queue.RenderQueue.ShadowMode;
import com.jme3.scene.Geometry;
import com.jme3.scene.Node; import com.jme3.scene.Node;
import com.jme3.scene.Spatial; import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl; import com.jme3.scene.control.AbstractControl;
@ -23,14 +24,22 @@ import static mygame.appstate.RunLevel.world;
public class PhysicsControl extends AbstractControl implements Savable, Cloneable { public class PhysicsControl extends AbstractControl implements Savable, Cloneable {
final static float FRICTION = 0.4f;
float jumpSpd = 0.1f; float jumpSpd = 0.1f;
float vspd = 0.0f; float vspd = 0.0f;
float hspd = 0.0f; //Determines movement along the Z axis.
float gravity = -0.25f; float gravity = -0.25f;
float acceleration = 80f; //80 units/s acceleration.
float maxSpd = 10f; //Default max speed is 10.
float modelHeight = 2.5f; float modelHeight = 2.5f;
float walkOffTime = 0.25f; //How long you can jump after becoming airborne. float walkOffTime = 0.25f; //How long you can jump after becoming airborne.
float airTime = 0.0f; //Amount of time in air. float airTime = 0.0f; //Amount of time in air.
Geometry standingOn;
public PhysicsControl(float jumpSpd, float gravity, float modelHeight){ public PhysicsControl(float jumpSpd, float gravity, float modelHeight){
this.jumpSpd=jumpSpd; this.jumpSpd=jumpSpd;
@ -62,8 +71,21 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl
} else { } else {
vspd=0; vspd=0;
airTime=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 @Override
@ -112,6 +134,52 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl
return vspd; 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;
}
} else {
if (hspd-acceleration*tpf>-maxSpd) {
hspd -= acceleration*tpf;
} else {
hspd = -maxSpd;
}
}
}
float getHorizontalSpeed() {
return hspd;
}
boolean isOnGround() { boolean isOnGround() {
if (vspd>0) { if (vspd>0) {
//System.out.println(vspd); //System.out.println(vspd);
@ -137,6 +205,7 @@ public class PhysicsControl extends AbstractControl implements Savable, Cloneabl
//System.out.println(newResults.getClosestCollision()); //System.out.println(newResults.getClosestCollision());
if (newResults.getClosestCollision().getDistance()<=(modelHeight/2)+0.1-vspd) { if (newResults.getClosestCollision().getDistance()<=(modelHeight/2)+0.1-vspd) {
spatial.setLocalTranslation(newResults.getClosestCollision().getContactPoint()); spatial.setLocalTranslation(newResults.getClosestCollision().getContactPoint());
standingOn = newResults.getClosestCollision().getGeometry();
return true; return true;
} else { } else {
return false; return false;

@ -52,7 +52,7 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
AnimControl control; AnimControl control;
PhysicsControl physics; PhysicsControl physics;
Vector3f walkDirection; Vector3f walkDirection = Vector3f.ZERO.clone();
float lastActionPerformed = 0.0f; float lastActionPerformed = 0.0f;
static final float FREECAMERATIME = 0.5f; static final float FREECAMERATIME = 0.5f;
@ -115,7 +115,15 @@ 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)); //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 (moving) {
if (!(this instanceof NetworkPlayableCharacter)) { if (!(this instanceof NetworkPlayableCharacter)) {
cameraTransition+=tpf*4; cameraTransition+=tpf*4;
@ -129,22 +137,10 @@ public class PlayableCharacter extends AbstractControl implements Savable, Clone
moving=false; moving=false;
if (this instanceof NetworkPlayableCharacter) { walkDirection = getWalkDirection(tpf);
walkDirection = getWalkDirection((Vector3f)spatial.getUserData("lastCamDir"),(Vector3f)spatial.getUserData("lastCamLeftDir"));
} else {
walkDirection = getWalkDirection(main.getCamera().getDirection(),main.getCamera().getLeft());
}
/*if (this instanceof NetworkPlayableCharacter) { /*if (this instanceof NetworkPlayableCharacter) {
System.out.println(" 2:"+getWalkDirection()+"Moving:"+moving+"/"+strafingLeft+"/"+strafingRight+"/"+walkingBackward+"/"+walkingForward); 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 { } else {
if (!(this instanceof NetworkPlayableCharacter)) { if (!(this instanceof NetworkPlayableCharacter)) {
//System.out.println(spatial.getControl(CustomChaseCamera.class).getHorizontalRotation()+","+(spatial.getControl(CustomChaseCamera.class).getHorizontalRotation()%(2*Math.PI))); //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 void onAnimChange(AnimControl control, AnimChannel channel, String animName) {
} }
public Vector3f getWalkDirection(Vector3f camDir, Vector3f camLeftDir) { public Vector3f getWalkDirection(float tpf) {
camDir.y=0; camDir.normalizeLocal();
camLeftDir.y=0; camLeftDir.normalizeLocal();
Vector3f walkDirection = new Vector3f(0,0,0);
if (strafingLeft) { if (strafingLeft) {
walkDirection.addLocal(0,0,-1); this.walkDirection.setZ(-1);
//physics.setHorizontalSpeed(-speed);
physics.accelerate(false, tpf);
moving=true; moving=true;
} }
if (strafingRight) { if (strafingRight) {
walkDirection.addLocal(0,0,1); this.walkDirection.setZ(1);
//physics.setHorizontalSpeed(speed);
physics.accelerate(true, tpf);
moving=true; moving=true;
} }

Loading…
Cancel
Save