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

@ -20,122 +20,120 @@ import com.saltosion.gladiator.util.SpriteSequence;
public class GladiatorBrawler extends ApplicationAdapter { public class GladiatorBrawler extends ApplicationAdapter {
private Engine engine; private Engine engine;
private InputHandler inputHandler; private InputHandler inputHandler;
private Entity player; private Entity player;
@Override @Override
public void create() { public void create() {
// Initialize the Engine
engine = new Engine();
// Initialize the Engine engine.addSystem(new PhysicsSystem());
engine = new Engine(); engine.addEntityListener(new EntityListener() {
@Override
public void entityAdded(Entity entity) {
PhysicsSystem ps = engine.getSystem(PhysicsSystem.class);
ps.updateEntities(engine);
}
engine.addSystem(new PhysicsSystem()); @Override
engine.addEntityListener(new EntityListener() { public void entityRemoved(Entity entity) {
@Override PhysicsSystem ps = engine.getSystem(PhysicsSystem.class);
public void entityAdded(Entity entity) { ps.updateEntities(engine);
PhysicsSystem ps = engine.getSystem(PhysicsSystem.class); }
ps.updateEntities(engine); });
}
@Override engine.addSystem(new RenderingSystem());
public void entityRemoved(Entity entity) { engine.addEntityListener(new EntityListener() {
PhysicsSystem ps = engine.getSystem(PhysicsSystem.class); @Override
ps.updateEntities(engine); public void entityRemoved(Entity entity) {
} RenderingSystem rs = engine.getSystem(RenderingSystem.class);
}); rs.updateEntities(engine);
}
engine.addSystem(new RenderingSystem()); @Override
engine.addEntityListener(new EntityListener() { public void entityAdded(Entity entity) {
@Override RenderingSystem rs = engine.getSystem(RenderingSystem.class);
public void entityRemoved(Entity entity) { rs.updateEntities(engine);
RenderingSystem rs = engine.getSystem(RenderingSystem.class); }
rs.updateEntities(engine); });
}
@Override // Initialize stuff in the world
public void entityAdded(Entity entity) { initializePlayer();
RenderingSystem rs = engine.getSystem(RenderingSystem.class); initializeLevel();
rs.updateEntities(engine);
}
});
// Initialize stuff in the world // Initialize input
initializePlayer(); inputHandler = new InputHandler();
initializeLevel(); Gdx.input.setInputProcessor(inputHandler);
}
// Initialize input @Override
inputHandler = new InputHandler(); public void render() {
Gdx.input.setInputProcessor(inputHandler); engine.update(Gdx.graphics.getDeltaTime());
} }
@Override public void initializePlayer() {
public void render() { player = new Entity();
engine.update(Gdx.graphics.getDeltaTime());
}
public void initializePlayer() { CRenderedObject renderedObject = new CRenderedObject();
Sprite player1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 64, 64);
Sprite player2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 64, 64);
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
renderedObject.addSequence("Idle", sequence);
renderedObject.playAnimation("Idle");
player.add(renderedObject);
player.add(new CPhysics().setSize(player1.getRegionWidth() * Global.SPRITE_SCALE,
player1.getRegionHeight() * Global.SPRITE_SCALE).setPosition(0, 5));
player = new Entity(); engine.addEntity(player);
CRenderedObject renderedObject = new CRenderedObject(); AppUtil.player = player;
Sprite player1 = SpriteLoader.loadSprite(Name.PLAYERIMG, 0, 0, 64, 64); }
Sprite player2 = SpriteLoader.loadSprite(Name.PLAYERIMG, 1, 0, 64, 64);
SpriteSequence sequence = new SpriteSequence(1).addSprite(player1).addSprite(player2);
renderedObject.addSequence("Idle", sequence);
renderedObject.playAnimation("Idle");
player.add(renderedObject);
player.add(new CPhysics().setSize(player1.getRegionWidth() * Global.SPRITE_SCALE,
player1.getRegionHeight() * Global.SPRITE_SCALE).setPosition(0, 5));
engine.addEntity(player); public void initializeLevel() {
Entity ground = new Entity();
AppUtil.player = player; Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG, 0, 0, 256, 64);
} CRenderedObject renderedObject = new CRenderedObject(groundSprite);
ground.add(renderedObject);
CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
.setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE,
groundSprite.getRegionHeight() * Global.SPRITE_SCALE);
physics.position.set(new Vector2(0, -4));
ground.add(physics);
public void initializeLevel() { Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 64, 64);
Entity ground = new Entity();
Sprite groundSprite = SpriteLoader.loadSprite(Name.GROUNDIMG, 0, 0, 256, 64); Entity wall0 = new Entity();
CRenderedObject renderedObject = new CRenderedObject(groundSprite); CRenderedObject wall0RenderedObject = new CRenderedObject(wallSprite);
ground.add(renderedObject); CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
CPhysics physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) .setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
.setSize(groundSprite.getRegionWidth() * Global.SPRITE_SCALE, wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
groundSprite.getRegionHeight() * Global.SPRITE_SCALE); wall0Physics.position.set(new Vector2(6, 0));
physics.position.set(new Vector2(0, -4)); wall0.add(wall0RenderedObject);
ground.add(physics); wall0.add(wall0Physics);
Sprite wallSprite = SpriteLoader.loadSprite(Name.WALLIMG, 0, 0, 64, 64); Entity wall1 = new Entity();
CRenderedObject wall1RenderedObject = new CRenderedObject(wallSprite);
CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false)
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE,
wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
wall1Physics.position.set(new Vector2(-6, 0));
wall1.add(wall1RenderedObject);
wall1.add(wall1Physics);
Entity wall0 = new Entity(); engine.addEntity(ground);
CRenderedObject wall0RenderedObject = new CRenderedObject(wallSprite); engine.addEntity(wall0);
CPhysics wall0Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) engine.addEntity(wall1);
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, }
wallSprite.getRegionHeight() * Global.SPRITE_SCALE);
wall0Physics.position.set(new Vector2(6, 0));
wall0.add(wall0RenderedObject);
wall0.add(wall0Physics);
Entity wall1 = new Entity(); @Override
CRenderedObject wall1RenderedObject = new CRenderedObject(wallSprite); public void resize(int width, int height) {
CPhysics wall1Physics = new CPhysics().setMovable(false).setGravityApplied(false).setDynamic(false) super.resize(width, height);
.setSize(wallSprite.getRegionWidth() * Global.SPRITE_SCALE, RenderingSystem rs = engine.getSystem(RenderingSystem.class);
wallSprite.getRegionHeight() * Global.SPRITE_SCALE); float aspectratio = ((float) width) / ((float) height);
wall1Physics.position.set(new Vector2(-6, 0)); rs.setViewport((int) (AppUtil.VPHEIGHT_CONST * aspectratio), AppUtil.VPHEIGHT_CONST);
wall1.add(wall1RenderedObject); }
wall1.add(wall1Physics);
engine.addEntity(ground);
engine.addEntity(wall0);
engine.addEntity(wall1);
}
@Override
public void resize(int width, int height) {
super.resize(width, height);
RenderingSystem rs = engine.getSystem(RenderingSystem.class);
float aspectratio = ((float) width) / ((float) height);
rs.setViewport((int) (AppUtil.VPHEIGHT_CONST * aspectratio), AppUtil.VPHEIGHT_CONST);
}
} }

