Page 1 of 3

2D-Vector class library

Posted: Mon Apr 19, 2010 4:48 pm
by blackops7799
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.

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 )
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.

Re: 2D-Vector class library

Posted: Mon Apr 19, 2010 7:46 pm
by nevon
I can tell you're new here. Libraries and other useful snippets of code are required to have a NSFW name.

Re: 2D-Vector class library

Posted: Mon Apr 19, 2010 10:29 pm
by blackops7799
That's pretty stupid. Shows the maturity of this place.

Re: 2D-Vector class library

Posted: Mon Apr 19, 2010 10:59 pm
by Umbrageous
Nah, I see it as kind of fun, not everything has to be serious you know, The best part is the nsfw name lol.
But This is really cool, and pretty useful too

Re: 2D-Vector class library

Posted: Mon Apr 19, 2010 11:42 pm
by bmelts
I think nevon may have been being a tad facetious :ehem:

Re: 2D-Vector class library

Posted: Tue Apr 20, 2010 4:11 am
by Jasoco
As hard as I think, I can't come up with a clever NSFW name for this library. Anyone else?

Also, is MiddleClass and MindState the exceptions to this silly rule? ;)

Re: 2D-Vector class library

Posted: Tue Apr 20, 2010 4:43 am
by bmelts
Vectors Are Great

Re: 2D-Vector class library

Posted: Tue Apr 20, 2010 5:57 am
by bartbes
blackops7799 wrote:That's pretty stupid. Shows the maturity of this place.
Maturity is overrated.

Re: 2D-Vector class library

Posted: Tue Apr 20, 2010 7:12 am
by nevon
blackops7799 wrote:That's pretty stupid. Shows the maturity of this place.
I know it can be kind of hard to tell what's serious or not on the internet, but geez!

At least Anjo got it. :ehem:

Re: 2D-Vector class library

Posted: Tue Apr 20, 2010 7:55 am
by kikito
Jasoco wrote:As hard as I think, I can't come up with a clever NSFW name for this library. Anyone else?

Also, is MiddleClass and MindState the exceptions to this silly rule? ;)
Well, PÄSSION is also quite SFW, IMHO. OMG BBQ.