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