So I am having issues with collision. For some reason when my entity moves to another fixture it is not able to jump. The only exceptions I have noticed so far is when the object lands on a fixture, if it jumps to another fixture, or if its in between two fixtures. These are the only time where the code will work and I am able to jump.
package net.brokegames.www;
import com.badlogic.ashley.core.Entity;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.ContactImpulse;
import com.badlogic.gdx.physics.box2d.ContactListener;
import com.badlogic.gdx.physics.box2d.Filter;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.Manifold;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.uwsoft.editor.renderer.components.TransformComponent;
import com.uwsoft.editor.renderer.components.physics.PhysicsBodyComponent;
import com.uwsoft.editor.renderer.scripts.IScript;
import com.uwsoft.editor.renderer.utils.ComponentRetriever;
import net.brokegames.www.physics.CollisionFilter;
import net.brokegames.www.physics.Sensors;
/**
* @author Steven
*
*/
public class Player implements IScript {
private TransformComponent position;
private Vector2 speed;
private PhysicsBodyComponent physics;
private final float MAX_VELOCITY = 2f;
private Fixture footSensor;
private boolean isGrounded = false;
@Override
public void init(Entity entity) {
position = ComponentRetriever.get(entity, TransformComponent.class);
physics = ComponentRetriever.get(entity, PhysicsBodyComponent.class);
speed = new Vector2(.5f, 1f);
}
@Override
public void act(float delta) {
if (physics.body != null && footSensor == null) {
physics.body.getFixtureList().get(0).setUserData(Sensors.BODY);
Filter filter = new Filter();
filter.categoryBits = CollisionFilter.CATEGORY_PLAYER; //It will allow us to filter out other players from the collision detection system.
physics.body.getFixtureList().get(0).setFilterData(filter);
physics.body.setFixedRotation(true); //The player will not spin around
createBodyFixtures();
createCollision();
}
Vector2 vel = physics.body.getLinearVelocity();
if (Math.abs(vel.x) > MAX_VELOCITY) { // We need to set the max Linear Velocity of the moving object
vel.x = Math.signum(vel.x) * MAX_VELOCITY;
physics.body.setLinearVelocity(vel);
}
if (Gdx.input.isKeyPressed(Keys.D)) {
physics.body.applyLinearImpulse(new Vector2(speed.x, 0), physics.body.getWorldCenter(), true);
position.scaleX = 1;
}
if (Gdx.input.isKeyPressed(Keys.A)) {
physics.body.applyLinearImpulse(new Vector2(-speed.x, 0), physics.body.getWorldCenter(), true);
position.scaleX = -1;
}
if (Gdx.input.isKeyPressed(Keys.W) && isGrounded) {
physics.body.applyLinearImpulse(new Vector2(0, speed.y), physics.body.getWorldCenter(), true);
}
}
private void createBodyFixtures() {
PolygonShape polygonShape = new PolygonShape();
polygonShape.setAsBox(0.3f, 0.3f, new Vector2(1f,0), 0f);
FixtureDef def = new FixtureDef();
def.shape = polygonShape;
def.isSensor = true;
footSensor = physics.body.createFixture(def);
footSensor.setUserData(Sensors.FOOT);
polygonShape.setAsBox(0.3f, 0.3f, new Vector2(2f, 1), 0f);
def.shape = polygonShape;
def.isSensor = true;
footSensor = physics.body.createFixture(def);
footSensor.setUserData(Sensors.RIGHT);
polygonShape.setAsBox(0.3f, 0.3f, new Vector2(0f, 1), 0f);
def.shape = polygonShape;
def.isSensor = true;
footSensor = physics.body.createFixture(def);
footSensor.setUserData(Sensors.LEFT);
polygonShape.dispose();
}
public void createCollision() {
ProjectZen.getScene().world.setContactListener(new ContactListener() {//Cant wait until Android can run Java 8 huehuehue!!
@Override
public void beginContact(Contact contact) {
System.out.println(contact.getFixtureA().getUserData() + " " + contact.getFixtureB().getUserData());
if (contact.getFixtureB().isSensor()) {
if (contact.getFixtureB().getUserData() != null) {
if (contact.getFixtureB().getUserData().equals(Sensors.FOOT)) {
isGrounded = true;
}
}
if (contact.getFixtureA().getUserData() != null) {
if (contact.getFixtureA().getUserData().equals(Sensors.FOOT)) {
isGrounded = true;
}
}
}
}
@Override
public void endContact(Contact contact) {
if (contact.getFixtureB().getUserData() != null) {
if (contact.getFixtureB().getUserData().equals(Sensors.FOOT)) {
isGrounded = false;
}
}
if (contact.getFixtureA().getUserData() != null) {
if (contact.getFixtureA().getUserData().equals(Sensors.FOOT)) {
isGrounded = false;
}
}
}
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
// TODO Auto-generated method stub
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
// TODO Auto-generated method stub
}
});
}
@Override
public void dispose() {
}
public Vector2 getPosition() {
return new Vector2(position.x, position.y);
}
}
I have also added an image so you guys can have a visual representation of what is going on.