WEBVTT

00:00.120 --> 00:00.660
Hello again!

00:01.020 --> 00:05.610
In this video, we are going to add interaction between the ball and the paddle to our game.

00:07.110 --> 00:13.290
To do this, we need to be able to detect whether the ball and the paddle have actually collided.

00:13.350 --> 00:17.070
And we also need some way to say, what will happen when that occurs.

00:18.710 --> 00:22.880
We are going to add a function, handle_collision(), which will make the ball bounce off the paddle.

00:23.540 --> 00:25.960
This will modify the ball, by changing its direction.

00:25.970 --> 00:27.650
So we take that by reference.

00:28.430 --> 00:36.950
The paddle is not modified, so we take that by const reference. And then later, when we add bricks to the game,

00:37.550 --> 00:40.940
we need a function which will make the brick disappear when the ball hits it.

00:41.570 --> 00:43.790
So we have an overload for the ball and the brick.

00:45.650 --> 00:50.930
We also need to detect whether the ball and the paddle are colliding.

00:51.500 --> 00:56.510
And in fact, this is true for any entity so we can write a polymorphic function.

00:56.840 --> 01:03.680
This takes the base class, entity, by reference, and then we can pass any subclass of entity, for either

01:03.680 --> 01:04.220
argument.

01:04.730 --> 01:08.420
And this will return true if the two entities overlap with each other.

01:09.620 --> 01:16.640
In C++ we only make functions a member the function if it is part of the class interface, and obviously

01:16.640 --> 01:17.560
that is not the case here.

01:17.570 --> 01:20.120
So we make these non-member functions.

01:21.830 --> 01:26.030
So that means that these non-member functions will need to change the direction of the ball.

01:26.420 --> 01:30.170
So we need to add some member functions which will do that.

01:30.650 --> 01:35.480
So we have functions to make the ball move up, and to make it move to the left, or to the right.

01:38.420 --> 01:45.650
So here is out entity header again. We have added the declarations of these member functions.

01:45.650 --> 01:47.390
We are going to make them pure virtual.

01:47.810 --> 01:50.030
So every subclass will have to implement them.

01:52.140 --> 01:56.600
Then in the ball class we implement these. To make the ball move up,

01:56.610 --> 02:04.740
we make the "y" member of the velocity negative. To make the ball move to the left, we make the "x" component negative.

02:05.370 --> 02:09.090
And to make it move to the right, we make the "x" component positive.

02:10.740 --> 02:13.260
And we also need to implement these for the ball.

02:13.830 --> 02:17.880
The paddle cannot move upwards, so we just leave that as an empty member function.

02:18.840 --> 02:24.090
And you could argue that these member functions really should belong in the ball class and not be in the entity.

02:24.810 --> 02:27.180
So again, that is an architectural decision.

02:27.720 --> 02:30.030
And in fact, I have made the exact opposite decision

02:30.030 --> 02:34.950
to the one I made in the last video, so I am afraid I am being rather inconsistent here!

02:37.540 --> 02:44.520
We have a new header where we declare these interaction functions. And then we have a source code file

02:44.540 --> 02:44.920
for them.

02:46.800 --> 02:54.030
To detect interactions, we can actually use it an SFML feature. So we get the bounding box for

02:54.030 --> 03:00.090
each of the entities. And then we can call this member function, intersects(). And this will return true

03:00.090 --> 03:03.690
if the two boxes overlap each other, otherwise false.

03:05.490 --> 03:11.610
And then in our function for the collision, we first of all check whether the paddle and the ball are

03:11.850 --> 03:14.820
overlapping. And if they are not, then there is nothing to do.

03:16.260 --> 03:18.630
If they are, then we make the ball bounce upwards.

03:18.630 --> 03:23.640
So, for the ball to hit the paddle, it must have been coming down, so we can now make it go up, to bounce

03:23.640 --> 03:24.000
off it.

03:24.720 --> 03:28.080
And then we decide whether the ball is going to bounce to the left or the right.

03:29.070 --> 03:34.200
So if the ball is on the left of the paddle, then we make it bounce to the left.

03:34.470 --> 03:36.180
Otherwise we bounce to the right.

03:38.280 --> 03:40.890
And then finally we need to add this code to the main function.

03:40.890 --> 03:42.540
So we include the header.

03:44.630 --> 03:50.240
And then when we do the update graphics, we add a call to this handle_collision().

03:50.660 --> 03:52.780
So this will update the graphics.

03:52.790 --> 03:54.710
So the ball is now going in the right direction.

03:55.160 --> 03:59.120
If it has collided with the paddle. And there we are.

03:59.130 --> 04:01.790
So, yes, it does bounce off the paddle.

04:05.360 --> 04:11.450
So, if it bounces off the left, it goes to the left, and if it bounces off the right hand side of the

04:11.450 --> 04:11.900
paddle.

04:12.770 --> 04:14.960
(Sorry, it is very difficult to do this and talk at the same time!)

04:15.920 --> 04:18.590
So if it is... there is the left.

04:22.140 --> 04:24.840
There is the right. And the right again.

04:25.350 --> 04:27.060
Okay, so that is it for this video.

04:27.840 --> 04:28.650
I will see you next time.

04:28.860 --> 04:31.050
Until then, keep coding!
