【问题标题】:How do I apply physics on moving Ball which Collide with non moving ball - AndEngine我如何将物理应用于与非移动球碰撞的移动球 - AndEngine
【发布时间】:2012-08-01 12:23:14
【问题描述】:

我正在创建 Demo 应用程序,其中 2 个 Ball 一个正在移动,一个是静态的(不移动)。我只想在运行时碰撞它们。但是当我在这行代码中应用 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);
    }
}
 }

Bellow 是运行时的图像,但两者都不是碰撞。

【问题讨论】:

  • 当我评论这一行时它触动了:this.registerUpdateHandler(this.mPhysicsHandler);意味着它不会移动..:/
  • 看起来,您在悬赏这个问题之前没有做太多研究。
  • 我使用了 elastic 1,所以它的反弹率相同,我尝试了 body.setLinearVelocity(10,10);也。但由于重力,它不能很好地移动意味着不会有角度。

标签: android andengine collision


【解决方案1】:

只需在 mScene.registerUpdateHandler(this.mPhysicsHandler); 后面加上这一行即可

mScene.registerUpdateHandler(this);

然后实现updatehandler并添加未实现的方法,现在你可以在onUpdate方法中做任何事情,比如

body.setLinearVelocity(10,10);

使其与其他物体发生碰撞。

【讨论】:

  • 您可以查看 AndEngine 示例。或者你可以告诉我现在出了什么问题,我会帮你解决的。
  • 第一个布尔值用于启用移动,第二个用于启用旋转。你有没有做任何事情来移动球?比如对其施加力或设置速度?
  • 您没有将 Linear Velocity 设置为 Physics Handler,而是直接将其设置为 body。代码好像不是你自己写的,我强烈建议你先看看例子:github.com/nicolasgramlich/AndEngineExamples.
  • 嗨,我得到了解决方案..prob 是重力,我想将其设为 0。所以那里只需要再增加 1 行。 mPhysicsWorld.setGravity(new Vector2()); Nd 它的作品 :) 现在我正在努力,将球移动到点击/触摸的地方.. 如果你有想法然后.. :)
  • 好吧,如果你告诉我重力的问题,我会早点帮助你的。无论如何,玩弄你的代码并自己解决问题总是好的。顺便说一句,不客气:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-07-19
  • 2013-06-12
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多