Body moving without any force applied? (Box2d)

痞子三分冷 提交于 2020-01-15 08:31:38

问题


So I am making a game in box 2d. In one part I need a ball which has a constant bounce height. I set the ball's restitution to 1 and am applying no force on it whatsoever (except gravity of course). Now this ball, on every bounce, bounces a bit higher everytime till it goes out of the top edge of the screen. What is wrong with this code?

    public class Ball implements ApplicationListener {
 World world ;  
 Box2DDebugRenderer debugRenderer;  
 OrthographicCamera camera;  
 static final float BOX_STEP=1/60f;  
 static final int BOX_VELOCITY_ITERATIONS=8;  
 static final int BOX_POSITION_ITERATIONS=3;  
 static final float WORLD_TO_BOX=0.01f;  
 static final float BOX_WORLD_TO=100f;  
 Rectangle ball;
 Body body;
 /* (non-Javadoc)
 * @see com.badlogic.gdx.ApplicationListener#create()
 */
@Override  
 public void create() {
     world = new World(new Vector2(0, -10), true);
      camera = new OrthographicCamera(); 

      camera.viewportHeight = 480;  
      camera.viewportWidth = 800;  
      camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);  
      camera.update();
      //Ground body  
      BodyDef groundBodyDef =new BodyDef();  
      groundBodyDef.position.set(new Vector2(0, 10 * WORLD_TO_BOX));  
      Body groundBody = world.createBody(groundBodyDef);  
      PolygonShape groundBox = new PolygonShape();
      float w = (camera.viewportWidth * 2) * WORLD_TO_BOX;
      float h = 10.0f * WORLD_TO_BOX;
      groundBox.setAsBox(w,h);  
      groundBody.createFixture(groundBox, 0.0f); 
      String a="gb";
      groundBody.setUserData(a);
      //Dynamic Body  
      BodyDef bodyDef = new BodyDef();  
      bodyDef.type = BodyType.DynamicBody;  
      float posX = (camera.viewportWidth / 8) * WORLD_TO_BOX;
      float posY = (camera.viewportHeight / 2) * WORLD_TO_BOX;
      bodyDef.position.set(posX, posY);
      body = world.createBody(bodyDef);  
   // create a Rectangle to logically represent the ball
      CircleShape dynamicCircle = new CircleShape();  
      dynamicCircle.setRadius(1f);  
      FixtureDef fixtureDef = new FixtureDef();  
      fixtureDef.shape = dynamicCircle;  
      fixtureDef.density = 1.0f;  
      fixtureDef.friction = 0.0f;  
      fixtureDef.restitution =1f;
      body.createFixture(fixtureDef);
      debugRenderer = new Box2DDebugRenderer();  
 }
 @Override  
 public void dispose() {  
 }  
 @Override  
 public void render() {            
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
      debugProjection.set(camera.combined);
      debugProjection.scl(BOX_WORLD_TO);
      debugRenderer.render(world, debugProjection);  
      world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);
          }



 @Override  
 public void resize(int width, int height) {  
 }  
 @Override  
 public void pause() {  
 }  
 @Override  
 public void resume() {  
 }  
}

回答1:


This may be caused by numerical instability. Repeatedly calculating positions of your ball inevitably introduces tiny numerical errors (because a float can only hold a limited number of digits). These may grow and add up from iteration to iteration, bounce to bounce.

You can try using restitution 0.9999999 or something. But chances are that there is no restitution value which gives desired results.

Try to actively compensate for the numerical instability e.g. by controlling the ball's position and/or velocity in situations in which you can calculate values non-iteratively. (I do not know Box2D. Maybe when the ball hits the ground you can reset its speed or something.)




回答2:


Ok I solved it by setting the restitution to 0 and applying an upward force of 5N on each contact. As halfbit said, the problem is that because box2d uses float, the position calculations are not precise which causes minor changes in height on every bounce.




回答3:


If I have done the math right for the calculations, your sphere has a radius of 1m, and your box as dimensions of 16m x 0.1m.

In Box2d, everything needs to be on the scale of 0.1m to 10m. The reason your bodies may be numerically unstable is because the ratio of the width to height is greater than the normal sizes of objects. While you may get some stability by changing the restitution, I think you will always be on the "hairy edge" with these dimensions.

Try changing the dimensions of your objects so the width is 8 and the height is 0.1.



来源:https://stackoverflow.com/questions/20016142/body-moving-without-any-force-applied-box2d

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!