Jbox2d Issues with positioning platforms - java

I am working on developing a platformer, and everything's working fine, except, I seem to be unable to position the bodies for the static platforms.
// creating all the bodies
BodyDef bdef = new BodyDef();
bdef.position.set(x, y);
bdef.type = BodyType.STATIC;
Body body = world.createBody(bdef);
PolygonShape shape = new PolygonShape();
shape.setAsBox(width, height);
FixtureDef fdef = new FixtureDef();
fdef.friction = 0.3f;
fdef.shape = shape;
body.createFixture(fdef);
This is general code that's used to create every body in the map. The bodies work fine, but they don't line up with the width nor the coordinates of the map that I've set. I've noticed that I must specify the bottom left point as starting point for it to make body, but what else I am missing? Why bodies tend to be bigger and go past the starting point of X and Y?

Bodies in box2d are made from their center points. When you define it's position, you are setting it's center and when you define it's width/height, you are actually setting half the width/height (i.e. the size ends up double what you intended).

Related

How to create infinite Platform with Box2d?

I'm making a side scrolling game and I wan't to know how can I create an infinite terrain, do I need to use a static body that keeps on increasing its width? Also since its an infinite world, is it a good idea to just create a body to be used as an obstacle and then remove it when not in range?
public Body createPlatform(){
Body body;
BodyDef def = new BodyDef();
def.type = BodyDef.BodyType.StaticBody;
def.fixedRotation = true;
def.position.set(0.6f, 1.6f);
body = world.createBody(def);
PolygonShape shape = new PolygonShape();
shape.setAsBox(2f, 1.5f);//have no Idea how to increase width infinitely or should I even be using a Body as ground.
FixtureDef fDef = new FixtureDef();
fDef.shape = shape;
fDef.density = 1f;
body.createFixture(fDef);
shape.dispose();
return(body);
}
Also I'm using Libgdx library and java of course.
It's best to split the world into chunks: once the player exits one chunk, load in one or more. You do not need to have the chunks pre-loaded; you load them in on the fly depending on the player's location.

Box2D Polygon Bodies not rotating

I'm trying to create a game with "libGDX" and "Box2D".
I've several shapes in the game, so I created a BodyFactory class which creates my bodies using PolygonShape
The problem is, when I create a body, with Shape.setAsBox() method, everything works fine, but when I create bodies with PolygonShape.set(vertices), the position of bodies changes as I wish, but they won't rotate at all.
This is what I get (after stability) when I drop 3 bodies from the sky:
The square rotates and stays at the ground, bot the other shapes won't.
Also note that I tried adding
body.setFixedRotation(false);
to my code, but nothing changed.
Also friction, mass and density of shapes are at a reasonable amount.
This is the part of my code which creates a "PolygonShape" from a file:
...
Body body = world.createBody(bodyDef);
...
for (int i = 0; i < bodyConf.meshData.length; i++) {
PolygonShape polygonShape = new PolygonShape();
polygonShape.set(bodyConf.meshData[i]);
fixtureDef.shape = polygonShape;
body.createFixture(fixtureDef);
polygonShape.dispose();
}
I think the problem is that you are creating only one Body with three Fixtures attached to it.
What you actually want is three Bodys, with one Fixture attached to each of them. That way, each body can rotate independent from the others.
for (int i = 0; i < bodyConf.meshData.length; i++) {
BodyDef bodyDef = ...;
Body body = world.createBody(bodyDef);
PolygonShape polygonShape = new PolygonShape();
polygonShape.set(bodyConf.meshData[i]);
fixtureDef.shape = polygonShape;
body.createFixture(fixtureDef);
polygonShape.dispose();
}

Drawing rotated bodies using JBox2D and JavaFX

