简体   繁体   中英

Canvas Game: Collision Detection

For my education I have to make a basic game in HTML5 canvas. The game is a shooter game. When you can move left -> right and space is shoot. When I shoot the bullets will move up. The enemy moves down. When the bullet hits the enemy the enemy has to dissapear and it will gain +1 score. But the enemy will dissapear after it comes up the screen.

Demo: http://jordikroon.nl/test.html

space = shoot + enemy shows up

This is my code:

            for (i=0;i<enemyX.length;i++) {

                if(enemyX[i] > canvas.height) {

                    enemyY.splice(i,1);
                    enemyX.splice(i,1);
                 } else {

                    enemyY[i] += 5;
                    moveEnemy(enemyX[i],enemyY[i]);

                 }
            }

            for (i=0;i<bulletX.length;i++) {

                if(bulletY[i] < 0) {
                    bulletY.splice(i,1);
                    bulletX.splice(i,1);
                 } else {

                    bulletY[i] -= 5;
                    moveBullet(bulletX[i],bulletY[i]);

                    for (ib=0;ib<enemyX.length;ib++) {

                      if(bulletX[i] + 50 < enemyX[ib] ||
                              enemyX[ib] + 50 < bulletX[i] ||
                               bulletY[i] + 50 < enemyY[ib] ||
                               enemyY[ib] + 50 < bulletY[i]) 
                        {   
                            ++score;
                            enemyY.splice(i,1);
                            enemyX.splice(i,1);
                        }
                    }   
                 }
            }

Objects:

        function moveBullet(posX,posY) {
            //console.log(posY);
            ctx.arc(posX, (posY-150), 10, 0 , 2 * Math.PI, false);

        }

        function moveEnemy(posX,posY) {

            ctx.rect(posX, posY, 50, 50);
            ctx.fillStyle = '#ffffff';
            ctx.fill(); 
        }

ORI think I see a problem. You have the following code on your collision detection

                  if(bulletX[i] + 50 < enemyX[ib] ||
                          enemyX[ib] + 50 < bulletX[i] ||
                           bulletY[i] + 50 < enemyY[ib] ||
                           enemyY[ib] + 50 < bulletY[i]) 

This is straight ORs(||), and the +50 is on the less than side. That means that it should actually trigger as true whenever the bullet is not in the hitbox. I suspect that you instead want to have the +50s on the greater than side, and have the ORs(||) be ANDs(&&) instead.

Further to Ben's correction, for future reference it's better to do a line-line collision detection, rather than your point-box collision detection.

The reason for this is, say your enemy is small and your bullet is moving fast. There is always a chance the bullet will appear BEFORE the enemy in one frame, and then behind the enemy in the next, therefore never registering as a hit.

Testing for an intersect with the bullet path and an imaginary line on the enemy will be far more accurate.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM