我是 SDL 2 的新手,一段时间以来一直被困在子弹和宇宙飞船之间的碰撞检测上。
我明白必须发生什么,但我对什么与什么发生冲突以及放置代码的方式感到困惑。碰撞检测不起作用,我所做的其他一些尝试导致子弹在宇宙飞船的 y 坐标处消失,即使它在屏幕的另一侧!我基于一个懒惰的 foo 教程。
我的整个代码在这里:http ://codepad.org/YaCavBvm
我基于 LazyFoo 的碰撞检测:
//sides of sprite rectangles
int leftBullet, leftEnemy;
int rightBullet, rightEnemy;
int topBullet, topEnemy;
int bottomBullet, bottomEnemy;
//Calculate the sides of bullet
leftBullet = posBullet.x;
rightBullet = posBullet.x + BULLET_WIDTH;
topBullet = posBullet.y;
bottomBullet = posBullet.y + BULLET_HEIGHT;
//Calculate the sides of enemy
leftEnemy = posEnemy.x;
rightEnemy = posEnemy.x + SPRITE_WIDTH;
topEnemy = posEnemy.y;
bottomEnemy = posEnemy.y + SPRITE_HEIGHT;
for (int i=0; i<MAX_BULLETS; i++)
{
if (topBullet == bottomEnemy)
{
arrayofBullets[i].isActive = false;
}
}