Reputation: 730
My problem is that no matter the tuning I don't achieve a fast moving ball. It floats through the air rather than shoots through it.
In the following method I'm creating a ball with the radius of 5
units. Setting the radius to 0.01
yields no difference in the result.
After the creation I apply a LinearImpulse
of ridiculous proportions yet, the ball only floats.
(Also, I'm using the DebugRender. Isn't that supposed to take care of the unit conversions?)
Ball class
public Body createBody(float x, float y, float w, float h) {
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyDef.BodyType.DynamicBody;
bodyDef.position.set(x,y);
Body body = this.getWorld().createBody(bodyDef);
CircleShape shape = new CircleShape();
shape.setRadius(5);
shape.setPosition(new Vector2(0, 0)); // relative to body position
FixtureDef fixtureDef = new FixtureDef();
fixtureDef.shape = shape;
fixtureDef.density = 3f;
fixtureDef.friction = 0.1f;
fixtureDef.restitution = 0.8f;
body.createFixture(fixtureDef);
shape.dispose();
// note the massive impulse vector
body.applyLinearImpulse(new Vector2(10000, 100000), body.getWorldCenter(), true);
return body;
}
render method in Game class
@Override
public void render(){
Gdx.gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
debugRenderer.render(world, camera.combined);
// time step, velocityIterations, positionIterations
world.step(1/40.0f, 6, 2);
// scheduler takes care of creating game objects on the fly
}
Upvotes: 0
Views: 1714
Reputation: 19776
Okay, together with my answer to this question Is there an upper limit on velocity when using box2d? I'll try to explain your problem.
Box2D has a hard limit on velocity, which is 2meters per time step. That means the more timesteps you do per second, the faster your Bodies will be able to move. But since you actually want to have a fixed amount of timesteps per second, going with more timesteps is usually not an option.
What you should do instead is introducing a meter to pixel ratio which could be something in between 1:16 to 1:128. That means 1 meter in box2d should be translated to 16 or 128 pixels in screen units. That will increase the overall precision, because Box2D is more precise in smaller ranges. If you have a ratio of 1:1, it would mean that a standard screen these days would span a range of 1980m x 1080m, which is definetely too much. You can also see that with 60fps = 60 timesteps = 60*2m max distance per second, that would be just 120pixels per second at max speed. And this is what you currently experience as the floating body.
If you would translate 1m to 16px for example, your max speed would increase to 120px*16 = 7200px per second. That would already be sufficient in your case I guess.
Furthermore you should also set body.bullet
to true
to increase the collision precision for very fast moving bodies.
Upvotes: 3
Reputation: 697
One of the reason is World scale, you may probably have a very big size world scale it done if it is a big.
It suggested that for better accuracy use set bullet to true however it takes more calculation
body.bullet = true;
Upvotes: 2