Fixed bug where player sinks into the ground.

This commit is contained in:
Jeasonfire 2015-05-10 01:09:54 +03:00
parent b2fe6338a5
commit 71a2ce2b40
3 changed files with 243 additions and 240 deletions

View File

@ -27,7 +27,6 @@ public class GladiatorBrawler extends ApplicationAdapter {
@Override @Override
public void create() { public void create() {
// Initialize the Engine // Initialize the Engine
engine = new Engine(); engine = new Engine();
@ -76,7 +75,6 @@ public class GladiatorBrawler extends ApplicationAdapter {
} }
public void initializePlayer() { public void initializePlayer() {
player = new Entity(); player = new Entity();
CRenderedObject renderedObject = new CRenderedObject(); CRenderedObject renderedObject = new CRenderedObject();

View File

@ -9,7 +9,7 @@ public class CPhysics extends Component {
public Vector2 position = new Vector2(); public Vector2 position = new Vector2();
public Vector2 velocity = new Vector2(); public Vector2 velocity = new Vector2();
public Vector2 size = new Vector2(); public Vector2 size = new Vector2();
public float movespeed = 5f, jumpForce = 0.3f, gravity = 1f; public float movespeed = 5f, jumpForce = 0.5f, gravity = 1f;
public boolean grounded = true; public boolean grounded = true;
public CollisionListener collisionListener = null; public CollisionListener collisionListener = null;

View File

@ -15,6 +15,7 @@ import com.saltosion.gladiator.physics.CollisionSide;
*/ */
public class PhysicsSystem extends EntitySystem { public class PhysicsSystem extends EntitySystem {
private static final float MAX_VEL = 0.5f;
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class); private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private ImmutableArray<Entity> entities; private ImmutableArray<Entity> entities;
@ -25,6 +26,7 @@ public class PhysicsSystem extends EntitySystem {
@Override @Override
public void update(float deltaTime) { public void update(float deltaTime) {
deltaTime = Math.min(deltaTime, 0.033f);
for (int i = 0; i < entities.size(); i++) { for (int i = 0; i < entities.size(); i++) {
CPhysics obj = pm.get(entities.get(i)); CPhysics obj = pm.get(entities.get(i));
@ -49,6 +51,8 @@ public class PhysicsSystem extends EntitySystem {
obj.velocity.y -= obj.gravity * deltaTime; obj.velocity.y -= obj.gravity * deltaTime;
} }
obj.velocity.y = Math.max(Math.min(obj.velocity.y, MAX_VEL), -MAX_VEL);
// Collisions // Collisions
if (obj.dynamic) { if (obj.dynamic) {
for (int j = 0; j < entities.size(); j++) { for (int j = 0; j < entities.size(); j++) {
@ -112,6 +116,7 @@ public class PhysicsSystem extends EntitySystem {
cp0.velocity.y = 0; cp0.velocity.y = 0;
} }
cp0.grounded = true; cp0.grounded = true;
cp0.position.y += (y11 - y00) * 1.1f;
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1); cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1);