Vectors, Thrust, & Spaceships: Obstacles

Planning
Step 1
Obstacles are a very important piece to almost any game

We have several posibilities for handling obstacles and we will outline three different ways below

Temp1567027261
Step 2
Create an obstacle file
Temp1567036597
Step 3
Include the new script
Temp1567037012
Planning
Step 4
Implementing Collisions

Choose which of the the following types of collision you want to use in your game:

Planning
Step 5
Circular Collision

Circular collision is the easiest to apply and works by comparing the distance between the two objects and their sizes.

We have previously used it when checking collisions between coins and helicopters.

The explanation for circular collision will begin on step 8

Temp1567027739
Planning
Step 6
Basic rectangular collision

Basic rectangular collision checks for collisions between rectangular objects

It does not account for rotation.

This method was used in JavaScript Game Design.

The explanation will begin on step 20

Temp1567028318
Planning
Step 7
Rotated Rectangular Collision

Rotated Rectangular collision expands basic rectangular collision to check four directions instead of two

The methods involved use linear algebra to project each corner onto a line parallel to an edge.

While more complicated, it allows us to handle collisions at any rotation.

The explanation will begin on step 30

Temp1567030473
Planning
Step 8
Circular Collision
Temp1568063607
Step 9
Create a class
Temp1567107196
Step 10
Add a constructor
Temp1567107262
Step 11
Add a draw method
Temp1567107314
Step 12
Create an instance of this class
Temp1567107474
Challenge
Step 13
Draw it!
Temp1568234485 Temp1567107576
Step 14
Add a method to the CircleObstacle class to check for collisions with an object
Temp1567107745
Step 15
Calculate the radii

Because object may or may not have a size, we first check to see if it has a size.

If not, we use the larger of the object's width/height

Temp1567108139
Step 16
Find the distances to compare
Temp1568063870
Step 17
Return true if touching
Temp1568063930
Challenge
Step 18
Test the function by changing the color of the obstacle when it is touching the player
Temp1567108569 Temp1567108682
Check Your Work
Step 19
You should now have working CircleObstacles

We'll prevent the player from moving into them starting on step 50

Temp1567108834
Planning
Step 20
RectangularObstacles
Temp1568063996
Step 21
Rectangular Collision!
Temp1567108965
Step 22
Add a constructor
Temp1567109163
Step 23
Add a draw method
Temp1567109290
Step 24
Create an obstacle using our new class

If you have already made circular obstacles you will need to update your existing code instead of adding this

Temp1567109391
Challenge
Step 25
Draw it
Temp1568234584 Temp1567109563
Step 26
Add a method to check for collisions with an object
Temp1567109982
Step 27
Compare opposite sides of the two objects
Temp1567544830 Temp1568064858 Temp1568064589 Temp1568064678 Temp1568064792
Challenge
Step 28
Test the function by changing the color of the obstacle when it is touching the player
Temp1567111120 Temp1567111239
Check Your Work
Step 29
You should now have working rectangular obstacles

these will work slightly better with a square ship

We'll prevent the player from moving into them on step 50

Temp1567111375
Planning
Step 30
Rotated Rectangular Obstacles
Temp1568064921
Step 31
Create a class for rotated rectangular obstacles
Temp1567111510
Step 32
Add a constructor
Temp1567111692
Step 33
Create a draw method
Temp1567115351
Step 34
Create a new instance of our RotatedRectangularObstacle class

If you have already made circular or rectangular obstacles you will need to update your existing code instead of adding this

Temp1567112008
Challenge
Step 35
Draw the obstacle
Temp1568234626 Temp1567112125
Step 36
Add a method to see if it is touching an object
Temp1567115448
Step 37
Create a function to find the corners of a rotated rectangle

We need the corners to calculate parallel lines and project the rectangles onto them

Temp1568237374 Temp1568237462
Step 38
Find the corners of both objects
Temp1567115649
Step 39
Next we can use corners to find parallel lines
Temp1568075339 Temp1567115917 Temp1567116143
Step 40
Create a loop
Temp1567116294
Information
Step 41
Projecting points onto lines

Projecting points onto a line is useful for finding the closest point on that line to the point, or in our case comparing different points progression along that line

Temp1568070261
Step 42
Project our corners onto each line
Temp1567116905 Temp1567199683 Temp1567199528
Information
Step 43
Scalar Values

We can compare scalar values against each other much more easily than vector values

Temp1568071682
Step 44
Evaluate how far along the line each point is as a scalar value
Temp1567199929 Temp1567200023 Temp1567200542
Step 45
Sort these values from least to greatest for each object
Temp1567200700
Step 46
Compare The largest and smallest values
Temp1567200858 Temp1568072620
Step 47
Report positive collisions
Temp1567201412
Step 48
Update the fill to show collisions
Temp1567201521 Temp1567201569
Check Your Work
Step 49
You should now have working rotated obstacles
Temp1567201654
Planning
Step 50
Next we will be making our obstacles solid
Temp1568072955
Step 51
Begin acting on collisions!
Temp1567204262
Step 52
Check for collisions before moving

If there will be a collision, we bounce instead, losing half our velocity

Temp1567542523
Check Your Work
Step 53
Your wall should now be solid
Temp1567542596
Step 54
Create a file to store the info for our level
Temp1567542813
Step 55
Load in our new file
Temp1567542928
Step 56
Create an array of obstacles

You can add as many obstacles as you want and they can be all one type or a mix

Temp1567543853
Step 57
Draw them all!
Temp1568245852
Step 58
Check collisions with all the new obstacles
Temp1567544304
Check Your Work
Step 59
Many working obstacles
Temp1567544372
Challenge
Step 60
Create an arena/track for your ship to explore
Temp1567546286
Vectors, Thrust, & Spaceships: Obstacles Info

Account

MVCode Clubs

Created By

Missing tiny jake wheaton

Access Level

premium

For Teachers and Schools

Teach coding to your students with MVCode Teach

MVCode offers an integrated product designed to teach coding to students.

Learn more about MVCode Teach