AndEngine学习 : BasePhysicsJoint(简单地和物理模型的联系)

  和以前一样,代码一目了然,我把该写的东西都写到注释里边了,和大家分享!

  所需图片:

face_box_tiled.png

face_circle_tiled.png

public class BasePhysicsJoint extends BaseGameActivity implements
IAccelerometerListener, IOnSceneTouchListener
{
// ===========================================================
// Constants
// ===========================================================

protected static final int CAMERA_WIDTH = 720;
protected static final int CAMERA_HEIGHT = 480;

// ===========================================================
// Fields
// ===========================================================

private BitmapTextureAtlas mBitmapTextureAtlas;

private Scene mScene;

protected TiledTextureRegion mBoxFaceTextureRegion;
protected TiledTextureRegion mCircleFaceTextureRegion;

// mPhysicsWorld: 我们所有的有关Sprite的物理操作都需要通过该对象
protected PhysicsWorld mPhysicsWorld;

private int mFaceCount = 0;

public Engine onLoadEngine()
{
Toast.makeText(this, "Touch the screen to add objects", Toast.LENGTH_SHORT).show();

final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
final EngineOptions engineOptions = new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera);
engineOptions.getTouchOptions().setRunOnUpdateThread(true);
return new Engine(engineOptions);
}

public void onLoadResources()
{
this.mBitmapTextureAtlas = new BitmapTextureAtlas(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
this.mBoxFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_box_tiled.png", 0, 0, 2, 1); // 64x32
this.mCircleFaceTextureRegion = BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this, "face_circle_tiled.png", 0, 32, 2, 1); // 64x32
this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
}

public Scene onLoadScene()
{
this.mScene = new Scene();
this.mScene.setBackground(new ColorBackground(0, 0, 0));
this.mScene.setOnSceneTouchListener(this);

// 创建物理世界
this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, 0), false);

final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);

final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 1f, 1f);

// 创建刚体矩形,用来定义物理世界的边界
PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);

this.mScene.attachChild(ground);
this.mScene.attachChild(roof);
this.mScene.attachChild(left);
this.mScene.attachChild(right);

this.mScene.registerUpdateHandler(this.mPhysicsWorld);

return mScene;
}

public void onLoadComplete()
{

}

public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
{
if(mPhysicsWorld != null)
{
if(pSceneTouchEvent.isActionDown())
{
this.addFace(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
return true;
}
}
return false;
}

public void onAccelerometerChanged(AccelerometerData pAccelerometerData)
{
// 根据重力感应计算重力加速度
final Vector2 gravity = Vector2Pool.obtain(pAccelerometerData.getX(), pAccelerometerData.getY());
this.mPhysicsWorld.setGravity(gravity);
Vector2Pool.recycle(gravity);
}

@Override
public void onResumeGame()
{
super.onResumeGame();
this.enableAccelerometerSensor(this);
}

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

private void addFace(final float pX, final float pY)
{
this.mFaceCount++;

final AnimatedSprite face;
final Body body;

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

if (this.mFaceCount % 2 == 0) {
face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion);
face.setScale(MathUtils.random(0.5f, 1.25f));
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, objectFixtureDef);
} else {
face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion);
face.setScale(MathUtils.random(0.5f, 1.25f));
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, objectFixtureDef);
}

face.animate(200);

this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face,
body, true, true));
}
}



原文地址:https://www.cnblogs.com/xiaobo68688/p/2230270.html