2

2つのボールが動いていて、もう1つは静的(動かない)のデモアプリケーションを作成しています。実行時に両方を衝突させたいだけです。しかし、このコード行でupdate position trueを適用すると、移動するオブジェクトが移動しません:

this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, body, false, true));

ここに私のコードがあります

public class DizyBall extends SimpleBaseGameActivity implements IOnSceneTouchListener {
// ===========================================================
// Constants
// ===========================================================

private static final int CAMERA_WIDTH = 740;
private static final int CAMERA_HEIGHT = 480;

private static final float DEMO_VELOCITY = 150.0f;

// ===========================================================
// Fields
// ===========================================================
private Scene mScene;
private PhysicsWorld mPhysicsWorld;
private Body body;
private FixtureDef objectFixtureDef ;
private BitmapTextureAtlas mBitmapTextureAtlas;
private TiledTextureRegion mFaceTextureRegion;
private TextureRegion mColiTextureRegion;

// ===========================================================
// Constructors
// ===========================================================

// ===========================================================
// Getter & Setter
// ===========================================================

// ===========================================================
// Methods for/from SuperClass/Interfaces
// ===========================================================

@Override
public EngineOptions onCreateEngineOptions() {
    final Camera camera = new Camera(0, 0, DizyBall.CAMERA_WIDTH, DizyBall.CAMERA_HEIGHT);

    return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, new RatioResolutionPolicy(DizyBall.CAMERA_WIDTH, DizyBall.CAMERA_HEIGHT), camera);
}

@Override
public void onCreateResources() {
    BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");

    this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), 48, 48, TextureOptions.BILINEAR);
    this.mFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "ball.png", 0, 0, 1, 1);
    this.mColiTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBitmapTextureAtlas, this, "ball.png",0,0);

    this.mBitmapTextureAtlas.load();
}

@Override
public Scene onCreateScene() {
    this.mEngine.registerUpdateHandler(new FPSLogger());


    mScene = new Scene();
    mScene.setBackground(new Background(0.09804f, 0.6274f, 0.8784f));
    mScene.setOnSceneTouchListener(this);


    objectFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);

    this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, 0), false, 3, 2);

    final float centerX = (DizyBall.CAMERA_WIDTH - this.mFaceTextureRegion.getWidth()) / 2;
    final float centerY = (DizyBall.CAMERA_HEIGHT - this.mFaceTextureRegion.getHeight()) / 2;

    final Sprite Coli = new Sprite(centerX, centerY, this.mColiTextureRegion, this.getVertexBufferObjectManager());
    body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, Coli, BodyType.DynamicBody, objectFixtureDef);
    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(Coli, body, true, true));


    mScene.attachChild(Coli);
    this.mScene.registerUpdateHandler(this.mPhysicsWorld);
    return mScene;
}

// ===========================================================
// Methods
// ===========================================================


@Override
public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {

  //        pSceneTouchEvent
    if(this.mPhysicsWorld != null) {
        if(pSceneTouchEvent.isActionUp()){
            final Ball ball = new Ball(pSceneTouchEvent.getX(), pSceneTouchEvent.getY(), this.mFaceTextureRegion, this.getVertexBufferObjectManager());



            body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, objectFixtureDef);

            this.mScene.attachChild(ball);
            this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, body, false, true));
            return false;
        }
    }else{
        Log.v("PhysicsWorld","NULL");
    }

    return false;
}
// ===========================================================
// Inner and Anonymous Classes
// ===========================================================

private static class Ball extends AnimatedSprite {
    private final PhysicsHandler mPhysicsHandler;

    public Ball(final float pX, final float pY, final TiledTextureRegion pTextureRegion, final VertexBufferObjectManager pVertexBufferObjectManager) {
        super(pX, pY, pTextureRegion, pVertexBufferObjectManager);
        this.mPhysicsHandler = new PhysicsHandler(this);
        this.registerUpdateHandler(this.mPhysicsHandler);
        this.mPhysicsHandler.setVelocity(DizyBall.DEMO_VELOCITY, DizyBall.DEMO_VELOCITY);
    }

    @Override
    protected void onManagedUpdate(final float pSecondsElapsed) {
        if(this.mX < 0) {
            this.mPhysicsHandler.setVelocityX(DizyBall.DEMO_VELOCITY);
        } else if(this.mX + this.getWidth() > DizyBall.CAMERA_WIDTH) {
            this.mPhysicsHandler.setVelocityX(-DizyBall.DEMO_VELOCITY);
        }

        if(this.mY < 0) {
            this.mPhysicsHandler.setVelocityY(DizyBall.DEMO_VELOCITY);
        } else if(this.mY + this.getHeight() > DizyBall.CAMERA_HEIGHT) {
            this.mPhysicsHandler.setVelocityY(-DizyBall.DEMO_VELOCITY);
        }

        super.onManagedUpdate(pSecondsElapsed);
    }
}
 }

以下は実行時のイメージですが、どちらも衝突ではありません。 ここに画像の説明を入力

4

1 に答える 1

0

mScene.registerUpdateHandler(this.mPhysicsHandler); の後に次の行を追加するだけです。

mScene.registerUpdateHandler(this);

次に、updatehandler を実装し、実装されていないメソッドを追加します。これで、onUpdate メソッドで何でもできるようになります。

body.setLinearVelocity(10,10);

他のオブジェクトと衝突させます。

于 2012-08-01T12:29:58.550 に答える