View File

@ -6,53 +6,53 @@ import com.saltosion.gladiator.physics.CollisionListener;
public class CPhysics extends Component { 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;
// Movable toggles if the entity can move by itself // Movable toggles if the entity can move by itself
public boolean movable = true; public boolean movable = true;
// GravityApplied toggles if the entity is affected by gravity // GravityApplied toggles if the entity is affected by gravity
public boolean gravityApplied = true; public boolean gravityApplied = true;
// Dynamic toggles if the entity processes collisions // Dynamic toggles if the entity processes collisions
public boolean dynamic = true; public boolean dynamic = true;
// Movement (/input) vars // Movement (/input) vars
public boolean movingLeft = false; public boolean movingLeft = false;
public boolean movingRight = false; public boolean movingRight = false;
public boolean jumping = false; public boolean jumping = false;
public CPhysics setMovable(boolean movable) { public CPhysics setMovable(boolean movable) {
this.movable = movable; this.movable = movable;
return this; return this;
} }
public CPhysics setGravityApplied(boolean gravityApplied) { public CPhysics setGravityApplied(boolean gravityApplied) {
this.gravityApplied = gravityApplied; this.gravityApplied = gravityApplied;
return this; return this;
} }
public CPhysics setDynamic(boolean dynamic) { public CPhysics setDynamic(boolean dynamic) {
this.dynamic = dynamic; this.dynamic = dynamic;
return this; return this;
} }
public CPhysics setSize(float w, float h) { public CPhysics setSize(float w, float h) {
this.size.set(w, h); this.size.set(w, h);
return this; return this;
} }
public CPhysics setPosition(float x, float y) { public CPhysics setPosition(float x, float y) {
this.position.set(x, y); this.position.set(x, y);
return this; return this;
} }
public CPhysics setCollisionListener(CollisionListener collisionListener) { public CPhysics setCollisionListener(CollisionListener collisionListener) {
this.collisionListener = collisionListener; this.collisionListener = collisionListener;
return this; return this;
} }
} }

