Good utility libraries for love.physics? I'm sick of trig :p

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
Snackrilege
Prole
Posts: 19
Joined: Sat Sep 06, 2014 8:50 pm

Good utility libraries for love.physics? I'm sick of trig :p

Post by Snackrilege » Thu Sep 11, 2014 5:05 pm

So, basic things like "angleTo(fixture1, fixture2)" or "angleToXYcomponents(angle)" and "XYcomponentsToAngle(vx,vy)"

Things that dont seem to be included anywhere in love that make life easier.

I can do it myself, but I'd rather stand on the shoulders of giants, naw mean?

User avatar
Ref
Party member
Posts: 690
Joined: Wed May 02, 2012 11:05 pm

Re: Good utility libraries for love.physics? I'm sick of tri

Post by Ref » Thu Sep 11, 2014 5:55 pm

Could modified my simple module for drawing arrows.

Code: Select all

local Arrow = {}
local gr,pi,sin,cos,atan2,floor,sqrt = love.graphics,math.pi,math.sin,math.cos,math.atan2,math.floor,math.sqrt
function Arrow.angle( pt1, pt2 )
	return atan2( pt1.y-pt2.y, pt1.x-pt2.x ) - pi
	end

function Arrow.length(pt1,pt2)
	return math.sqrt((pt1.x-pt2.x)^2+(pt1.y-pt2.y)^2)
	end

function Arrow.StartEnd( pt1, pt2, cl )	-- from, to, color
	local cl	= cl or { 0, 255, 0 }
	gr.setColor( unpack( cl ) )
	Arrow.Draw( pt1, pt2 )
	local an	= floor( 180 * (atan2( pt1.y-pt2.y , pt1.x-pt2.x ) ) / pi + 270 )
	an			= an <= 360 and an or an - 360
	gr.print( an, pt1.x - 10, pt1.y + 30 )
	end

function Arrow.Direction( pt1, an, ln, cl )	-- location, direction, length, [color]
	local cl	= cl or { 0, 255, 0 }
	gr.setColor( unpack( cl ) )
	local pt2	= {x=ln * cos(an) + pt1.x,y=ln * sin(an) + pt1.y }
	Arrow.Draw( pt1, pt2, ln )
	local aa	= floor( 180 * an / pi + 450 )	-- convert angle to North = 0 degrees
	return aa   and aa or aa - 360
	end

function Arrow.Draw( pt1, pt2, ln )		-- start, end, [length]
	local x1 ,y1, x2, y2 = pt1.x, pt1.y, pt2.x, pt2.y
	local ln	= ln and ln or sqrt( (x2-x1)^2 + (y2-y1)^2 )
	local hd	= 0.2 * ln
	gr.line( x1, y1, x2, y2 )					-- arrow shaft (point1 to point2)
	local dx 	= x2 - x1						-- arrow head  (at point2)
	local dy 	= y2 - y1
	local a1	= -0.42 * pi  - atan2( dy , dx )
	local x3 	= hd * sin( a1) + x2
	local y3 	= hd * cos( a1 ) + y2
	gr.line( x2, y2, x3, y3 )
	local a2	= -0.58 * pi  - atan2( dy , dx )
	local x4 	= hd * sin( a2 ) + x2
	local y4 	= hd * cos( a2 ) + y2
	gr.line( x2, y2, x4, y4 )
	end

return Arrow

Snackrilege
Prole
Posts: 19
Joined: Sat Sep 06, 2014 8:50 pm

Re: Good utility libraries for love.physics? I'm sick of tri

Post by Snackrilege » Thu Sep 11, 2014 6:58 pm

thanks for the offer, mate!

I've implemented all the functions I mentioned up there while I was waiting, but I would still be happy to see any libraries for these kinds of common operations. I'm all about collaboration :D

User avatar
bdjnk
Citizen
Posts: 81
Joined: Wed Jul 03, 2013 11:44 pm

Re: Good utility libraries for love.physics? I'm sick of tri

Post by bdjnk » Thu Sep 11, 2014 7:06 pm

Don't know if you've seen, but the wiki has a list of libraries. Lume might be what you want.

Post Reply

Who is online

Users browsing this forum: No registered users and 39 guests