【发布时间】:2013-12-17 19:20:49
【问题描述】:
所以这就是正在发生的事情,游戏一直停止,因为两个圆圈相互碰撞,我试图通过确保 ID 是正方形 ID 来防止这种情况发生,我试图让圆圈碰撞在一起而忽略碰撞,但它没有这样做,我不知道如何阻止它相互碰撞,只允许它与正方形碰撞。
基本上我想阻止圆圈与另一个圆圈碰撞并让它与正方形发生碰撞。
任何帮助都会非常感谢您
game.engine.engine 类
//collision detection class
iter = p_group.listIterator();
Sprite spr = null;
while (iter.hasNext()) {
spr = (Sprite)iter.next();
//remove from list if flagged
if (!spr.getAlive()) {
iter.remove();
continue;
}
//is collision enabled for this sprite?
if (spr.getCollidable()) {
//has this sprite collided with anything?
if (spr.getCollided()) {
//is the target a valid object?
if (spr.getOffender() != null) {
/*
* External func call: notify game of collision
* (with validated offender)
*/
collision(spr);
//reset offender
spr.setOffender(null);
}
//reset collided state
spr.setCollided(false);
}
}
}
这是我在扩展 game.engine.engine 包的游戏类中检查碰撞的地方。
游戏类
// all the circles were are set up the same, theres four
// circletopleft, circletopright, circlebottomleft
// circle bottom left collides with circle top right
// theres no diffence between the code of the circles they are all the same
/*ball*/circleBottomLeft.setCollidable(true);
circleBottomLeft.setIdentifier(CIRCLEID_3);
addToGroup(circleBottomLeft);
//init ball sprite
circleBottomRight.position = new Float2(550, h-550);
circleBottomRight.setVelocity(new Float2(10.0f, -9.0f));
//keep ball inside secreen boundary
Point size4 = circleBottomRight.getSize();
circleBottomRight.addAnimation(new ReboundBehavior(new RectF
(0 , 0 , w-size4.x, h-size4.y),size4,circleBottomRight.getVelocity()) );
/*ball*/circleBottomRight.setCollidable(true);
circleBottomRight.setIdentifier(CIRCLEID_4);
addToGroup(circleBottomRight);
}
//collision class extended from game.engine.Engine
public void collision(Sprite sprite) {
// TODO Auto-generated method stub
final int oneMin = 60;
final int twoMin = 120;
final int threeMin = 180;
final int fourMin = 240;
final int fiveMin = 300;
Sprite other = sprite.getOffender();
Float2 vel = sprite.getVelocity();
switch (sprite.getIdentifier()) {
case CIRCLEID_1:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_2:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_3:
vel = sprite.getVelocity();
other.position.y += 1;
case CIRCLEID_4:
vel = sprite.getVelocity();
other.position.y += 1;
break;
}
switch (other.getIdentifier()) {
case oneMin:
if (this.getElapsedTime() == oneMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case twoMin:
if (this.getElapsedTime() == twoMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case threeMin:
if (this.getElapsedTime() == threeMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case fourMin:
if (this.getElapsedTime() == fourMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case fiveMin:
if (this.getElapsedTime() == fiveMin) {
vel.y = -1;
sprite.setVelocity(vel);
sprite.position.y += vel.y*2;
}
break;
case SQUARE_ID:
//make sure its not touched
if (other.getIdentifier() == SQUARE_ID &&
other.getIdentifier() == CIRCLEID_1 ||
other.getIdentifier() == CIRCLEID_2 ||
other.getIdentifier() == CIRCLEID_3 ||
other.getIdentifier() == CIRCLEID_4) {
stop();
Log.d("Game", "Collided, I collided with something");
if (p_paused) {
Message msg = handler.obtainMessage();
msg.arg1 = 1;
handler.sendMessage(msg);
当两个圆圈碰撞顶部时它会一直停止(游戏开始后417秒发生碰撞,并且它没有碰到正方形。它恰好发生在左下角的圆圈与右上角的圆圈碰撞时
【问题讨论】:
标签: java android collision-detection game-engine