WEBVTT

00:00.120 --> 00:01.830
Hello again! In this video,

00:01.830 --> 00:06.540
we are going to look at how to make our bouncing ball bounce off the edges of the window.

00:08.310 --> 00:11.280
To do this, we need to be able to find the position of the ball.

00:11.640 --> 00:14.780
So we are going to add a member function which will return that.

00:15.810 --> 00:21.900
We are also going to add some member functions which will come in useful, later on. So we can find the centre

00:21.900 --> 00:25.140
of the ball, and also the bounding rectangle.

00:26.220 --> 00:31.320
The bounding rectangle is actually the same as the texture rectangle, but it has different significance.

00:31.950 --> 00:33.390
With a texture rectangle.

00:33.400 --> 00:34.770
we are interested in the image

00:34.770 --> 00:37.560
that it is bound to. With a bounding rectangle,

00:37.980 --> 00:39.720
we are interested in its geometry.

00:41.520 --> 00:46.530
When we get the bounding rectangle, we will get the coordinates of the origin.

00:47.430 --> 00:53.880
And then, if we want the centre, we can just add half the width of the rectangle to move across, and

00:53.880 --> 00:56.400
then we move down by half the height of the rectangle.

00:58.690 --> 01:02.410
These functions are going to be useful for all the sprites, not just the bull.

01:03.010 --> 01:07.480
SFML has some functions for detecting collisions which use the bounding rectangle.

01:07.750 --> 01:09.490
So that is why we are interested in it.

01:10.150 --> 01:13.420
And we are going to use that with bricks, which are not a moving entity.

01:13.870 --> 01:16.750
So we are going to add this to the basic entity class.

01:18.900 --> 01:24.330
So here is our revised entity class. We have added these member functions.

01:25.970 --> 01:29.100
We are going to make these noexcept and const, for efficiency.

01:29.490 --> 01:32.100
I hope you are noticing these C++11 features!

01:32.460 --> 01:38.310
So noexcept for functions which cannot throw, constexpr for expressions which are constant

01:38.310 --> 01:38.940
expressions.

01:39.660 --> 01:44.430
And when we override a virtual function, we use the "override" keyword.

01:47.480 --> 01:50.210
get_bounding_box() will return the bounding rectangle.

01:50.600 --> 01:57.380
This will be an object of type FloatRect. get_centre() will return the centre of the sprite, as

01:57.380 --> 02:02.900
a Vector2f, which we have met before, and then we have functions for getting the x and y coordinates.

02:05.130 --> 02:09.240
I have added a source file for implementing these member functions.

02:10.050 --> 02:12.360
get_bounding_box() will call the member

02:12.360 --> 02:15.300
function of the sprite, get_global_bounds().

02:15.750 --> 02:22.170
This will return the bounding rectangle in global coordinates, which means the coordinates relative

02:22.230 --> 02:24.030
to the top left hand corner of the window.

02:24.660 --> 02:27.030
So that is the same coordinates as the display.

02:29.190 --> 02:30.030
get_centre().

02:30.030 --> 02:38.490
This will return in the coordinates of the sprite. So the center of the sprite will be half the bounding

02:38.490 --> 02:44.790
box width from the top left hand corner, and half the box height below the top left hand corner.

02:48.620 --> 02:53.930
And then, to get to the position, we just call get_position() on the sprite. That returns a Vector2f,

02:54.290 --> 02:58.520
and then we use the 'x' and 'y' members for the coordinates of the position.

03:02.120 --> 03:06.230
In the ball class, we need to modify the update() function.

03:06.740 --> 03:10.730
So if the ball is moving off the screen, we need to make sure it moves back onto the screen.

03:12.170 --> 03:18.110
If it is moving off the screen to the left, x is less than zero. Then that means the velocity is negative.

03:18.110 --> 03:21.080
It is moving to the left and we want it to move to the right.

03:21.380 --> 03:23.600
So we need to make the velocity positive.

03:24.140 --> 03:26.300
So we just change the sign of the velocity.

03:27.980 --> 03:32.720
If it is moving off the right hand side of the screen, x is greater than the width of the window.

03:33.320 --> 03:38.420
Then we want to make it move to the left, so we make the velocity negative.

03:40.280 --> 03:44.570
And similarly for the top and bottom of the screen. At the top y equals zero.

03:44.960 --> 03:48.320
So we invert the velocity to make it move down, instead of up.

03:49.070 --> 03:53.930
And when y is equal to the window height at the bottom of the screen, then we change the sign again.

03:54.200 --> 03:56.330
So it is now moving up instead of down.

03:57.590 --> 03:59.900
And we do not need to change anything else in the code.

04:00.140 --> 04:04.040
We do not need to change the main() function, because this is all done in the ball class.

04:07.020 --> 04:11.070
And then when we run it, you can see the ball is now bouncing off the edge of the screen.

04:11.580 --> 04:12.330
That is much better.

04:13.230 --> 04:14.490
Okay, that is it for this video.

04:14.940 --> 04:15.780
I will see you next time.

04:15.780 --> 04:18.090
But until then, keep coding!
