Character falling through the ground in some places

Discuss issues specific the Java port of Box2D
Matroc
Posts: 2
Joined: Fri Jan 13, 2012 1:47 pm

Character falling through the ground in some places

Postby Matroc » Fri Jan 13, 2012 2:24 pm

Hi,

i'm in the process of writing a small game and have stumbled upon a problem in jbox2ds collision detection.
My code is generating polygons from a tile map and uses those for collisions.

However, there are places where the character just warps through my polygons (some of them, not all).

I've created the following sample (based on the Character Collision Test and one of the generated polygons) to illustrate the problem.

Code: Select all

import javax.swing.*;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.*;
import org.jbox2d.testbed.framework.*;
import org.jbox2d.testbed.framework.j2d.TestPanelJ2D;
import org.slf4j.*;

public class CharacterCollisionProblem extends TestbedTest {

   @Override
   public void initTest(boolean argDeserialized) {
      // character warps through this polygon
      {
         BodyDef bd = new BodyDef();
         bd.position = new Vec2(1, 0);
         Body ground = getWorld().createBody(bd);

         PolygonShape shape = new PolygonShape();
         Vec2[] vertices = new Vec2[8];
         vertices[0] = new Vec2(0.0f, -5.3f);
         vertices[1] = new Vec2(0.033333335f, -5.3333335f);
         vertices[2] = new Vec2(9.633333f, -5.3333335f);
         vertices[3] = new Vec2(9.666667f, -5.3f);
         vertices[4] = new Vec2(9.666667f, -4.7000003f);
         vertices[5] = new Vec2(9.633333f, -4.666667f);
         vertices[6] = new Vec2(0.033333335f, -4.666667f);
         vertices[7] = new Vec2(0.0f, -4.7000003f);

         shape.set(vertices, 8);
         ground.createFixture(shape, 1f);
      }

      // this character warps
      {
         BodyDef bd = new BodyDef();
         bd.position.set(4.0f, 5.0f);
         bd.type = BodyType.DYNAMIC;
         bd.fixedRotation = true;
         bd.allowSleep = false;

         Body body = getWorld().createBody(bd);

         PolygonShape shape = new PolygonShape();
         shape.setAsBox(.2f, .8f);

         FixtureDef fd = new FixtureDef();
         fd.shape = shape;
         fd.density = 20.0f;
         body.createFixture(fd);
      }

      // this one doesn't (because he doesn't land in that weird "warp spot")
      {
         BodyDef bd = new BodyDef();
         bd.position.set(6.0f, 5.0f);
         bd.type = BodyType.DYNAMIC;
         bd.fixedRotation = true;
         bd.allowSleep = false;

         Body body = getWorld().createBody(bd);

         PolygonShape shape = new PolygonShape();
         shape.setAsBox(.2f, .8f);

         FixtureDef fd = new FixtureDef();
         fd.shape = shape;
         fd.density = 20.0f;
         body.createFixture(fd);
      }
   }

   @Override
   public void step(TestbedSettings settings) {
      super.step(settings);
      addTextLine("Test that illustrates a collision problem.");
   }

   @Override
   public String getTestName() {
      return "Collision Problem";
   }

   private static final Logger log = LoggerFactory
         .getLogger(CharacterCollisionProblem.class);

   public static void main(String[] args) {
      try {
         UIManager
               .setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
      } catch (Exception e) {
         log.warn("Could not set the look and feel to nimbus.  "
               + "Hopefully you're on a mac so the window isn't ugly as crap.");
      }
      TestbedModel model = new TestbedModel();
      TestbedPanel panel = new TestPanelJ2D(model);
      model.addCategory("Problem");
      model.addTest(new CharacterCollisionProblem());
      JFrame testbed = new TestbedFrame(model, panel);
      testbed.setVisible(true);
      testbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   @Override
   public boolean isSaveLoadEnabled() {
      return true;
   }

}


I'd be really thankful for a fix/workaround/nudge in the right direction.

toucansam
Posts: 356
Joined: Mon Jun 08, 2009 12:21 pm
Contact:

Re: Character falling through the ground in some places

Postby toucansam » Sun Jan 15, 2012 6:42 pm

happy to say that I figured out the issue here. Caused by an incorrect calculation in computing the centroid in the polygon shape. Fixed in rev 562 in the 2.1.2.1 branch

Matroc
Posts: 2
Joined: Fri Jan 13, 2012 1:47 pm

Re: Character falling through the ground in some places

Postby Matroc » Thu Jan 19, 2012 6:44 am

Cool, thank you very much.

r_bewick
Posts: 29
Joined: Fri Oct 29, 2010 1:47 am

Re: Character falling through the ground in some places

Postby r_bewick » Sun Feb 05, 2012 9:55 pm

Wonderful fix, toucansam.
I was having the same problem in the 2.1.2 version, but with edge shapes.
No more falling through the ground in 2.1.2.2! :)
Thanks!

toucansam
Posts: 356
Joined: Mon Jun 08, 2009 12:21 pm
Contact:

Re: Character falling through the ground in some places

Postby toucansam » Tue Jun 02, 2015 9:13 am

update to the new version :)
2.3.0


Return to “Java”



Who is online

Users browsing this forum: No registered users and 0 guests