1
00:00:01,002 --> 00:00:02,001
- [Instructor] Hey!

2
00:00:02,001 --> 00:00:05,003
This is the last video of this
section, Collision Detection.

3
00:00:05,003 --> 00:00:08,003
Previously, we looked at
updating the game engine.

4
00:00:08,003 --> 00:00:11,006
In this video, we'll be
coding the detectCollisions

5
00:00:11,006 --> 00:00:13,005
function.

6
00:00:13,005 --> 00:00:16,000
We will handle collision
detection using rectangle

7
00:00:16,000 --> 00:00:19,009
intersection and the
SFML intersects function.

8
00:00:19,009 --> 00:00:22,004
What will be different in
this project is that we will

9
00:00:22,004 --> 00:00:25,003
abstract the collision
detection code into is own

10
00:00:25,003 --> 00:00:28,005
function and Thomas and Bob
as we have already seen,

11
00:00:28,005 --> 00:00:31,006
have multiple rectangles:
m underscore Head,

12
00:00:31,006 --> 00:00:35,007
m underscore Feet, m underscore
Left, m underscore Right,

13
00:00:35,007 --> 00:00:39,000
that we need to check for collisions.

14
00:00:39,000 --> 00:00:42,000
Let's start coding the
detectCollisions function.

15
00:00:42,000 --> 00:00:44,006
To be clear, this function
is part of the engine class.

16
00:00:44,006 --> 00:00:49,002
Open up the engine dot h file
and add a declaration for

17
00:00:49,002 --> 00:00:51,008
a function called detectCollisions.

18
00:00:51,008 --> 00:00:55,005
You have to add the declaration
after void loadLevel

19
00:00:55,005 --> 00:00:56,007
function.

20
00:00:56,007 --> 00:00:57,009
Write a comment.

21
00:00:57,009 --> 00:01:01,005
Run will call all the private functions.

22
00:01:01,005 --> 00:01:05,000
Then bool detectCollisions,
playable character,

23
00:01:05,000 --> 00:01:08,001
ampersand character.

24
00:01:08,001 --> 00:01:10,006
Notice from the signature
that the detectCollision

25
00:01:10,006 --> 00:01:13,003
function takes a polymorphic argument.

26
00:01:13,003 --> 00:01:15,001
A player character object.

27
00:01:15,001 --> 00:01:18,006
As we know, playable
character is abstract and can

28
00:01:18,006 --> 00:01:20,009
never be instantianated.

29
00:01:20,009 --> 00:01:23,004
We do, however, inherit
from it with the Thomas and

30
00:01:23,004 --> 00:01:25,000
Bob classes.

31
00:01:25,000 --> 00:01:27,007
We'll be able to pass
either m underscore Thomas

32
00:01:27,007 --> 00:01:31,006
or m underscore Bob to detect collisions.

33
00:01:31,006 --> 00:01:36,001
Now create a dot CPP file
for detecting collisions.

34
00:01:36,001 --> 00:01:39,004
Right click source files
in the solution explorer,

35
00:01:39,004 --> 00:01:41,005
and select add new item.

36
00:01:41,005 --> 00:01:44,008
Then click C plus plus file
and then in the name field

37
00:01:44,008 --> 00:01:47,003
type detect collisions.

38
00:01:47,003 --> 00:01:49,007
Lastly, click add.

39
00:01:49,007 --> 00:01:53,002
We are now ready to code the
detectCollisions function.

40
00:01:53,002 --> 00:01:55,009
Add this highlighted code
to detectCollisions dot

41
00:01:55,009 --> 00:01:57,005
CPP file.

42
00:01:57,005 --> 00:02:00,005
Note that this is just the
first part of the function.

43
00:02:00,005 --> 00:02:02,005
The first thing that
happens is that we declare a

44
00:02:02,005 --> 00:02:05,006
boolean called reachedGoal.

45
00:02:05,006 --> 00:02:08,001
This is the value that the
detectCollisions function

46
00:02:08,001 --> 00:02:10,001
returns to the calling code.

