2D-Vector class library

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: 2D-Vector class library

Post by Robin »

Fling/Flirt are also SFW LÖVE references, but at least I'm not mentioning them.

Wait...
Help us help you: attach a .love.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: 2D-Vector class library

Post by mikembley »

Hey blackops thanks for posting this lib

Although im a little lacking in the mathematical department, Im keen to learn about 2d vectors - but most write ups about vectors just confuse me further, either due to the formulas written (the crazy symbols) or the code is in c/c++.
I have used a few flash tutorials on vectors, and managed to get the same results as the flash version of the tutorials, yet i havent been able to relate to vectors and how they work.

Im not asking or wanting you to explain it in lamens terms, but rather asking if you would kindly post your pong game or quickly knock up a small simple example so i can see how reflection in vectors work.

I noticed you have come from the FP community? Which would explain your LUA knowledge?

Anywho, Welcome to LÖVE :)
blackops7799
Prole
Posts: 13
Joined: Mon Apr 19, 2010 4:30 pm

Re: 2D-Vector class library

Post by blackops7799 »

No problem. Here's a little demo I made.

http://www.blackopsservers.com/blackops ... _demo.love

That's pretty much all I had to do to make the pong ball in my game I'm working on. To reset the ball just press space.

And to answer your question, yes I came from FP and I have been messing around with lua for almost 2-3 years now.

I just noticed an error with one of the methods, the Vector.Distance one if anyone cares. I updated the download in the OP.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: 2D-Vector class library

Post by mikembley »

Thank you for posting the example, Although it wasn't really what i was looking for.

I was just trying to work out vectors, but i think your lib helped me on my way though, as most of my tests and experiments work flawlessly, besides normals.

Which brings me to a function

Code: Select all

function Vector.GetNormal( vec ) --Duplicate of GetNormalized
	local len = vec:Length()
	return Vector.new( vec.x/len, vec.y/len )
end
Correct me if i am wrong, But normalization is not the same as a normal, So i was just wondering why this function would copy the other? Could you expand on this, Was this function overlooked and not corrected? Im only asking to see if my assumptions were correct because this has thrown me a little since most of the pseudo code and other tutorials that i am using require me to use a normal

Thank you for your time :)
blackops7799
Prole
Posts: 13
Joined: Mon Apr 19, 2010 4:30 pm

Re: 2D-Vector class library

Post by blackops7799 »

It's possible that there is a difference. I made this lib off of previous knowlege, so it's possible I overlooked something. I based my lib off of this http://wiki.garrysmod.com/?title=Vector too and according to that, they are the same. There's nothing else I could think of that it would be, but if you can support your argument I would be happy to fix it. Also if anyone needs any of those methods on that page I would be happy to make them.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: 2D-Vector class library

Post by mikembley »

Scroll to the bottom of this page

http://www.metanetsoftware.com/technique/tutorialA.html

to the perp product part
Another useful concept in 2D geometry is the "normal"...not to be confused with normalization!
Like i mentioned previously math isnt my most favourite subject, and from the material ive read thus far about normals, they all seem to say the same the same thing about that tutorial i have linked

But what do i know :( maybe it all falls down terminology and how others portray functions and naming conventions, i really dont know
blackops7799
Prole
Posts: 13
Joined: Mon Apr 19, 2010 4:30 pm

Re: 2D-Vector class library

Post by blackops7799 »

Oh okay, that makes sense. Pretty much that site is saying that a "normal" is a vector perpendicular to it while normalization is getting the vector length in a unit of 1. It's just a mix up of terms on my/garrysmods fault. Not so much mine as I was copying their function names. :nyu:

I can add some functions to get the left or right normal of a vector if that helps.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: 2D-Vector class library

Post by mikembley »

Code: Select all

function Vector.Normal( vec1, vec2 )
	local vec = vec2 - vec1
	return Vector.new( -vec.y, vec.x )
end
I just added my own in, but it would be nice to see yours edited

I dont think you need to put in both left and right, you can just negate the vector and its instantly the opposite, but thats entirely up to you considering it is your lib :)
blackops7799
Prole
Posts: 13
Joined: Mon Apr 19, 2010 4:30 pm

Re: 2D-Vector class library

Post by blackops7799 »

Yeah, I know. Vector:GetLeft() is the same as Vector:GetRight() * -1, but I always found it annoying that libs did that. It just feels cleaner for me to use Vector:GetLeft(). :rofl:

Anyway I will update this lib again sometime today to add some more things.
  • Adding..
  • Vector.DotProduct --Duplicate of Dot
  • Vector.Zero --"nullifies a vector"
  • Vector.Rotate --rotate a vector by degrees
  • Vector.RotateRad --rotate a vector by radians
  • Vector.GetRight --get the rightward perpendicular vector to vector supplied
  • Vector.GetLeft --get the leftward perpendicular vector to vector supplied
  • Vector.Project --Projects a vector onto another vector, contribution from mikembley
I also removed pointless zeros that were being used as the z coords.
Last edited by blackops7799 on Wed Apr 21, 2010 7:27 pm, edited 1 time in total.
User avatar
mikembley
Citizen
Posts: 72
Joined: Wed Dec 17, 2008 5:30 pm
Location: Blackburn, UK
Contact:

Re: 2D-Vector class library

Post by mikembley »

Code: Select all

function Vector.Project( vec1, vec2 )
	local proj = Vector.new(0, 0)
	local dot = Vector.Dot(vec1, vec2)
	proj.x = ( dot / (vec2.x*vec2.x + vec2.y*vec2.y) ) * vec2.x
	proj.y = ( dot / (vec2.x*vec2.x + vec2.y*vec2.y) ) * vec2.y
	
	return Vector.new(proj.x, proj.y)
end
Projecting a vector onto another vector
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 2 guests