I'm doing some initial experiments for a CS course, where I'm trying to use JBox2D as a physics library and drawing using JavaFX. I have had some luck draw circles that fall from the top of the screen, but now I have issues doing the same with rectangles (or any other shape).
I'm able to both draw and simulate, but I cannot figure out how to rotate the drawn nodes in JavaFX using values from the Body objects.
Here's my code for the bodies using JBox2D:
#Override
public Body createBody() {
BodyDef bd = new BodyDef();
bd.type = BodyType.DYNAMIC;
bd.position.set(centerX, centerY);
PolygonShape ps = new PolygonShape();
ps.setAsBox(2.0f,4.0f);
FixtureDef fd = new FixtureDef();
fd.shape = ps;
fd.density = 0.9f;
fd.friction = 0.3f;
fd.restitution = 0.5f;
Body b = PhysicalScene.world.createBody(bd);
b.createFixture(fd);
return b;
}
}
This is drawn using nodes in JavaFX - as written here:
#Override
public Node create() {
Rectangle r = new Rectangle();
r.setHeight(20);
r.setWidth(40);
r.setStroke(Color.RED);
r.setLayoutX(Physics.toPixelX(centerX));
r.setLayoutY(Physics.toPixelY(centerY));
// THIS IS WHAT I'M MISSING
// r.setRotate(/* some value from Body */);
return r;
}
The code isn't pretty, but I hope you get the idea. I am able to rotate the nodes by inserting arguments in the method, but do not know how to get these values from the Body objects themselves.
I hope you can help, thanks!

Box2D static body collision performance issue

I'm using JBox2d to perform collision detection in a game project I'm working on.
I represent obstacles in the world with static bodies. Whenever a dynamic body (i.e. a game character) collides with one of these obstacles, there is a very noticeable drop in performance. Fps will drop from ~120 to ~5. This seems to happen more frequently when the corners of the static body are collided with.
When I set the body type of the world obstacles to be dynamic instead of static, with very high density(to prevent the body from moving when it is collided with), this issue disappears... This solution is not ideal for my situation however......
Any ideas as to what could be causing this huge fps drop?
Here's the code I use to create the static bodies:
BodyDef def = new BodyDef();
def.type = BodyType.STATIC; // If this line is commented and the other
//commented lines are uncommented, the issue goes away.
//def.type = BodyType.DYNAMIC;
def.position.set(worldBounds.getCenterX(), worldBounds.getCenterY());
Body staticBody = b2World.createBody(def);
PolygonShape box = new PolygonShape();
box.setAsBox(worldBounds.getWidth() * 0.5f, worldBounds.getHeight() * 0.5f);
FixtureDef fixture = new FixtureDef();
fixture.shape = box;
fixture.friction = 0.3f;
//fixture.density = 1000000000;
staticBody.createFixture(fixture);
//staticBody.setSleepingAllowed(true);
//staticBody.setFixedRotation(true);
I've tried using a CircleShape instead of a PolygonShape, but that doesn't help anything.
Thank you!
this is the code from the game im working on at the moment which works fine. hopefully if you copy and paste, change a few variable names and stuff it might sort your problem. im new to box2d so cant tell you exactly where the problem lies. hope it helps.
//bodydef
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
bodyDef.position.set(position);
body = world.createBody(bodyDef);
//shape
PolygonShape shape = new PolygonShape();
shape.setAsBox(dimension.x / 2, dimension.y / 2);
//fixture
FixtureDef fixture = new FixtureDef();
fixture.friction = 0.3f;
fixture.shape = shape;
body.createFixture(fixture);
shape.dispose();

setLinearVelocity() not working as expected

I've been messing around with jbox2d and was suprised when the x-velocity of a body was affected by the gravity of the world. Here's my code:
//create world
Vec2 gravity = new Vec2(0, 1);
boolean sleep = true;
world = new World(gravity, sleep);
//create wheel
BodyDef wheelBodyDef = new BodyDef();
wheelBodyDef.type = BodyType.DYNAMIC;
wheelBody = world.createBody(wheelBodyDef);
CircleShape circleShape = new CircleShape();
FixtureDef wheelFixtureDef = new FixtureDef();
wheelFixtureDef.shape = circleShape;
Fixture wheelFixture = wheelBody.createFixture(wheelFixtureDef);
wheelBody.setLinearVelocity(new Vec2(50, 0));
The linear velocity only makes a significant difference if I apply it every frame or if I disable gravity. Can anybody figure out what I'm doing wrong?
Box2d does not have support for zero-gravity simulations; that is why you see no difference when you disable gravity. Also you probably see little difference because a gravity of (0, 1) is very weak; try (0, 10).
Also setLinearVelocity is a rayCast function. You are probably looking for applyForce().
If you set linear velocity on a body means that it will move in direction an speed set with the vector. If you disable gravity there is nothing that effects this movement, but with gravity enabled, its movement is influenced by gravity every frame. If you throw an apple in the outerspace it moves infinite in direction of the shoot, but on earth gravity continuously pulls it down again. If you want to move your body straight in the direction you want, than you have to set the linear velocity every frame. watch this tutorials, #2.35 and #2.36, there the proble is nicely explained.
good luck

Categories