47
00:02:10,001 --> 00:02:12,004
It is initialized to false.

48
00:02:12,004 --> 00:02:15,009
Next, we declare a FloatRect
called detectionZone

49
00:02:15,009 --> 00:02:18,003
and initialize it with
the same rectangle that

50
00:02:18,003 --> 00:02:21,008
represents the entire rectangle
of the character Sprite.

51
00:02:21,008 --> 00:02:23,009
Note that we will not
actually do intersection tests

52
00:02:23,009 --> 00:02:26,000
with this rectangle.

53
00:02:26,000 --> 00:02:30,001
After that we declare nother
FoatRect called block.

54
00:02:30,001 --> 00:02:33,008
We initialize block as
a 50 by 50 rectangle.

55
00:02:33,008 --> 00:02:36,003
We will see block in use shortly.

56
00:02:36,003 --> 00:02:39,008
Next, we see how we
will use detectionZone.

57
00:02:39,008 --> 00:02:44,004
We initialize four int
variables: startX, startY,

58
00:02:44,004 --> 00:02:48,000
endX and endY, by
expanding the area around

59
00:02:48,000 --> 00:02:50,008
detectionZone a few blocks.

60
00:02:50,008 --> 00:02:53,006
In the four if statements
that follow, we check that

61
00:02:53,006 --> 00:02:56,001
it is not possible to try
and do collisionDetection

62
00:02:56,001 --> 00:02:58,007
on a tile that does not exist.

63
00:02:58,007 --> 00:03:01,004
We achieve this by making
sure we never check positions

64
00:03:01,004 --> 00:03:04,004
less than zero or greater
than the value returned

65
00:03:04,004 --> 00:03:09,003
by getLevelSize dot x or dot y.

66
00:03:09,003 --> 00:03:12,001
Now, all this highlighted
code creates an area with

67
00:03:12,001 --> 00:03:14,002
which to do collision detection.

68
00:03:14,002 --> 00:03:16,009
There is no point doing
collision detection on a block

69
00:03:16,009 --> 00:03:19,005
that is hundreds or
thousands of pixels away from

70
00:03:19,005 --> 00:03:21,000
the character.

71
00:03:21,000 --> 00:03:23,005
In addition, if we try and
do collision detection where

72
00:03:23,005 --> 00:03:26,001
an array position doesn't
exist, less than zero or

73
00:03:26,001 --> 00:03:29,005
greater than getLevelSize,
the game will crash.

74
00:03:29,005 --> 00:03:33,003
Next, add this highlighted
code, which handles the player

75
00:03:33,003 --> 00:03:35,003
falling out of the level.

76
00:03:35,003 --> 00:03:37,005
For a character to stop
falling, it must collide with

77
00:03:37,005 --> 00:03:39,001
a platform.

78
00:03:39,001 --> 00:03:41,005
Therefore, if the player
moves out of the map, where

79
00:03:41,005 --> 00:03:44,006
there are no platforms,
it will continuously fall.

80
00:03:44,006 --> 00:03:46,009
This code checks whether the
character does not intersect

81
00:03:46,009 --> 00:03:49,005
with the FloatRect level.

82
00:03:49,005 --> 00:03:51,007
If it doesn't not, then
it has fallen out of the

83
00:03:51,007 --> 00:03:55,002
level and the spawn function
sends it back to the start.

84
00:03:55,002 --> 00:03:58,003
Now, add this highlighted
code right after the previous

85
00:03:58,003 --> 00:04:00,001
code we added.

86
00:04:00,001 --> 00:04:03,001
It's quite large, I must
say, but don't worry.

87
00:04:03,001 --> 00:04:05,009
We'll go through it and see what it does.

88
00:04:05,009 --> 00:04:08,009
This code does three things
using the same techniques.

89
00:04:08,009 --> 00:04:12,002
It loops through all the
values contained between startX

90
00:04:12,002 --> 00:04:15,005
endX and startY endY.

