i need help making a trail for the ball in my game of pong

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
solidyoshi
Prole
Posts: 1
Joined: Thu Jun 04, 2020 3:07 pm

i need help making a trail for the ball in my game of pong

Post by solidyoshi »

hey guys im extremely new to love and lua. I just finished making my game of pong and even developed an AI for one player mode and now want to work on getting a trail for the ball. I'm completely lost right now as i can't find something similar to this anywhere on this forum or on the love2d subreddit. Any help would be much appreciated.
nameless tee
Prole
Posts: 15
Joined: Mon Apr 22, 2019 8:16 am

Re: i need help making a trail for the ball in my game of pong

Post by nameless tee »

You mean you'd like the ball to leave a trail on the screen?

Assuming that the ball is white and the background is black, one simple way of doing that would be to override love.run with a modified version. Instead of calling love.graphics.clear(...) to clear the screen on each frame, you can temporarily set the blend mode (love.graphics.setBlendMode(...)) to "subtract" and use love.graphics.rectangle(...) to subtract a small value (at least 1/255) from the image. You can also play around with the blend mode "multiply" but that might result in problems because of the low precision of the colour values.

The trail you'd get would decay linearly and vary in length depending on the frame rate. There are more complicated ways to draw a trail that could give you more control, but maybe this is good enough.
User avatar
pgimeno
Party member
Posts: 3656
Joined: Sun Oct 18, 2015 2:58 pm

Re: i need help making a trail for the ball in my game of pong

Post by pgimeno »

I recommend against overriding love.run and also against using the draw buffer for doing this kind of operation. Canvases can be used for this purpose instead.
User avatar
ThatBonk
Prole
Posts: 11
Joined: Thu May 23, 2019 6:11 pm

Re: i need help making a trail for the ball in my game of pong

Post by ThatBonk »

You can also add 3 more balls behind the original one that move with it. You can simply make the ones following more transparent than the original one to make them look like a trail. You can also make the trail longer, the higher speed the actual ball reaches.
User avatar
inJuly
Prole
Posts: 29
Joined: Fri May 01, 2020 9:02 pm
Contact:

Re: i need help making a trail for the ball in my game of pong

Post by inJuly »

Hi, making a trail is very simple.
so im guessing you may have a ball object (table) and you call update to update it's position etc every frame. All you have to do is every `n` seconds (n can be any number), create a circle at the ball's position, and every frame it's size decreases a bit. once the size of the circle is less than a certain value, it is freed from memory. Here is a similar trail I made that follows the mouse:

Code: Select all

lg = love.graphics
lm = love.mouse

-- small display circle class
local PARTICLE_SIZE = 30
local PARTICLE_SIZE_MIN
local Particle = {}

function Particle:new(x, y)
	local p = {}
	self.__index = self
	p.x = x
	p.y = y
	p.size = PARTICLE_SIZE
	p.alpha = 1
	return setmetatable(p, Particle)
end

function Particle:update()
	self.size = self.size - 1
	self.alpha = self.alpha - 0.05
end

function Particle:draw()
	lg.setColor(1, 1, 1, self.alpha)
	lg.circle('fill', self.x, self.y, self.size)
end

-- trailing particle effect on mouse cursor

local particle_array = {}

function love.load()

end


function love.draw()
	for i = 1, #particle_array do
		particle_array[i]:draw()
	end
end

function love.update(dt)
	table.insert(particle_array, Particle:new(lm.getX(), lm.getY()))
	for i = #particle_array, 1, -1 do
		particle_array[i]:update()
		if particle_array[i].size <= 0 then
			table.remove(particle_array, i)
		end
	end
end
Here is what it looks like:
trail2.gif
trail2.gif (684.21 KiB) Viewed 4492 times
trail2.gif
trail2.gif (684.21 KiB) Viewed 4492 times
Attachments
trail.gif
trail.gif (953.74 KiB) Viewed 4492 times
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 6 guests