Andengine:PhysicsFactory.createCircleBody 正在创建一个正方形 body.. 为什么?
Andengine: PhysicsFactory.createCircleBody is creating a square body.. why?
我正在使用它来创建播放器 body
private void addPlayer(){
// viral -- add player
final float startX = (CAMERA_WIDTH - this.mPlayerTextureRegion
.getWidth()) / 2;
final float startY = CAMERA_HEIGHT + 100;
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
final PhysicsHandler physicsHandler = new PhysicsHandler(playerFace);
playerFace.registerUpdateHandler(physicsHandler);
playerBody.setBullet(true);
playerFaceHalfWidth = playerFace.getWidth() / 2;
playerFaceHalfHeight = playerFace.getHeight() / 2;
this.mScene.attachChild(playerFace);
playerFace.setY(CAMERA_HEIGHT - playerFaceHalfHeight * 2);
}
这就是我检测与其他精灵碰撞的方式:
protected void onManagedUpdate(float pSecondsElapsed) {
if (playerFace.collidesWith(this)) {
// safe of touching border - else game over
if (!isPlayerInSafeZone()) {
Log.w("cd", "Game Over");
isGameInProgress = false;
mScene.setIgnoreUpdate(true);
processLevelEnd();
}
}
};
现在的问题是,即使我添加了一个圆圈 body,当其他 sprite 接触播放器的 png 角时,也会检测到与播放器的碰撞。它是一个透明区域,图像中只有一个圆圈,body 就是为这个圆圈创建的。为什么会这样?上面的代码有什么问题?
::根据 Lucaz 的输入更新:
这是我的联系人
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
final String x1id = (String) x1.getBody().getUserData();
final String x2id = (String) x2.getBody().getUserData();
Log.w("cd", "x1 = " + x1id + " --- x2 = " + x2id);
}
@Override
public void endContact(Contact contact)
{
}
@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse)
{
}
};
return contactListener;
}
这就是我为玩家和其他 sprite 设置用户数据的方式:
玩家:
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
playerBody.setBullet(true);
playerBody.setUserData("player");
其他精灵(球):
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, FIXTURE_DEF);
body.setLinearVelocity(vector2);
body.setBullet(true);
body.setUserData("ball");
这就是我将听众添加到物理世界的方式
this.mPhysicsWorld.setContactListener(createContactListener());
写监听器打印用户碰撞数据objects:
但是当一个 sprite 与玩家碰撞时,我得到的玩家数据为 null,但另一个 sprite 的用户数据如下所示...如何解决这个问题?
x1 = null --- x2 = ball
这是因为 collidesWith() 方法正在检查精灵的碰撞,而不是物体的碰撞。因此它会检查两个精灵是否重叠(即使它们是 png 图像,它们仍将被视为非透明矩形)。要检查物体之间的碰撞,还有另一种方法:
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
}
@Override
public void endContact(Contact contact)
{
}
@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse)
{
}
};
return contactListener;
}
请看这个简单教程:
http://www.matim-dev.com/handling-collisions-between-bodies.html
我正在使用它来创建播放器 body
private void addPlayer(){
// viral -- add player
final float startX = (CAMERA_WIDTH - this.mPlayerTextureRegion
.getWidth()) / 2;
final float startY = CAMERA_HEIGHT + 100;
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
final PhysicsHandler physicsHandler = new PhysicsHandler(playerFace);
playerFace.registerUpdateHandler(physicsHandler);
playerBody.setBullet(true);
playerFaceHalfWidth = playerFace.getWidth() / 2;
playerFaceHalfHeight = playerFace.getHeight() / 2;
this.mScene.attachChild(playerFace);
playerFace.setY(CAMERA_HEIGHT - playerFaceHalfHeight * 2);
}
这就是我检测与其他精灵碰撞的方式:
protected void onManagedUpdate(float pSecondsElapsed) {
if (playerFace.collidesWith(this)) {
// safe of touching border - else game over
if (!isPlayerInSafeZone()) {
Log.w("cd", "Game Over");
isGameInProgress = false;
mScene.setIgnoreUpdate(true);
processLevelEnd();
}
}
};
现在的问题是,即使我添加了一个圆圈 body,当其他 sprite 接触播放器的 png 角时,也会检测到与播放器的碰撞。它是一个透明区域,图像中只有一个圆圈,body 就是为这个圆圈创建的。为什么会这样?上面的代码有什么问题?
::根据 Lucaz 的输入更新:
这是我的联系人
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
final String x1id = (String) x1.getBody().getUserData();
final String x2id = (String) x2.getBody().getUserData();
Log.w("cd", "x1 = " + x1id + " --- x2 = " + x2id);
}
@Override
public void endContact(Contact contact)
{
}
@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse)
{
}
};
return contactListener;
}
这就是我为玩家和其他 sprite 设置用户数据的方式:
玩家:
this.playerFace = new Sprite(startX, startY, this.mPlayerTextureRegion,
this.getVertexBufferObjectManager());
this.playerBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld,
playerFace, BodyType.DynamicBody, FIXTURE_DEF);
playerBody.setBullet(true);
playerBody.setUserData("player");
其他精灵(球):
body = PhysicsFactory.createCircleBody(this.mPhysicsWorld, face,
BodyType.DynamicBody, FIXTURE_DEF);
body.setLinearVelocity(vector2);
body.setBullet(true);
body.setUserData("ball");
这就是我将听众添加到物理世界的方式
this.mPhysicsWorld.setContactListener(createContactListener());
写监听器打印用户碰撞数据objects:
但是当一个 sprite 与玩家碰撞时,我得到的玩家数据为 null,但另一个 sprite 的用户数据如下所示...如何解决这个问题?
x1 = null --- x2 = ball
这是因为 collidesWith() 方法正在检查精灵的碰撞,而不是物体的碰撞。因此它会检查两个精灵是否重叠(即使它们是 png 图像,它们仍将被视为非透明矩形)。要检查物体之间的碰撞,还有另一种方法:
private ContactListener createContactListener()
{
ContactListener contactListener = new ContactListener()
{
@Override
public void beginContact(Contact contact)
{
final Fixture x1 = contact.getFixtureA();
final Fixture x2 = contact.getFixtureB();
}
@Override
public void endContact(Contact contact)
{
}
@Override
public void preSolve(Contact contact, Manifold oldManifold)
{
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse)
{
}
};
return contactListener;
}
请看这个简单教程:
http://www.matim-dev.com/handling-collisions-between-bodies.html