HUMP - yet another set of helpers

Showcase your libraries, tools and other projects that help your fellow love users.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: HUMP - yet another set of helpers

Post by bartbes » Fri Oct 17, 2014 2:53 pm

Your problem is the combination of this line:

Code: Select all

Actor = require 'Actor'
and that your Actor file does not return the Actor class. Require defaults to 'true' if nothing was explicitly returned, so that is the boolean from your error message. (By the way, you're setting a global here, named Actor, so I'm pretty sure you just nuked that class..)

That in turn means that here

Code: Select all

Player = Class{
__includes = Actor,
you try to inherit from the boolean 'true', which.. well, doesn't work.

Oh, and I only just saw your edit, damn you! :(

Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: HUMP - yet another set of helpers

Post by Ford_Prefect » Thu Jan 08, 2015 6:09 pm

[edit]

I screwed up my coordinate systems and should be ashamed of myself. Nothing to see here, move along.

User avatar
Jeeper
Party member
Posts: 587
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: HUMP - yet another set of helpers

Post by Jeeper » Thu Jan 08, 2015 6:55 pm

Ford_Prefect wrote:[edit]

I screwed up my coordinate systems and should be ashamed of myself. Nothing to see here, move along.
Don't worry, sometimes explaining a problem to someone else is the best way to solve it. As you then think of all the things that in your mind are obvious as you are explaining them.

User avatar
kikito
Inner party member
Posts: 3153
Joined: Sat Oct 03, 2009 5:22 pm
Location: Madrid, Spain
Contact:

Re: HUMP - yet another set of helpers

Post by kikito » Thu Jan 08, 2015 7:00 pm

Yep. It is called Rubber Duck Debugging.
When I write def I mean function.

User avatar
Jeeper
Party member
Posts: 587
Joined: Tue Mar 12, 2013 7:11 pm
Contact:

Re: HUMP - yet another set of helpers

Post by Jeeper » Thu Jan 08, 2015 7:03 pm

kikito wrote:Yep. It is called Rubber Duck Debugging.
Hah, you learn something new every day :)

Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: HUMP - yet another set of helpers

Post by Ford_Prefect » Thu Jan 08, 2015 10:11 pm

That's great and all, but unfortunately my code still doesn't work the way I want it to :D

I like that duck though.

User avatar
ivan
Party member
Posts: 1703
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: HUMP - yet another set of helpers

Post by ivan » Fri Jan 09, 2015 3:38 pm

Hey Vrld, this is a pretty cool lib. :)
Just have a small suggestion about the following piece of code:

Code: Select all

-- ref.: http://blog.signalsondisplay.com/?p=336
function vector:trim_inplace(maxLen)
	local s = maxLen * maxLen / self:len2()
	s = (s > 1 and 1) or math.sqrt(s)
	self.x, self.y = self.x * s, self.y * s
	return self
end
self:len2() could return 0 here so I would suggest an "if" check or perhaps the approach that I use:

Code: Select all

-- trims vector size if above a given length
function vector:trim_inplace(maxLen)
  assert(maxLen >= 0)
  local d = self:len()
  if d > maxLen then
    local s = 1/d * maxLen -- 1/d = inverse length, could be simplified to: maxLen/d
    self.x, self.y = self.x * s, self.y * s
  end
  return self
end
1/d is basically the inverse length.
I believe the code above should work as long as "maxLen" is >= 0.
If you want to support negative magnitudes then it becomes:

Code: Select all

function vector:trim_inplace(maxLen)
  local d = self:len()
  if d > maxLen and d > 0 then
    local s = maxLen/d
    self.x, self.y = self.x * s, self.y * s
  end
  return self
end
Same as above, but with "len2" instead of "len":

Code: Select all

function vector:trim_inplace(maxLen)
  local d2 = self:len2()
  if d2 > maxLen*maxLen and d2 > 0 then
    local s = maxLen/math.sqrt(d2)
    self.x, self.y = self.x * s, self.y * s
  end
  return self
end
Great work on the lib, just wanted to mention this since HugoBDesigner had a similar issue.

PS. Also, I have another small suggestion :)

Code: Select all

function vector:perpendicular()
  return new(-self.y, self.x)
end
could become:

Code: Select all

function vector:rotate90ccw()
  return new(-self.y, self.x)
end
function vector:rotate90cw()
  return new(self.y, -self.x)
end
function vector:rotate180()
  return new(-self.x, -self.y)
end
Last edited by ivan on Fri Jan 09, 2015 4:45 pm, edited 3 times in total.

Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: HUMP - yet another set of helpers

Post by Ford_Prefect » Fri Jan 09, 2015 3:57 pm

ivan wrote: PS. Also, I have another small suggestion :)

Code: Select all

function vector:perpendicular()
  return new(-self.y, self.x)
end
could become:

Code: Select all

function vector:rotate90ccw()
  return new(-self.y, self.x)
end
function vector:rotate90cw()
  return new(self.y, -self.x)
end
function vector:rotate180()
  return new(-self.x, -self.y)
end
I think this would be great. I found :perpendicular() confusing at first, too.
Also, I think HUMP rotates in the other direction than löve. (HUMP goes counterclockwise for positive values, löve clockwise.)


On a different note, I figured out what caused problems in my code. Take a look at the attached .love file. It's calculating the angle between a rotating vector and a vertical one.
See that jump from 1/2 pi to 3/2 pi? That's a frustration feature...

edit: For now I created my own :angleTo() using the scalar product which does everything I need:

Code: Select all

function vector:angleTo(input)
	return math.acos( (self.x * input.x + self.y * input.y) / (math.sqrt((self.x^2 + self.y^2)) * math.sqrt((input.x^2 + input.y^2))))
end
Attachments
vec_test.love
(123.05 KiB) Downloaded 131 times
Last edited by Ford_Prefect on Fri Jan 09, 2015 4:21 pm, edited 1 time in total.

User avatar
ivan
Party member
Posts: 1703
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: HUMP - yet another set of helpers

Post by ivan » Fri Jan 09, 2015 4:21 pm

Ford_Prefect wrote:I think this would be great. I found :perpendicular() confusing at first, too.
Also, I think HUMP rotates in the other direction than löve. (HUMP goes counterclockwise for positive values, löve clockwise.)
With trigonometry:
0 degrees (0 radians) is East
90 degrees (pi/2) is North
180 degrees (pi) is West
270 degrees (3*pi/2) is South.
On a different note, I figured out what caused problems in my code. Take a look at the attached .love file. It's calculating the angle between a rotating vector and a vertical one.
See that jump from 1/2 pi to 3/2 pi? That's a frustration feature...
The angle between two vectors (R) is a classic problem, could be solved using the dot product instead of atan2:

Code: Select all

-- for two normalized vectors:
cos(R) = dot(a,b)
R = acos(dot(a,b))

-- for any two vectors:
cos(R) = dot(a,b)/(length(a)*length(b))
R = acos(dot(a,b)/(length(a)*length(b)))
Last edited by ivan on Fri Jan 09, 2015 4:24 pm, edited 1 time in total.

Ford_Prefect
Prole
Posts: 31
Joined: Sun Dec 30, 2012 7:14 pm

Re: HUMP - yet another set of helpers

Post by Ford_Prefect » Fri Jan 09, 2015 4:23 pm

ivan wrote:[...]
See my edit above, exactly what I did :D

(You beat me by a few seconds)

edit:
The best thing is, my code works now. :)

Post Reply

Who is online

Users browsing this forum: No registered users and 33 guests