From the course: Game Development Foundations: Game-Related Math

Unlock the full course today

Join today to access over 22,700 courses taught by industry experts or purchase this course individually.

Point collisions

Point collisions - JavaScript Tutorial

From the course: Game Development Foundations: Game-Related Math

Start my 1-month free trial

Point collisions

- [Instructor] In this example, we're going to test to see if a single point is inside of another object. This is the basic cornerstone for doing collision detection. We'll start by going into the exercise files folder and open up chapter two's folder. Then, navigate to 02_01 and open up the Start folder. Here we'll find the point-collision-example.js file. Open this up in your text editor of choice. Select all the code and copy it. Next, switch over to CodePen and create a New Pen. And we'll simply name this Point Collision Example. Next, double click on the JS tab in order to expand it and paste in our code. Once our preview renders, simply drag the center bar over to the right to give yourself some more room to code. In this example, we follow the mouse's x, y position. We want to detect whether the mouse is inside of the red box. In order to do this our code is simply looping through each frame, saving out the mouse's x and y position, and calling the contains function on line 49…

Contents