Hello again! In this video,
we are going to look at how to make our bouncing ball bounce off the edges of the window.
To do this, we need to be able to find the position of the ball.
So we are going to add a member function which will return that.
We are also going to add some member functions which will come in useful, later on. So we can find the centre
of the ball, and also the bounding rectangle.
The bounding rectangle is actually the same as the texture rectangle, but it has different significance.
With a texture rectangle.
we are interested in the image
that it is bound to. With a bounding rectangle,
we are interested in its geometry.
When we get the bounding rectangle, we will get the coordinates of the origin.
And then, if we want the centre, we can just add half the width of the rectangle to move across, and
then we move down by half the height of the rectangle.
These functions are going to be useful for all the sprites, not just the bull.
SFML has some functions for detecting collisions which use the bounding rectangle.
So that is why we are interested in it.
And we are going to use that with bricks, which are not a moving entity.
So we are going to add this to the basic entity class.
So here is our revised entity class. We have added these member functions.
We are going to make these noexcept and const, for efficiency.
I hope you are noticing these C++11 features!
So noexcept for functions which cannot throw, constexpr for expressions which are constant
expressions.
And when we override a virtual function, we use the "override" keyword.
get_bounding_box() will return the bounding rectangle.
This will be an object of type FloatRect. get_centre() will return the centre of the sprite, as
a Vector2f, which we have met before, and then we have functions for getting the x and y coordinates.
I have added a source file for implementing these member functions.
get_bounding_box() will call the member
function of the sprite, get_global_bounds().
This will return the bounding rectangle in global coordinates, which means the coordinates relative
to the top left hand corner of the window.
So that is the same coordinates as the display.
get_centre().
This will return in the coordinates of the sprite. So the center of the sprite will be half the bounding
box width from the top left hand corner, and half the box height below the top left hand corner.
And then, to get to the position, we just call get_position() on the sprite. That returns a Vector2f,
and then we use the 'x' and 'y' members for the coordinates of the position.
In the ball class, we need to modify the update() function.
So if the ball is moving off the screen, we need to make sure it moves back onto the screen.
If it is moving off the screen to the left, x is less than zero. Then that means the velocity is negative.
It is moving to the left and we want it to move to the right.
So we need to make the velocity positive.
So we just change the sign of the velocity.
If it is moving off the right hand side of the screen, x is greater than the width of the window.
Then we want to make it move to the left, so we make the velocity negative.
And similarly for the top and bottom of the screen. At the top y equals zero.
So we invert the velocity to make it move down, instead of up.
And when y is equal to the window height at the bottom of the screen, then we change the sign again.
So it is now moving up instead of down.
And we do not need to change anything else in the code.
We do not need to change the main() function, because this is all done in the ball class.
And then when we run it, you can see the ball is now bouncing off the edge of the screen.
That is much better.
Okay, that is it for this video.
I will see you next time.
But until then, keep coding!