91
00:04:15,005 --> 00:04:18,000
For each pass, it checks some conditions.

92
00:04:18,000 --> 00:04:20,006
First, has the character
burned or drowned?

93
00:04:20,006 --> 00:04:23,003
This line of code determines
if the current position

94
00:04:23,003 --> 00:04:26,006
being checked is a fire or water tile.

95
00:04:26,006 --> 00:04:29,008
If the character's head intersects
with one of these tiles,

96
00:04:29,008 --> 00:04:32,001
the player is respawned.

97
00:04:32,001 --> 00:04:35,000
We also code an empty if else block,

98
00:04:35,000 --> 00:04:38,004
in preparation for adding
sound in the next section.

99
00:04:38,004 --> 00:04:41,005
Next, has the character
touched a regular tile?

100
00:04:41,005 --> 00:04:43,003
This line determines if
the current position being

101
00:04:43,003 --> 00:04:45,007
checked holds a regular tile.

102
00:04:45,007 --> 00:04:48,000
If it intersects with any
of the rectangles that

103
00:04:48,000 --> 00:04:51,002
represent the various body
parts of the character,

104
00:04:51,002 --> 00:04:53,005
the related function is called stopRight,

105
00:04:53,005 --> 00:04:56,007
stopLeft, stopFalling and stopJump.

106
00:04:56,007 --> 00:04:59,001
The value that is passed to
each of these functions and

107
00:04:59,001 --> 00:05:01,001
how the function uses the
value to reposition the

108
00:05:01,001 --> 00:05:03,002
character is quite nuanced.

109
00:05:03,002 --> 00:05:06,001
While it is not necessary
to closely examine these

110
00:05:06,001 --> 00:05:08,007
values to understand the
code, you might like to look

111
00:05:08,007 --> 00:05:11,004
at the values passed in
and then refer back to the

112
00:05:11,004 --> 00:05:14,003
appropriate function of the
playable character class

113
00:05:14,003 --> 00:05:16,002
in the previous section.

114
00:05:16,002 --> 00:05:19,004
This will help you appreciate
exactly what is going on.

115
00:05:19,004 --> 00:05:22,002
Has the character touched the goal tile?

116
00:05:22,002 --> 00:05:25,005
This is determined by this
highlighted line of code.

117
00:05:25,005 --> 00:05:29,005
All we need to do is
set reachedGoal to true.

118
00:05:29,005 --> 00:05:31,005
The update function of
the engine class will keep

119
00:05:31,005 --> 00:05:34,002
track of whether both
characters, Thomas and Bob,

120
00:05:34,002 --> 00:05:37,000
have reached the goal simultaneously.

121
00:05:37,000 --> 00:05:40,001
We will write this code in
update in just a moment.

122
00:05:40,001 --> 00:05:42,005
Now, write the last line of
code to detect collisions

123
00:05:42,005 --> 00:05:43,009
function.

124
00:05:43,009 --> 00:05:45,002
Add a comment.

125
00:05:45,002 --> 00:05:48,000
All done, return, whether
or not a new level might be

126
00:05:48,000 --> 00:05:49,004
required.

127
00:05:49,004 --> 00:05:52,007
Then write return reachedGoal.

128
00:05:52,007 --> 00:05:55,009
The highlighted line of
code returns reachedGoal,

129
00:05:55,009 --> 00:05:58,003
so that the calling code
can keep track and respond

130
00:05:58,003 --> 00:06:00,007
appropriately if both
characters reach the goal

131
00:06:00,007 --> 00:06:03,001
simultaneously.

132
00:06:03,001 --> 00:06:05,005
All we need to do now is
call the detectCollision

133
00:06:05,005 --> 00:06:08,003
function once per character per frame.

134
00:06:08,003 --> 00:06:11,004
Open the update dot CPP file, and inside

135
00:06:11,004 --> 00:06:15,002
m underscore playing and after Update Bob,

136
00:06:15,002 --> 00:06:17,003
we have to add a code.

137
00:06:17,003 --> 00:06:19,004
Add this highlighted code I've shown.

