isGrounded detection problem. [libGDX] [Box2D]

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.

i find it hard to follow your code but can you say why you are doing this ?

 bodyPlayer = null; 

Ps :
you should post this sort of thing in Newbie & Debugging Questions

good luck

I put bodyPlayer = null because the original code the creator put this. I put it when I saw dot the method don’t work.

And yeah, I’m really sorry. The other times dot I do a post in this forum I put it in these forums. When I do this post I was doing another thing. :stuck_out_tongue:

P.D: I know my English is bad, sorry. I’m trying put all how it must be. :persecutioncomplex:

[quote]because the original code the creator put this.
[/quote]
PLEASE DON’T !!
i highly suggest that you stop following these “advanced” tutorials unless you learned Java and libGdx basics, if you did, then you are able to create a little game by yourself without any help (pong for example), if you did, then you are able to start making a little platformer game by yourself without “any” help, you will find many problems during the task, but dude, that’s the real fun.

good luck