2D-Vector class library
Posted: Mon Apr 19, 2010 4:48 pm
I was making a physics based pong game so I came up with this class library.
It allows you to use vectors like so in the code below.
It just makes things a lot simpler, organized, and easy to read. Sorry if there is already something like this here, but I searched and didn't find anything.
Enjoy!
Demo:
http://www.blackopsservers.com/blackops ... _demo.love
Demo shows a ball continuously bouncing around on your screen.
Press space to reset the ball and randomly punt it again.
It allows you to use vectors like so in the code below.
Code: Select all
--I used this within a collision callback for my pong game
local velx, vely = coll:getVelocity()
local IncidentVec = Vector.new( velx, vely )
local speed = IncidentVec:Length()
local colx, coly = coll:getNormal()
local Normal = Vector.new( colx, coly )
Normal:Normalize()
local DotProduct = Normal:Dot( IncidentVec * -1 ) --reflect the bounce
local Dir = ( 2 * Normal * DotProduct ) + IncidentVec
Dir:Normalize()
someBodyObject:setLinearVelocity( Dir.x * speed, Dir.y * speed )
Enjoy!
Demo:
http://www.blackopsservers.com/blackops ... _demo.love
Demo shows a ball continuously bouncing around on your screen.
Press space to reset the ball and randomly punt it again.