Reputation: 153
I have used LibGDX Project Setup by Aurélien Ribon for my game. It creates the camera in the following way:
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
camera = new OrthographicCamera(1, h/w);
I like it, it makes positioning the sprites easier, because I have a range from -0.5 to 0.5 in horizontal axis and from -0.5*screenHeight to 0.5*screenHeight in vertical axis.
However, I believe it causes some problems with Box2d. I'm trying to create a bouncing ball, I do the following:
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(0, 0); //center of screen
ballBody = world.createBody(bodyDef);
dynamicCircle = new CircleShape();
dynamicCircle.setRadius(0.02f); // really small body size
ballFixture = new FixtureDef();
ballFixture.shape = dynamicCircle;
ballFixture.density = 0.5f;
ballFixture.restitution = 0.9f;
ballBody.createFixture(ballFixture);
There is also ground static body near the bottom of the screen. The problem is, that ball bounces couple of times and then at the end (when the bounces should be really low and frequent) it just lands and doesn't move.
I believe this has something to do with my world units (1 unit in horizontal axis). How can I keep the current world setup and get correct Box2d physics?
Upvotes: 0
Views: 505
Reputation: 8262
These sizes are too small to work well with the default values that Box2D uses when calculating things like overlapping between shapes, maximum velocity, and in your case, the minimum velocity required to cause a bounce.
You could change all the defaults (in b2Settings.h) but it is a much better idea to just scale up all the dimension used for the physics world. Ideally you would want to have the most common dynamic body in the scene to be around 1 physics unit in size, so you could scale everything up by say 25 or so.
This just means that you would multiply every dimension and position you give to Box2D by that factor when setting things up, and divide every dimension and position that Box2D gives you (eg. from GetPosition etc) by the same factor before using it with the rest of your game.
You will find that this changes the behavior of the ball because now it is the size of a large boulder instead of a tiny pebble, so gravity will appear to affect it slower and it will feel more floaty. This can be countered by making gravity stronger.
Upvotes: 0
Reputation: 961
declare these variables in your class
static final float BOX_STEP = 1 / 60f;
static final int BOX_VELOCITY_ITERATIONS = 6;
static final int BOX_POSITION_ITERATIONS = 0;
static final float WORLD_TO_BOX = 0.01f;
static final float BOX_WORLD_TO = 100f;
but i think the main problem is
ballFixture.restitution = 0.9f;
put some other higher value in restitution, i think you will get good resultss...
;
Upvotes: 0