Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (476)
Games in Android Showcase (106)
games submitted by our members
Games in WIP (532)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  isGrounded detection problem. [libGDX] [Box2D]  (Read 614 times)
0 Members and 1 Guest are viewing this topic.
Offline SrFurias

Senior Newbie





« Posted 2013-07-16 22:09:27 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
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.
Offline alaslipknot
« Reply #1 - Posted 2013-07-16 22:33:09 »

i find it hard to follow your code but can you say why you are doing this ?
1  
 bodyPlayer = null; 


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

good luck

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Offline SrFurias

Senior Newbie





« Reply #2 - Posted 2013-07-16 22:50:24 »

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.  Tongue

P.D: I know my English is bad, sorry. I'm trying put all how it must be. persecutioncomplex
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline alaslipknot
« Reply #3 - Posted 2013-07-18 01:39:31 »

Quote
because the original code the creator put this.

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 

"It's not at all important to get it right the first time. It's vitally important to get it right the last time."
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

pw (14 views)
2014-07-24 01:59:36

Riven (14 views)
2014-07-23 21:16:32

Riven (13 views)
2014-07-23 21:07:15

Riven (15 views)
2014-07-23 20:56:16

ctomni231 (43 views)
2014-07-18 06:55:21

Zero Volt (40 views)
2014-07-17 23:47:54

danieldean (32 views)
2014-07-17 23:41:23

MustardPeter (36 views)
2014-07-16 23:30:00

Cero (50 views)
2014-07-16 00:42:17

Riven (50 views)
2014-07-14 18:02:53
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!