applyLinearImpulse不工作(Box2d)?

时间:2013-11-13 17:13:38

标签: java android box2d

我正在使用ContactListener来听取任何接触,如果在球和地面之间发现碰撞,球应该向上冲动,这在void endContact()中提供ContactListener类的功能。但对于任何冲动值,这种冲动根本没有任何影响。对于任何冲动值,球只会反弹相同的距离。我究竟做错了什么。这是完整的代码:

public class Game 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;

    @Override  
     public void create() {
          world = new World(new Vector2(0, -100), 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));  
          Body groundBody = world.createBody(groundBodyDef);  
          PolygonShape groundBox = new PolygonShape();  
          groundBox.setAsBox((camera.viewportWidth) * 2, 10.0f);  
          groundBody.createFixture(groundBox, 0.0f);  

          //Dynamic Body  
          BodyDef bodyDef = new BodyDef();  
          bodyDef.type = BodyType.DynamicBody;  
          bodyDef.position.set(camera.viewportWidth / 8, camera.viewportHeight / 2);  
          body = world.createBody(bodyDef); 

          // create a Rectangle to logically represent the ball
          CircleShape dynamicCircle = new CircleShape();  
          dynamicCircle.setRadius(40f);  
          FixtureDef fixtureDef = new FixtureDef();  
          fixtureDef.shape = dynamicCircle;  
          fixtureDef.density = 1.0f;  
          fixtureDef.friction = 0.0f;  
          fixtureDef.restitution =1f;
          body.createFixture(fixtureDef);
          debugRenderer = new Box2DDebugRenderer();  

          listener listener1=new listener();
          world.setContactListener(listener1);
     }
     @Override  
     public void render() {            
          Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);  
          debugRenderer.render(world, camera.combined);  
          world.step(BOX_STEP, BOX_VELOCITY_ITERATIONS, BOX_POSITION_ITERATIONS);


     }  
     class listener implements ContactListener 
     {

        @Override
        public void beginContact(Contact contact) {
            // TODO Auto-generated method stub

        }

        @Override
        public void endContact(Contact contact) {
            // TODO Auto-generated method stub
            body.applyLinearImpulse(0, 500000f, body.getWorldCenter().x, body.getWorldCenter().y, true);
            //body.setLinearVelocity(0,50000000);
            System.out.println("Collision detected");
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {
            // TODO Auto-generated method stub

        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {
            // TODO Auto-generated method stub

        } 

     }; 

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

2 个答案:

答案 0 :(得分:0)

在最初的C ++版本中,在Step函数中调用EndContact,这是因为引擎仍然在使用它而无法改变世界中的任何东西。我认为Java版本是一样的。您可能需要记下球已经反弹的事实,并在步骤完成后应用冲动。

答案 1 :(得分:0)

对冲动没有影响的原因是你试图在endcontact方法中应用冲动。

在box2d ContactListener方法中,您无法更改正文的属性或位置。

你可以这样做

boolean b;
 @Override
    public void endContact(Contact contact) {
        // TODO Auto-generated method stub
        //body.applyLinearImpulse(0, 500000f, body.getWorldCenter().x, body.getWorldCenter().y, true);
        //body.setLinearVelocity(0,50000000);
       b = true;
        System.out.println("Collision detected");
    }

在你更新方法.....

if(b)
{
  body.applyLinearImpulse(0, 500000f, body.getWorldCenter().x, body.getWorldCenter().y, true);
}