3 – THE BALL
CLASS INTRO In this section, we will code in the Ball
class. DATA FIELDS As always, we start by deciding on the data
fields. The ball will have x and y coordinates. It will have a width and height. The ball will also have a velocityX and velocityY datafield that will store the ball’s speed in both the
horizontal and vertical directions. Let’s look at how velocityX
and velocityY will work. Each time the render method is called, the
puck will be moved in the x direction by velocityX
and in the y direction by velocityY. So the data fields look like this: private float x; private float y; private float velocityX; private float velocityY; private float width; private float height; CONSTRUCTOR For now, we’ll make a constructor that
gets an argument for each datafield. public Ball(float x, float y, float vx, float vy, float w, float h) { this.x = x; this.y = y; this.velocityX
= vx; this.velocityY
= vy; this.width = w; this.height
= h; } GET
METHODS We need to create get methods for each
of the datafields above. You
need to create these 6 get methods on your own. Code will not be provided for this. EXTRA
GET METHODS We will have to check if the ball
collides with the paddles. This will
involve checking if the ball is either below, above, to the left or to the
right of the paddle. To help us do
this, we will include the following methods: getBottom() – returns y getTop() – returns y + height getLeft() – returns x getRight() – returns x + width You
need to implement the four above methods in the Ball class. EXTRA
GET METHODS FOR PADDLE The getBottom(), getTop(), getLeft() and getRight() methods are also useful for the Paddle
class. They will actually be identical
in the Paddle class as they are in the Ball class. Implement
the 4 methods in the Paddle class. COLLISIONS We need to be able to check if the
ball is colliding with a paddle. To do
this, we will make a method called hitting
that will get a Paddle object as argument and returns true is the ball is
colliding with the paddle. Otherwise,
it will return false. At first, this may seem quite
complicated to do. But there is a
fairly simple solution. Here is pseudo
code explaining our strategy. If ball is below paddle
Return false If ball is above paddle
Return false If ball is to the left of paddle
Return ralse If ball is to the right of paddle
Return false Otherwise (the only option left is
that the ball is colliding with the paddle)
Return true The pseudo code above looks like this
in Java: public boolean hitting(Paddle p) { if (getTop() < p.getBottom()) //if ball
below p { return false; } else if (getBottom() > p.getTop()) //if ball above p { return false; } else if (getLeft() > p.getRight()) //if ball
right of p { return false; } else if (getRight() < p.getLeft()) //if ball is left of p { return false; } else //then it
is hitting { return true; } } UPDATING
THE BALL’S LOCATION For the paddles, we updated their
location inside the PongGame class. However, there is a fair bit to do to
update the ball’s location. We need to
move the ball to its new location, then check if it needs to bounce off the
top or bottom, then check if it needs to bounce off either paddle. Because there is so much to do, we
will create a method called update that can be called. The method consists of three sections. First, it updates the values of x and
y. Then it checks if the ball is bouncing
off the top or bottom of the room. If
it is, it makes the required changes to velocity and the y coordinate. Then, it check if the ball is
colliding with the paddles. If it is,
it makes the required changes to velocity and the x coordinate. public void
update(Paddle p1, Paddle p2) { x = x + velocityX; y = y + velocityY; //BOUNCE OFF TOP? float roomTop = Gdx.graphics.getHeight(); if(getTop() > roomTop) //if
hitting top { //bounce velocityY = -velocityY; y = roomTop - (getTop() - roomTop) - height; } //BOUNCE OFF BOTTOM? if(getBottom() < 0) { velocityY = -velocityY; y = 0 + (0 - getBottom()); } //HITTING P1? if
(hitting(p1)) { velocityX = -velocityX; x =
p1.getRight() + (p1.getRight() - x); } //HITTING P2? if
(hitting(p2)) { velocityX = -velocityX; x =
p2.getLeft() - (getRight() - p2.getLeft()) - width; } } |
|
|