View File

@ -15,123 +15,128 @@ import com.saltosion.gladiator.physics.CollisionSide;
*/ */
public class PhysicsSystem extends EntitySystem { public class PhysicsSystem extends EntitySystem {
private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class); private static final float MAX_VEL = 0.5f;
private ImmutableArray<Entity> entities; private ComponentMapper<CPhysics> pm = ComponentMapper.getFor(CPhysics.class);
private ImmutableArray<Entity> entities;
@Override @Override
public void addedToEngine(Engine engine) { public void addedToEngine(Engine engine) {
updateEntities(engine); updateEntities(engine);
} }
@Override @Override
public void update(float deltaTime) { public void update(float deltaTime) {
for (int i = 0; i < entities.size(); i++) { deltaTime = Math.min(deltaTime, 0.033f);
CPhysics obj = pm.get(entities.get(i)); for (int i = 0; i < entities.size(); i++) {
CPhysics obj = pm.get(entities.get(i));
// Movement // Movement
if (obj.movable) { if (obj.movable) {
float move = 0; float move = 0;
if (obj.movingLeft) { if (obj.movingLeft) {
move--; move--;
} }
if (obj.movingRight) { if (obj.movingRight) {
move++; move++;
} }
obj.velocity.x = move * obj.movespeed * deltaTime; obj.velocity.x = move * obj.movespeed * deltaTime;
if (obj.jumping && obj.grounded) { if (obj.jumping && obj.grounded) {
obj.grounded = false; obj.grounded = false;
obj.velocity.y = obj.jumpForce; obj.velocity.y = obj.jumpForce;
} }
} }
// Gravity // Gravity
if (obj.gravityApplied) { if (obj.gravityApplied) {
obj.velocity.y -= obj.gravity * deltaTime; obj.velocity.y -= obj.gravity * deltaTime;
} }
// Collisions obj.velocity.y = Math.max(Math.min(obj.velocity.y, MAX_VEL), -MAX_VEL);
if (obj.dynamic) {
for (int j = 0; j < entities.size(); j++) {
if (i == j) {
continue;
}
collision(entities.get(i), entities.get(j));
}
}
// Apply movement // Collisions
obj.position.add(obj.velocity); if (obj.dynamic) {
} for (int j = 0; j < entities.size(); j++) {
} if (i == j) {
continue;
}
collision(entities.get(i), entities.get(j));
}
}
public void collision(Entity entity0, Entity entity1) { // Apply movement
CPhysics cp0 = pm.get(entity0); obj.position.add(obj.velocity);
CPhysics cp1 = pm.get(entity1); }
}
float x00 = cp0.position.x - cp0.size.x / 2; public void collision(Entity entity0, Entity entity1) {
float x01 = cp0.position.x + cp0.size.x / 2; CPhysics cp0 = pm.get(entity0);
float x10 = cp1.position.x - cp1.size.x / 2; CPhysics cp1 = pm.get(entity1);
float x11 = cp1.position.x + cp1.size.x / 2;
float y00 = cp0.position.y - cp0.size.y / 2;
float y01 = cp0.position.y + cp0.size.y / 2;
float y10 = cp1.position.y - cp1.size.y / 2;
float y11 = cp1.position.y + cp1.size.y / 2;
boolean colliding = (x00 < x11) && (x01 > x10) && (y00 < y11) && (y01 > y10); float x00 = cp0.position.x - cp0.size.x / 2;
float x01 = cp0.position.x + cp0.size.x / 2;
float x10 = cp1.position.x - cp1.size.x / 2;
float x11 = cp1.position.x + cp1.size.x / 2;
float y00 = cp0.position.y - cp0.size.y / 2;
float y01 = cp0.position.y + cp0.size.y / 2;
float y10 = cp1.position.y - cp1.size.y / 2;
float y11 = cp1.position.y + cp1.size.y / 2;
if (!colliding) { boolean colliding = (x00 < x11) && (x01 > x10) && (y00 < y11) && (y01 > y10);
return;
}
if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 16) { if (!colliding) {
// cp0's left side is colliding with cp1's right side return;
if (cp0.velocity.x < 0) { }
// cp0 is going left, stop
cp0.velocity.x = 0;
}
if (cp0.collisionListener != null) { if (x00 <= x11 && Math.abs(x00 - x11) < (cp0.size.x + cp1.size.x) / 16) {
cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1); // cp0's left side is colliding with cp1's right side
} if (cp0.velocity.x < 0) {
} // cp0 is going left, stop
if (x01 > x10 && Math.abs(x01 - x10) < (cp0.size.x + cp1.size.x) / 16) { cp0.velocity.x = 0;
// cp0's right side is colliding with cp1's left side }
if (cp0.velocity.x > 0) {
// cp0 is going right, stop
cp0.velocity.x = 0;
}
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1); cp0.collisionListener.collision(CollisionSide.LEFT, entity0, entity1);
} }
} }
if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.size.y + cp1.size.y) / 16) { if (x01 > x10 && Math.abs(x01 - x10) < (cp0.size.x + cp1.size.x) / 16) {
// cp0's bottom side is colliding with cp1's top side // cp0's right side is colliding with cp1's left side
if (cp0.velocity.y < 0) { if (cp0.velocity.x > 0) {
// cp0 is going down, stop // cp0 is going right, stop
cp0.velocity.y = 0; cp0.velocity.x = 0;
} }
cp0.grounded = true;
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1); cp0.collisionListener.collision(CollisionSide.RIGHT, entity0, entity1);
} }
} }
if (y01 > y10 && Math.abs(y01 - y10) < (cp0.size.y + cp1.size.y) / 16) { if (y00 <= y11 && Math.abs(y00 - y11) < (cp0.size.y + cp1.size.y) / 16) {
// cp0's top side is colliding with cp1's bottom side // cp0's bottom side is colliding with cp1's top side
if (cp0.velocity.y > 0) { if (cp0.velocity.y < 0) {
// cp0 is going up, stop // cp0 is going down, stop
cp0.velocity.y = 0; cp0.velocity.y = 0;
} }
cp0.grounded = true;
cp0.position.y += (y11 - y00) * 1.1f;
if (cp0.collisionListener != null) { if (cp0.collisionListener != null) {
cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1); cp0.collisionListener.collision(CollisionSide.BOTTOM, entity0, entity1);
} }
} }
} if (y01 > y10 && Math.abs(y01 - y10) < (cp0.size.y + cp1.size.y) / 16) {
// cp0's top side is colliding with cp1's bottom side
if (cp0.velocity.y > 0) {
// cp0 is going up, stop
cp0.velocity.y = 0;
}
public void updateEntities(Engine engine) { if (cp0.collisionListener != null) {
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class)); cp0.collisionListener.collision(CollisionSide.TOP, entity0, entity1);
} }
}
}
public void updateEntities(Engine engine) {
entities = engine.getEntitiesFor(Family.getFor(CPhysics.class));
}
} }