Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The way to solve this problem is to create a class that implements ContactListener and then when you create the World, you .setContactListener() to your class. Now your class will be notified of all collisions. From there you create a flag for if the player is in contact with any ground objects. </p> <p>Assume that you've set the shapes' userData to the relevant GameObjects (<code>shapeDef.userData = this;</code> in GameObject constructor). Assume you have an enum that defines the types of GameObject in your game, then you have a contact listener along these lines:</p> <pre> import org.jbox2d.dynamics.ContactListener; import org.jbox2d.dynamics.contacts.ContactPoint; import org.jbox2d.dynamics.contacts.ContactResult; import android.util.Log; import GameObject.GAME_OBJECT_TYPE; public class CollisionChecker implements ContactListener { private boolean groundCollision = false; @Override public void add(ContactPoint arg0) { // Called when a contact point is added. This includes the geometry and the forces. } @Override public void persist(ContactPoint arg0) { // Called when a contact point persists. This includes the geometry and the forces. } @Override public void remove(ContactPoint arg0) { // Called when a contact point is removed. This includes the last computed geometry and forces. } @Override public void result(ContactResult arg0) { // This is called once the collision has been resolved GAME_OBJECT_TYPE a = ((GameObject) arg0.shape1.getUserData()).getType(); GAME_OBJECT_TYPE b = ((GameObject) arg0.shape2.getUserData()).getType(); //check for ground groundCollision = check(a, b, GAME_OBJECT_TYPE.ground); } private boolean check(GAME_OBJECT_TYPE a, GAME_OBJECT_TYPE b, GAME_OBJECT_TYPE test){ if (a == test && b == GAME_OBJECT_TYPE.player){ return true; } else if (a == GAME_OBJECT_TYPE.player && b == test){ return true; } return false; } public boolean isGroundCollision(){ return groundCollision; } public void step() { /* if (groundCollision) Log.d("COLLSN", "We have a collision with ground");*/ //after logic, reset the state variables reset(); } private void reset(){ groundCollision = false; } } </pre> <p>Now you create a jump boolean in your player. When he jumps, set it to true. He can no longer jump when the flag is true. Test every frame for a collision with ground with 'collisionChecker.isGroundCollision()'. When a collision occurs, set player.jump = false again.</p> <p>There is a problem here in that if you have vertical ground, the player will be able to walljump. If the player hits his head against ground he will also be able to jump more. To solve these issues you check the 'arg0.position' to find out where the point of contact was in relation to the player object. If the contact is not underneath the player, then he's hitting his head or hitting a wall.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload