如何在android活动中显示JBox2D对象?

时间:2012-04-25 10:10:06

标签: android jbox2d

我使用jbox2D编写了一个显示圆体的代码。但是当运行此代码时我得到了空白屏幕。现在如何在屏幕上显示这个僵硬的身体。我需要使用任何视图在屏幕上显示它吗?请帮助..

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.BodyDef;
import org.jbox2d.dynamics.BodyType;
import org.jbox2d.dynamics.FixtureDef;
import org.jbox2d.dynamics.World;

import android.app.Activity;
import android.os.Bundle;

public class FirstBox2DGameActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//       setContentView(R.layout.main);     
        Vec2 gravity = new Vec2(0.0f, -10.0f);
        boolean doSleep = true;
        World world = new World(gravity, doSleep);

        // Body definition is created as below.
        BodyDef bd = new BodyDef();
        bd.position.set(100, 200);
        bd.type = BodyType.STATIC;

        // body Shape
        CircleShape cs = new CircleShape();
        cs.m_radius = 5.0f;

        // Fixture defines the material properties of the body. Fixtures are
        // also used for attaching shape to the body. These material properties
        // describe how two bodies should react when they collide with each
        // other.
        FixtureDef fd = new FixtureDef();
        fd.shape = cs;
        fd.density = 0.5f;
        fd.friction = 0.3f;
        fd.restitution = 0.5f;

        // Now final step is to create a body and add fixture to it. The bodies
        // are created using World class.
        Body body = world.createBody(bd);
        body.createFixture(fd);
    }
}

1 个答案:

答案 0 :(得分:0)

嗯,你也必须在屏幕上绘制它。 :)见这里的例子http://disanji.net/2010/12/18/android-2d-box2d/