Learn to Code Asteroids – Lesson 2 – Moving With Vectors
19th December 2019Learn to Code Asteroids – Lesson 4 – Point in Polygon Collision Detection
1st January 2020Learn to Code Asteroids – Lesson 3 – Generating Random Vector Asteroids
We’ve now got all the code we need to create move and draw our vector shapes on screen. We’ve got our player ship moving and rotating and we can use thrust button to accelerate it in any direction. We now need to add in some asteroids.
These too will be defined as vector objects but we don’t want them all to be the same shape. In this lesson will work out an algorithm that lets us generate random vector asteroids so that we can use our vector handling code to model them and include them in our game.
Creating algorithms, which are basically methods for performing certain tasks, is one of the main parts of writing any program. Some algorithms will be quite short such as our code to rotate a point around the origin. Others will be a bit more complex and requires to design the way in which algorithm will work before we go anywhere near our computer. For our random vector asteroid algorithm we’re going to have to work out how to create a series of points that describe the shape of asteroid keeping it within a certain range of sizes, but also making sure it looks like a proper boulder floating in space.
Again I’ll be talking you through all the theory behind the algorithm before asking you to have a go at programming it yourself. The more programming practice you get the better you’ll become. Once you’ve had a chance to develop their own code I’ll then take you through my solution to the algorithm so we can all get to a full working model by the end of the lesson.
As usual the full code for this lesson is available below.