为什么这个简单的Box2D不起作用?

时间:2011-07-27 02:39:49

标签: java box2d processing

当我运行时,没有任何反应。该块保持在(10.0,-10.0)并且不会下降。 Por que?

这是我的代码:

import processing.core.PApplet;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;  

public class PhysicsWorld extends PApplet {

    private World world;
    private Body block;
    private static final float RATIO = 30f;

    public void createWorld() {
        Vec2 gravity = new Vec2(0.f,-9.8f);
        world = new World(gravity, true);
        world.setGravity(gravity);

    }

    private void createBlock(){
        BodyDef blockDef;
        PolygonShape blockShape;

        blockDef = new BodyDef();
        blockDef.position.set(300.f / RATIO, -300.f / RATIO);

        blockShape = new PolygonShape();
        blockShape.setAsBox(20/RATIO, 25/RATIO);

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = blockShape;
        fixtureDef.density = 1.0f;
        fixtureDef.friction = 0.8f;
        fixtureDef.restitution = 0.3f;

        block = world.createBody(blockDef);
        block.createFixture(fixtureDef);
    }

    public void setup(){
        createWorld();
        createBlock();
    }

    public void draw(){
        world.step(1/60, 8, 3);
        //world.clearForces();
        System.out.println(block.getPosition() +" " + world.getGravity());

    }

}

1 个答案:

答案 0 :(得分:0)

默认情况下,身体是静态的,所以你只需要将身体类型设置为b2_dynamicBody。