# 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

Step 2
Create an obstacle file
Step 3
Include the new script
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

### The explanation for circular collision will begin on step 8

Planning
Step 6
Basic rectangular collision

### The explanation will begin on step 20

Planning
Step 7
Rotated Rectangular Collision

### The explanation will begin on step 30

Planning
Step 8
Circular Collision
Step 9
Create a class
Step 10
Step 11
Step 12
Create an instance of this class
Challenge
Step 13
Draw it!
Step 14
Add a method to the CircleObstacle class to check for collisions with an object
Step 15

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

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

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

Planning
Step 20
RectangularObstacles
Step 21
Rectangular Collision!
Step 22
Step 23
Step 24
Create an obstacle using our new class

Challenge
Step 25
Draw it
Step 26
Add a method to check for collisions with an object
Step 27
Compare opposite sides of the two objects
Challenge
Step 28
Test the function by changing the color of the obstacle when it is touching the player
Step 29
You should now have working rectangular obstacles

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

Planning
Step 30
Rotated Rectangular Obstacles
Step 31
Create a class for rotated rectangular obstacles
Step 32
Step 33
Create a draw method
Step 34
Create a new instance of our RotatedRectangularObstacle class

Challenge
Step 35
Draw the obstacle
Step 36
Add a method to see if it is touching an object
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

Step 38
Find the corners of both objects
Step 39
Next we can use corners to find parallel lines
Step 40
Create a loop
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

Step 42
Project our corners onto each line
Information
Step 43
Scalar Values

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

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

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

Step 53
Your wall should now be solid
Step 54
Create a file to store the info for our level
Step 55
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

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

Account

MVCode Clubs

Created By

Access Level