138
00:06:19,004 --> 00:06:22,005
This code calls the
detectCollision function,

139
00:06:22,005 --> 00:06:25,002
and checks if both Bob and
Thomas have simultaneously

140
00:06:25,002 --> 00:06:26,009
reached the goal.

141
00:06:26,009 --> 00:06:29,004
If they have, the next
level's prepared by setting

142
00:06:29,004 --> 00:06:32,003
m underscore NewLevelRequired to true.

143
00:06:32,003 --> 00:06:35,004
Otherwise, it runs Bobs
collisionDetection.

144
00:06:35,004 --> 00:06:38,001
You can run the game by
clicking on local Windows

145
00:06:38,001 --> 00:06:39,008
debugger button.

146
00:06:39,008 --> 00:06:42,001
You can now walk on the platforms.

147
00:06:42,001 --> 00:06:44,009
You can reach the goal
and start a new level.

148
00:06:44,009 --> 00:06:47,008
Also, for the first time,
the jump buttons will work.

149
00:06:47,008 --> 00:06:50,006
Bob can jump with the up
arrow key and Thomas can

150
00:06:50,006 --> 00:06:53,001
jump with the w key.

151
00:06:53,001 --> 00:06:55,005
If you reach the goal
the next level will load.

152
00:06:55,005 --> 00:06:58,000
If you reach the goal of the
last level, then the first

153
00:06:58,000 --> 00:07:01,007
level will load with a
10% reduced time limit.

154
00:07:01,007 --> 00:07:03,007
Of course, there is no
visual feedback for the time

155
00:07:03,007 --> 00:07:05,007
or the current level,
because we haven't built a

156
00:07:05,007 --> 00:07:07,002
HUD yet.

157
00:07:07,002 --> 00:07:09,002
We will do so in the next section.

158
00:07:09,002 --> 00:07:12,002
Let's see some more collisionDetection.

159
00:07:12,002 --> 00:07:14,006
You have to add this
highlighted code just after the

160
00:07:14,006 --> 00:07:18,004
previous code you added in
the update dot CPP file.

161
00:07:18,004 --> 00:07:21,003
Within the if m underscore PlayingSecion.

162
00:07:21,003 --> 00:07:25,005
This code helps Bob and Thomas
jump on each other's heads.

163
00:07:25,005 --> 00:07:28,001
You can run the game again
by clicking the local Window

164
00:07:28,001 --> 00:07:29,007
debugger button.

165
00:07:29,007 --> 00:07:32,002
And you can stand on the
heads of Thomas and Bob

166
00:07:32,002 --> 00:07:34,008
to get to the hard to reach
places that were previously

167
00:07:34,008 --> 00:07:36,006
not attainable.

168
00:07:36,006 --> 00:07:39,004
You can see how Bob is
jumping on the head of Thomas.

169
00:07:39,004 --> 00:07:41,007
Also, Thomas can jump on the head of Bob.

170
00:07:41,007 --> 00:07:43,003
Awesome!

171
00:07:43,003 --> 00:07:46,005
In this video, we have
learned collision detection.

172
00:07:46,005 --> 00:07:48,001
Cool!

173
00:07:48,001 --> 00:07:49,006
That marks the end of this section.

174
00:07:49,006 --> 00:07:52,002
We started this section
by designing levels in a

175
00:07:52,002 --> 00:07:53,001
text file.

176
00:07:53,001 --> 00:07:55,007
We then built a level
manager class, and coded

177
00:07:55,007 --> 00:07:57,006
load level function.

178
00:07:57,006 --> 00:08:01,000
Then we updated the game
engine to use level manager,

179
00:08:01,000 --> 00:08:03,009
and lastly, we coded a
polymorphic function to handle

180
00:08:03,009 --> 00:08:06,009
the collision detection
for Bob and Thomas.

181
00:08:06,009 --> 00:08:09,003
In the next section, we'll
take a look at sound,

182
00:08:09,003 --> 00:08:11,002
spatialization and HUD.

