logologologologo
  • home
  • learn to code
    • Space Invaders – Beginner’s Course
    • Asteroids – Beginner’s Level 2
    • Space Commander – Beginners Level 3
    • Snake – a Beginner’s Challenge
  • learn to make
    • Arduino Tutorials
    • Raspberry Retro Games Console
  • blog
    • Computer Science
    • Coding
      • Collision Detection
    • Retro Gaming
    • Computer Games for Vision Therapy
  • contact me
Drawing Vector Shapes Code Tutorial
Learn to Code Asteroids – Lesson 1 – Drawing Vector Shapes
18th December 2019
Random Vector Asteroids Tutorial
Learn to Code Asteroids – Lesson 3 – Generating Random Vector Asteroids
1st January 2020
Published by Bob at 19th December 2019
Categories
  • Learn to Code Asteroids
Tags
Simulating velocities with vectors code tutorial

Asteroids Lesson 2 – Moving With Vectors

In lesson 1 we built the software needed to model our vector graphics and draw our shapes at any position on the screen. we also added the code to rotate these shapes to any angle we wanted.

We’re now ready to get these vector graphics moving around the screen. To do this we’ll need to build a model of how objects move in our game. The best way to create these models is to base them as much as possible on the real world and then modify their behaviour to suit our game.

We want our player ship to have a rocket engine to provide thrust. This thrust will accelerate the ship in the direction it’s pointing in. We’ll also be able to rotate the ship as it’s moving and then thrust in a different direction so we need to model how this new thrust direction changes the existing movement of the ship – just like a real space craft!

Our code is going to have to model motion using velocities as vectors (just like in your Physics class!) with a magnitude and direction. When we hit the thrust button our code will have to add the thrust vector to the movement vector to find the resultant velocity vector. This will need a number of functions to add and handle our vector quantities. I’ll be taking you through the theory but I’d like you have have a go at writing the code before I take you through my solution.

We’ll be learning how to define our velocities as vectors, how to split them up into horizontal and vertical components and then how to use these to model the ship movement. We’ll then develop our vector addition function to create the engine thrust in any direction the ship points. We’ll finish by creating the iconic screen wrap around effect used in the real asteroids games.

The full code for this lesson is available below.

Code for Lesson 2

Download the code for this lesson
Share
2
Bob
Bob

Related posts

Lesson 9
11th April 2020

Learn to Code Asteroids – Lesson 11 – Alien Missiles Incoming


Read more
Lesson 9
29th March 2020

Learn to Code Asteroids – Lesson 10 – Alien Ships Sighted


Read more
Lesson 9
5th February 2020

Learn to Code Asteroids – Lesson 9 – Speeding Up The Code


Read more

Comments are closed.

  • Privacy Policy
  • Contact Me
© 2020 Bytes N Bits