Box2D Forums

It is currently Mon Sep 01, 2014 12:06 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Fri Jan 13, 2012 2:24 pm 
Offline

Joined: Fri Jan 13, 2012 1:47 pm
Posts: 2
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:
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.


Top
 Profile  
 
PostPosted: Sun Jan 15, 2012 6:42 pm 
Offline

Joined: Mon Jun 08, 2009 12:21 pm
Posts: 355
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


Top
 Profile  
 
PostPosted: Thu Jan 19, 2012 6:44 am 
Offline

Joined: Fri Jan 13, 2012 1:47 pm
Posts: 2
Cool, thank you very much.


Top
 Profile  
 
PostPosted: Sun Feb 05, 2012 9:55 pm 
Offline

Joined: Fri Oct 29, 2010 1:47 am
Posts: 29
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!


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group