© 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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.