gpt4 book ai didi

java - AndEngine 物理示例

转载 作者:行者123 更新时间:2023-12-01 17:30:38 26 4
gpt4 key购买 nike

所以,我正在研究 AndEnginePhysicsExample 代码。我想知道这个方法的含义是什么(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));
}

最佳答案

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));
}

此代码的作用是根据 Sprite 的形状将主体设置为 Sprite 。每次添加人脸时,mFaceCount 都会加 1。该行的作用是:

if(this.mFaceCount % 4 == 0)

检查除以 4 后的余数是否等于 0(其他为 1、2、3)。这一切所做的就是告诉它要将哪个 Sprite 添加到场景中。您会注意到,您首先添加了一个正方形,然后是一个圆形,然后是一个三角形,然后是一个六边形。

真正的代码在这里:

face = new AnimatedSprite(pX, pY, this.mBoxFaceTextureRegion, this.getVertexBufferObjectManager());
body = PhysicsFactory.createBoxBody(this.mPhysicsWorld, face, BodyType.DynamicBody, FIXTURE_DEF);

这会创建一个名为face的 Sprite ,并将一个盒子主体附加到它上面。下一个附加一个圆圈。现在您会注意到接下来的两个是不同的。他们说PhysicsExample.create 而不是PhysicsFactory.create。 PhysicsExample 是 Activity ,因此他们调用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);
}

这会在 Sprite 周围创建一个三角形(不要问我是怎么做的。我不明白他在这里使用的数学,但我准确地复制了它,它适用于大致等边三角形。整个顶点的事情需要一些时间.而且我没有得到 vector !!)。 createHexagonMethod 执行相同的操作,只是使用六边形。我希望回答你的问题?如果我遗漏了什么,请告诉我。

关于java - AndEngine 物理示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11282692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com