Hi, I’m having a problem with a Colision Detection method. I need dot the method detect when the player is grounded (on the ground, on a platform or on something like it). I have one, but it don’t run correctly. The player is on the ground but the method don’t detect it.
This is the method:
package com.unclain.udevelop.prueba1.contacts;
import java.util.List;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Contact;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.WorldManifold;
public class ContactCheck {
private List<Contact> listaContacto;
private WorldManifold worldManifold;
private float deltaTime;
private World world;
private Body bodyPlayer;
private Fixture fixturePlayer;
public ContactCheck(World world, Body bodyPlayer, Fixture fixturePlayer){
this.world = world;
this.bodyPlayer = bodyPlayer;
this.fixturePlayer = fixturePlayer;
System.out.println("¿isGrounded? " + isGrounded());
}
public boolean isGrounded(){
bodyPlayer = null;
listaContacto = world.getContactList();
for(int i = 0; i < listaContacto.size(); i++){
Contact contacto = listaContacto.get(i);
if((contacto.isTouching()) && contacto.getFixtureA() == fixturePlayer){
worldManifold = contacto.getWorldManifold();
Vector2 pos = bodyPlayer.getPosition();
boolean below = true;
for(int j = 0; j < worldManifold.getNumberOfContactPoints(); j++){
below &= worldManifold.getPoints()[j].y < pos.y;
}
if(below){
if(contacto.getFixtureA().getUserData() != null && contacto.getFixtureA().getUserData().equals("p")) {
bodyPlayer = (Body) contacto.getFixtureA().getBody().getUserData();
}
if(contacto.getFixtureB().getUserData() != null && contacto.getFixtureB().getUserData().equals("p")) {
bodyPlayer = (Body)contacto.getFixtureB().getBody().getUserData();
}
return true;
}
return false;
}
}
return false;
}
public void update(float deltaTime){
this.deltaTime = deltaTime;
}
public Vector2[] getContact(){
listaContacto = world.getContactList();
for(int i = 0; i < listaContacto.size(); i++){
Contact contacto = listaContacto.get(i);
if(contacto.isTouching() && contacto.getFixtureA() == fixturePlayer){
worldManifold = contacto.getWorldManifold();
Vector2 pos = bodyPlayer.getPosition();
boolean below = true;
for(int j = 0; j < worldManifold.getNumberOfContactPoints(); j++){
below &= worldManifold.getPoints()[j].y < pos.y - 1.5f;
}
}
}
return worldManifold.getPoints();
}
I need it for do a jump with my player.
¿Why it don’t work?
Thanks.