# Box2D Forums

 It is currently Sat May 25, 2013 6:09 pm

 All times are UTC - 8 hours [ DST ]

 Page 1 of 1 [ 4 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Character falling through the ground in some placesPosted: Fri Jan 13, 2012 2:24 pm

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);
JFrame testbed = new TestbedFrame(model, panel);
testbed.setVisible(true);
testbed.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

@Override
return true;
}

}

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

Top

 Posted: Sun Jan 15, 2012 6:42 pm

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

 Posted: Thu Jan 19, 2012 6:44 am

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

Top

 Posted: Sun Feb 05, 2012 9:55 pm

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

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 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 forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forumYou cannot post attachments in this forum

 Search for: