Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
Tags

How to make Ball for Pong Game.

A topic by GamerPlayz created Jan 31, 2022 Views: 422 Replies: 3
Viewing posts 1 to 4

So I have discovered this awesome game framework called Love2D that uses Lua. I am in love with it and am still very new to it. I have practiced to some extent. I am trying to create a simple Pong game in Love2D but I have no idea how do I code the ball to detect wall collision player collision and go other direction and stuff like that. Can someone please help me? Here's the project zip file in google drive.

https://drive.google.com/file/d/1UvA1q5GHJ5k2xj_87GIlTpmzR81Kzq-a/view?usp=shari...

Moderator moved this topic to General Development

use this:

love.graphics.ellipse( mode, x, y, radiusx, radiusy, segments )

this should work

OP asked about collision detection, not drawing graphics.

Not sure if you’re still looking for an answer, given how long it’s been, here’s an answer for anyone who wants it:

Q: How do I make the ball from pong in Love2D? A: It depends.

Long answer–there are two general ways to do this, either using a packaged physics engine or writing your own simple movement/collision code. Both options have upsides and downsides, and it really depends on how you want to work and what your exact end-goal is. Let’s look at both.

Using a Physics Engine Love2D comes with the love.physics module, which lets you easily set up physics objects with the Box2D physics engine. The workflow goes like this–you create a Physics World with love.physics.newWorld, then add Physics Bodies (ie, your objects) to it using love.physics.newBody. To give a body a shape, you’d add Fixtures to each body using love.physics.newFixture and providing a collision shape inside.

So, for Pong you might give the four walls of your arena and each paddle a body with a rectangular collision shape (love.physics.newRectangleShape), and the ball a body with a circular physics shape (love.physics.newCircleShape). Each body needs a “type” that determines how it works for physics, so you’d probably give the walls a static type (since they should never move), the paddles a kinematic type (since they only move according to player input), and the ball a dynamic type (since it’s a proper object that moves and bounces on its own. From there, you just draw each object in your scene where the body says it is, check if the ball has passed the paddles for scoring, and move the paddles when you get inputs.

This is a pretty simple solution, since the movement and collision is done for you. So, why write your own? In the case of pong (and other games with infinitely-bouncing balls), I’ve found that physics engines don’t handle ball movement very well. It’s very easy for certain bounces to make them lose speed or stop, meaning that you need to constantly “boost” them to make sure it doesn’t happen. Also, if your goal is to make something that feels like the original pong then you don’t want the kinds of bounces that you’ll get from a circle hitting the edge of a paddle.

So, the other option–custom code. I’m going to write this as simple as possible, so folks with physics programming experience will get a headache but you’ll have an easier time following and using this!

For pong, we’ll want to track a couple things for each moving object: position (x and y) and velocity (vx and vy). In love.update, we get a *delta time value*, dt`, which we can use to move objects without being sped up/slowed down by framerate. So here’s an example of making the ball move:

ball.x = ball.x + (ball.vx * dt)
ball.y = ball.y + (ball.vy * dt)

Simple!

To keep the ball from going out of bounds, we can check the y value and reverse our speed:

if ball.y <= 0 and ball.vy < 0 then
    ball.y = 0
    ball.vy = -ball.vy -- Multiplying the "y" movement by -1 will make the ball bounce vertically
elseif ball.y >= arena_height and ball.vy > 0 then
    ball.y = 0
    ball.vy = -ball.vy -- Multiplying the "y" movement by -1 will make the ball bounce vertically
end

To collide the ball against the paddle, we’ll use a simple AABB collision check. This is pretty simple, you just need to check if the left/right edges of the two boxes overlap, and if the top/bottom edges do as well:

function collidesWith(a, b)
    return (a.x > b.x and a.x < b.x + b.width) or (b.x > a.x and b.x < a.x + a.width) or
           (a.y > b.y and a.y < b.y + b.height) or (b.y > a.y and b.y < a.y + a.height)
end

Then, if a ball and paddle collide we can just reverse the direction of the ball:

if collidesWith(ball, leftpaddle) and ball.vx < 0 then
    ball.vx = -ball.vx -- Multiplying the "x" movement by -1 will make the ball bounce horizontally
elseif collidesWith(ball, rightpaddle) and ball.vx > 0 then
    ball.vx = -ball.vx -- Multiplying the "x" movement by -1 will make the ball bounce horizontally
end

Then, like the physics example above, you just draw your objects at their x and y positions and you’re set!

With that said, two things you’ll notice with this example is that the ball can be bounced back even when it’s behind the paddle, and the ball never changes angle. The first issue is easy, we just update the check from above to check where the ball is:

if collidesWith(ball, leftpaddle) and ball.vx < 0 and ball.x > leftpaddle.x + leftpaddle.width then -- "x" is our left side, "x + width" is our right.
    ball.vx = -ball.vx
elseif collidesWith(ball, rightpaddle) and ball.vx > 0 and rightpaddle.x > ball.x + ball.width then -- "x" is our left side, "x + width" is our right.
    ball.vx = -ball.vx
end

For the second part, most likely we want to adjust the ball’s vy, either according to the paddle’s vy or where the ball hit the paddle. I think I’m going to leave this last part as an exercise to the reader, both options are pretty simple and use the same elements that we’ve touched on above.

Hopefully that helps a bit! Remember–there are many solutions to most problems in gamedev, each with their own benefits and downsides. Even without specific knowledge of game programming, a little bit of math and creativity can go a long way!

And for anyone ready to point out more advanced flaws in the physics code above–I am aware, but please consider whether or not those details actually matter for someone making their first pong game before commenting :)