© Graeme Stuart 2017

Graeme Stuart, Introducing JavaScript Game Development , https://doi.org/10.1007/978-1-4842-3252-1_12

12. Collision Detection

Graeme Stuart

(1)Market Harborough, Leicestershire, UK

In this chapter we’ll look at detecting collisions between objects. Now that we’re in control of the ship and shooting projectiles, we should think about taking damage and scoring points. These require that we detect collisions between ship and asteroids for damage and between projectiles and asteroids for scoring points.

We’ll use a simple circle-to-circle collision detection, so all our objects must approximate to circles. It’s very simple to calculate: first, you need to calculate the distance between the centers of the two circles. If that distance ...

Get Introducing JavaScript Game Development : Build a 2D Game from the Ground Up now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.