Page 1 of 1
i need help making a trail for the ball in my game of pong
Posted: Thu Jun 04, 2020 3:14 pm
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.
Re: i need help making a trail for the ball in my game of pong
Posted: Fri Jun 05, 2020 1:57 pm
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.
Re: i need help making a trail for the ball in my game of pong
Posted: Fri Jun 05, 2020 3:55 pm
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.
Re: i need help making a trail for the ball in my game of pong
Posted: Sun Jun 07, 2020 10:27 am
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.
Re: i need help making a trail for the ball in my game of pong
Posted: Mon Aug 03, 2020 4:35 pm
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 (684.21 KiB) Viewed 4493 times
- trail2.gif (684.21 KiB) Viewed 4493 times