Made physics fps-independent and removed delta limit.
This commit is contained in:
parent
ed9d530705
commit
5acb1b301c
@ -9,7 +9,7 @@ public class CPhysics extends Component {
|
|||||||
private final Vector2 position = new Vector2();
|
private final Vector2 position = new Vector2();
|
||||||
private final Vector2 velocity = new Vector2();
|
private final Vector2 velocity = new Vector2();
|
||||||
private final Vector2 size = new Vector2();
|
private final Vector2 size = new Vector2();
|
||||||
private float movespeed = 5f, jumpForce = 0.5f, gravity = 1f;
|
private float movespeed = 7.5f, jumpForce = 35f, gravity = 100f;
|
||||||
private CollisionListener collisionListener = null;
|
private CollisionListener collisionListener = null;
|
||||||
|
|
||||||
private boolean movable = true;
|
private boolean movable = true;
|
||||||
|
@ -12,7 +12,7 @@ import com.saltosion.gladiator.util.Direction;
|
|||||||
|
|
||||||
public class PhysicsSystem extends EntitySystem {
|
public class PhysicsSystem extends EntitySystem {
|
||||||
|
|
||||||
private static final float MAX_VEL = 0.5f;
|
private static final float MAX_VEL = 2f, COLLISION_PRECISION = 24f, UPDATES_PER_SECOND = 120f;
|
||||||
|
|
||||||
private static final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
private static final ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
|
||||||
private static final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
|
private static final ComponentMapper<CCombat> cm = ComponentMapper.getFor(CCombat.class);
|
||||||
@ -25,49 +25,52 @@ public class PhysicsSystem extends EntitySystem {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(float deltaTime) {
|
public void update(float deltaTime) {
|
||||||
deltaTime = Math.min(deltaTime, 0.033f);
|
float freq = 1f / UPDATES_PER_SECOND;
|
||||||
for (int i = 0; i < entities.size(); i++) {
|
int times = (int) Math.ceil(deltaTime / freq);
|
||||||
CPhysics obj = pm.get(entities.get(i));
|
deltaTime /= (float) times;
|
||||||
CCombat combat = cm.get(entities.get(i));
|
for (int t = 0; t < times; t++) {
|
||||||
|
for (int i = 0; i < entities.size(); i++) {
|
||||||
|
CPhysics obj = pm.get(entities.get(i));
|
||||||
|
CCombat combat = cm.get(entities.get(i));
|
||||||
|
|
||||||
// Apply movement
|
// Apply movement
|
||||||
obj.getPosition().add(obj.getVelocity());
|
obj.getPosition().add(Math.max(Math.min(obj.getVelocity().x * deltaTime, MAX_VEL), -MAX_VEL),
|
||||||
|
Math.max(Math.min(obj.getVelocity().y * deltaTime, MAX_VEL), -MAX_VEL));
|
||||||
|
|
||||||
// Movement
|
// Movement
|
||||||
if (obj.isMovable()) {
|
if (obj.isMovable()) {
|
||||||
float move = 0;
|
float move = 0;
|
||||||
if (obj.movingLeft) {
|
if (obj.movingLeft) {
|
||||||
move--;
|
move--;
|
||||||
}
|
}
|
||||||
if (obj.movingRight) {
|
if (obj.movingRight) {
|
||||||
move++;
|
move++;
|
||||||
}
|
}
|
||||||
obj.getVelocity().x = move * obj.getMovespeed() * deltaTime;
|
obj.getVelocity().x = move * obj.getMovespeed();
|
||||||
if (combat != null) {
|
if (combat != null) {
|
||||||
if (combat.swingCdCounter > 0) {
|
if (combat.swingCdCounter > 0) {
|
||||||
obj.getVelocity().x /= 2;
|
obj.getVelocity().x /= 2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (obj.jumping && obj.isGrounded()) {
|
||||||
|
obj.setGrounded(false);
|
||||||
|
obj.getVelocity().y = obj.getJumpForce();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (obj.jumping && obj.isGrounded()) {
|
|
||||||
obj.setGrounded(false);
|
// Gravity
|
||||||
obj.getVelocity().y = obj.getJumpForce();
|
if (obj.isGravityApplied()) {
|
||||||
|
obj.getVelocity().y -= obj.getGravity() * deltaTime;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Gravity
|
// Collisions
|
||||||
if (obj.isGravityApplied()) {
|
if (obj.isProcessCollisions()) {
|
||||||
obj.getVelocity().y -= obj.getGravity() * deltaTime;
|
for (int j = 0; j < entities.size(); j++) {
|
||||||
}
|
if (i == j) {
|
||||||
|
continue;
|
||||||
obj.getVelocity().y = Math.max(Math.min(obj.getVelocity().y, MAX_VEL), -MAX_VEL);
|
}
|
||||||
|
collision(entities.get(i), entities.get(j));
|
||||||
// Collisions
|
|
||||||
if (obj.isProcessCollisions()) {
|
|
||||||
for (int j = 0; j < entities.size(); j++) {
|
|
||||||
if (i == j) {
|
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
collision(entities.get(i), entities.get(j));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,13 +97,14 @@ public class PhysicsSystem extends EntitySystem {
|
|||||||
|
|
||||||
boolean collidedAlready = false;
|
boolean collidedAlready = false;
|
||||||
|
|
||||||
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / 16) {
|
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.getSize().x + cp1.getSize().x) / COLLISION_PRECISION) {
|
||||||
// cp0's left side is colliding with cp1's right side
|
// cp0's left side is colliding with cp1's right side
|
||||||
if (!cp0.isGhost() && !cp1.isGhost()) {
|
if (!cp0.isGhost() && !cp1.isGhost()) {
|
||||||
if (cp0.getVelocity().x < 0) {
|
if (cp0.getVelocity().x < 0) {
|
||||||
// cp0 is going left, stop
|
// cp0 is going left, stop
|
||||||
cp0.getVelocity().x = 0;
|
cp0.getVelocity().x = 0;
|
||||||
}
|
}
|
||||||
|
cp0.getPosition().x += x11 - x00;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cp0.getCollisionListener() != null) {
|
if (cp0.getCollisionListener() != null) {
|
||||||
@ -108,13 +112,14 @@ public class PhysicsSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
collidedAlready = true;
|
collidedAlready = true;
|
||||||
}
|
}
|
||||||
if (x01 > x10 && Math.abs(x01 - x10) < (cp0.getSize().x + cp1.getSize().x) / 16) {
|
if (x01 > x10 && Math.abs(x01 - x10) < (cp0.getSize().x + cp1.getSize().x) / COLLISION_PRECISION) {
|
||||||
// cp0's right side is colliding with cp1's left side
|
// cp0's right side is colliding with cp1's left side
|
||||||
if (!cp0.isGhost() && !cp1.isGhost()) {
|
if (!cp0.isGhost() && !cp1.isGhost()) {
|
||||||
if (cp0.getVelocity().x > 0) {
|
if (cp0.getVelocity().x > 0) {
|
||||||
// cp0 is going right, stop
|
// cp0 is going right, stop
|
||||||
cp0.getVelocity().x = 0;
|
cp0.getVelocity().x = 0;
|
||||||
}
|
}
|
||||||
|
cp0.getPosition().x += x10 - x01;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cp0.getCollisionListener() != null) {
|
if (cp0.getCollisionListener() != null) {
|
||||||
@ -122,7 +127,7 @@ public class PhysicsSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
collidedAlready = true;
|
collidedAlready = true;
|
||||||
}
|
}
|
||||||
if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.getSize().y + cp1.getSize().y) / 16) {
|
if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.getSize().y + cp1.getSize().y) / COLLISION_PRECISION) {
|
||||||
// cp0's bottom side is colliding with cp1's top side
|
// cp0's bottom side is colliding with cp1's top side
|
||||||
if (!cp0.isGhost() && !cp1.isGhost()) {
|
if (!cp0.isGhost() && !cp1.isGhost()) {
|
||||||
if (cp0.getVelocity().y < 0) {
|
if (cp0.getVelocity().y < 0) {
|
||||||
@ -138,7 +143,7 @@ public class PhysicsSystem extends EntitySystem {
|
|||||||
}
|
}
|
||||||
collidedAlready = true;
|
collidedAlready = true;
|
||||||
}
|
}
|
||||||
if (y01 > y10 && Math.abs(y01 - y10) < (cp0.getSize().y + cp1.getSize().y) / 16) {
|
if (y01 > y10 && Math.abs(y01 - y10) < (cp0.getSize().y + cp1.getSize().y) / COLLISION_PRECISION) {
|
||||||
// cp0's top side is colliding with cp1's bottom side
|
// cp0's top side is colliding with cp1's bottom side
|
||||||
if (!cp0.isGhost() && !cp1.isGhost()) {
|
if (!cp0.isGhost() && !cp1.isGhost()) {
|
||||||
if (cp0.getVelocity().y > 0) {
|
if (cp0.getVelocity().y > 0) {
|
||||||
|
@ -6,11 +6,14 @@ import com.saltosion.gladiator.GladiatorBrawler;
|
|||||||
import com.saltosion.gladiator.util.Name;
|
import com.saltosion.gladiator.util.Name;
|
||||||
|
|
||||||
public class DesktopLauncher {
|
public class DesktopLauncher {
|
||||||
public static void main (String[] arg) {
|
|
||||||
|
public static void main(String[] args) {
|
||||||
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
|
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
|
||||||
config.title = Name.GAME_NAME;
|
config.title = Name.GAME_NAME;
|
||||||
config.width = 1280;
|
config.width = 1280;
|
||||||
config.height = 720;
|
config.height = 720;
|
||||||
new LwjglApplication(new GladiatorBrawler(), config);
|
config.vSyncEnabled = true; // Change this to false for performance testing
|
||||||
|
config.foregroundFPS = 0;
|
||||||
|
LwjglApplication app = new LwjglApplication(new GladiatorBrawler(), config);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user