How to make curvy lasers

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

How to make curvy lasers

Post by Gunroar:Cannon() »

I researched a bit and I found out how to do normal lasers. :awesome:

Now I was wondering about
...
...
this.

Image

It curves nice and naturally there but what I really want is this possibility:

Image
(It twists and turns, couldn't find the gif)
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
marclurr
Party member
Posts: 158
Joined: Fri Apr 22, 2022 9:25 am

Re: How to make curvy lasers

Post by marclurr »

Looks like a job for splines/bezier curves. Not something I've dabbled with myself but I'm pretty sure they would help give this effect. A spline will just be something similar to a "path" though so to make the laser "thick" you might have to use it to to generate or deform a mesh.

I haven't watched it but I remember coming across a video a few years back by a well known solitary programmer which went into some detail about splines https://www.youtube.com/watch?v=9_aJGUTePYo
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make curvy lasers

Post by zorg »

You could fake it with multiple overlapping segments made up of small sprites (circular bullets for example), probably less computationally expensive in most cases, compared to evaluating bezier curves i believe.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to make curvy lasers

Post by darkfrei »

Maybe the same as lightning, just make the line not so straight.
viewtopic.php?p=249816#p249816

But add more points and smooth thre result line as
t2 = 0.25*t1+0.5*t2+0.25*t3 where t2 is actual point, 1 and 3 are previous and next point in line.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

Re: How to make curvy lasers

Post by Gunroar:Cannon() »

You're all so helpful ^^

I didn't know ....heheh... about Bezier curves until know and I was thinking that would be my solution but then it takes more CPU to call the evaluate function?

Don't really understand the "fake it" method so for now I'll try the lightning method and post how it goes, else I might just use the curves.
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
milon
Party member
Posts: 472
Joined: Thu Jan 18, 2018 9:14 pm

Re: How to make curvy lasers

Post by milon »

zorg wrote: Fri Nov 04, 2022 7:40 pm You could fake it with multiple overlapping segments made up of small sprites (circular bullets for example), probably less computationally expensive in most cases, compared to evaluating bezier curves i believe.
Gunroar:Cannon() wrote: Sat Nov 05, 2022 9:22 am Don't really understand the "fake it" method so for now I'll try the lightning method and post how it goes, else I might just use the curves.
It's kind of like this:
Image
The closer together you draw each circle, the more it looks like a curvy line. If you put them so close they overlap enough, you end up with a solid curvy line. If you use a sprite instead of a circle, you get a curvy sprite line thing. In your case, a curvy laser. :)
Any code samples/ideas by me should be considered Public Domain (no attribution needed) license unless otherwise stated.
User avatar
Gunroar:Cannon()
Party member
Posts: 1143
Joined: Thu Dec 10, 2020 1:57 am

Re: How to make curvy lasers

Post by Gunroar:Cannon() »

Oh...wow. I see. Just like how trails are drawn. Though won't I still get the path of where to draw these circles ... from a Bezier Curve? :rofl:
The risk I took was calculated,
but man, am I bad at math.

-How to be saved and born again :huh:
User avatar
zorg
Party member
Posts: 3468
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: How to make curvy lasers

Post by zorg »

you don't need a bezier if you just focus on the "first" sprite, since it has position and velocity, you can just duplicate that, and draw the duplicate in the old positions before you update the current sprite's position.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
darkfrei
Party member
Posts: 1204
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to make curvy lasers

Post by darkfrei »

Based on the lightning

Code: Select all

local function newLaser (x1,y1, x2, y2, sd)
	-- d is amount of subdivisions, for example 3
	sd = sd or 3
	
	local p1 = {x=x1, y=y1}
	local p2 = {x=x2, y=y2}
	local points = {p1, p2}
	for i = 1, sd do
		local n = (#points-1)
		local temp = {unpack (points)}
--		print ('i'..i..' n'..n)
		for j = 1, n do
			
			local point = getPointBetween (points[j], points[j+1])
			table.insert (temp, j*2,point)
--			print ('inserted', #points, j*2)
		end
		points = temp
		-- make the line smooth
		if i > sd-4 then
			local s = 0.4
			for i=2, #points-1 do
				local p1 = points[i-1]
				local p2 = points[i]
				local p3 = points[i+1]
				p2.x = 0.5*s*p1.x+(1-s)*p2.x+0.5*s*p3.x
				p2.y = 0.5*s*p1.y+(1-s)*p2.y+0.5*s*p3.y
			end
			for i=#points-1, 2, -2 do
				local p1 = points[i-1]
				local p2 = points[i]
				local p3 = points[i+1]
				p2.x = 0.5*s*p1.x+(1-s)*p2.x+0.5*s*p3.x
				p2.y = 0.5*s*p1.y+(1-s)*p2.y+0.5*s*p3.y
			end
		end
	end
--	print ('#points', #points)
	local line = {}
	

	
	for i, point in ipairs (points) do
--		print (point.x, point.y)
		table.insert (line, point.x)
		table.insert (line, point.y)
	end
	local laser = {points=points, line=line, sd = sd}
	return laser
end
2022-11-10T16_45_37-Untitled.png
2022-11-10T16_45_37-Untitled.png (18.52 KiB) Viewed 4697 times
:awesome: :awesome: Explanation:
1) You have one line AB.
2) Add the point C between them, so you have ACB. The point C is in the middle, but with small movement to the normal of line AB.
3) Add points D and E between A and C; C and B, so you have ADCEB.
4) I don't want to use the alphabet, so add points t1, t2, t3, t4 between last letters: A-t1-D-t2-C-t3-E-t4-B
5) repeat 4) until you want and make inserted points smooth to neighbour points.
Attachments
curvy-lasers-01.love
(1.14 KiB) Downloaded 120 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 0 guests