Jet*_*res 0 java android physics game-development andengine
所以,我正在研究AndEngine PhysicsExample代码.我想知道这种方法的含义是什么(http://pastebin.com/Day2hciB):
private void addFace(final float pX, final float pY) {
this.mFaceCount++;
Debug.d("Faces: " + this.mFaceCount);
final AnimatedSprite face;
final Body body;
if(this.mFaceCount % 4 == 0) {
face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (this.mFaceCount % 4 == 1) {
face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (this.mFaceCount % 4 == 2) {
face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else {
face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
}
face.animate(200);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
}
Run Code Online (Sandbox Code Playgroud)
private void addFace(final float pX, final float pY) {
this.mFaceCount++;
Debug.d("Faces: " + this.mFaceCount);
final AnimatedSprite face;
final Body body;
if(this.mFaceCount % 4 == 0) {
face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (this.mFaceCount % 4 == 1) {
face = new AnimatedSprite(pX, pY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else if (this.mFaceCount % 4 == 2) {
face = new AnimatedSprite(pX, pY, this.mTriangleFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsExample.createTriangleBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
} else {
face = new AnimatedSprite(pX, pY, this.mHexagonFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsExample.createHexagonBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
}
face.animate(200);
this.mScene.attachChild(face);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(face, body, true, true));
}
Run Code Online (Sandbox Code Playgroud)
这段代码的作用是根据它的形状将一个体设置为一个精灵.每次添加面部时,mFaceCount会为其自身添加1.这行是做什么的:
if(this.mFaceCount % 4 == 0)
Run Code Online (Sandbox Code Playgroud)
是检查以除以4时的余数是否等于0(而其他的则为1,2,3).所有这一切都告诉它要添加到场景中的哪个精灵.你会注意到你首先添加一个正方形,然后是一个圆,然后是一个三角形,然后是六边形.
真正的代码在这些行中:
face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);
Run Code Online (Sandbox Code Playgroud)
这会创建一个名为face的精灵,并为其附加一个盒体.下一个附加一个圆圈.现在你会注意到接下来的两个是不同的.他们说PhysicsExample.create而不是PhysicsFactory.create.PhysicsExample是活动,因此他们在PhysicsExample中调用方法而不是从PhysicsFactory调用.createTriangleBody实际上调用了这个方法(稍后在代码中):
private static Body createTriangleBody(final PhysicsWorld pPhysicsWorld, final IAreaShape pAreaShape, final BodyType pBodyType, final FixtureDef pFixtureDef) {
/* Remember that the vertices are relative to the center-coordinates of the Shape. */
final float halfWidth = pAreaShape.getWidthScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
final float halfHeight = pAreaShape.getHeightScaled() * 0.5f / PIXEL_TO_METER_RATIO_DEFAULT;
final float top = -halfHeight;
final float bottom = halfHeight;
final float left = -halfHeight;
final float centerX = 0;
final float right = halfWidth;
final Vector2[] vertices = {
new Vector2(centerX, top),
new Vector2(right, bottom),
new Vector2(left, bottom)
};
return PhysicsFactory.createPolygonBody(pPhysicsWorld, pAreaShape, vertices, pBodyType, pFixtureDef);
}
Run Code Online (Sandbox Code Playgroud)
这会在精灵周围创建一个三角形(不要问我怎么做.我不明白他在这里使用的数学,但是我完全复制它并且它适用于大致等边三角形.整个顶点的东西需要一些游戏.而我不要得到矢量!!).createHexagonMethod使用六边形做同样的事情.回答你的问题我希望?如果我遗漏了任何东西,请告诉我.
归档时间: |
|
查看次数: |
779 次 |
最近记录: |