Page 2 of 2

Re: i made this thing

Posted: Wed Nov 23, 2016 11:46 am
by childonline
pgimeno wrote: childonline, I'm curious about what tool you used to save this png.
it was either IrfanView or Paint.NET, both have the option to save an uncompressed png
ivan wrote: - Don't use a huge bitmap when the background is an array of dots
- The current way you're using 'setmetatable' doesn't look correct -
sometimes you're using metabales and sometimes you're passing the 'self' reference manually.
Generally you need 1 table for the interface, one for the metatable and one for each instance,
You don't need metatables unless there is inheritance involved
- Don't clamp vectors along the x/y axis separately, clamp them based on length
- math.atan2 accepts Y as the first parameter
- use locals for temporary variables (ex: "vehicle.lua:232") - "strict.lua" can help you pinpoint these
- I didn't know the demo was interactive without looking at the code,
Either way you want to use abstraction for your 'vehicle' class so that it's not dependent on love2d's mouse
great feedback, thanks! i will definitely start using strict.lua more often. Could you please be more specific regarding what i'm doing wrong with the metatables/self-references (examples of best practices /bad practices would be most helpful)
ivan wrote: - mention Reynolds or Buckland in the comments
to be fair i wrote this demo after reading this excellent post: http://natureofcode.com/book/chapter-6- ... us-agents/, i assume the article is strongly based on Reynolds/Bucklands original code
Sir_Silver wrote: Very cool, it would be nice though if you talked a little bit about exactly whats goin' on here :)
this url will provide all the answers: http://natureofcode.com/book/chapter-6- ... us-agents/ :)

Re: i made this thing

Posted: Wed Nov 23, 2016 2:46 pm
by ivan
pgimeno wrote:Sometimes it may be convenient to swap them. In particular, when you use an angle convention where north is 0° and angle increases clockwise
Sure you can do 90 degree rotation or mirror the vector by flipping the x/y values. Note that in this case we're talking about game logic/math - so I would advise with sticking with how atan2 is "supposed" to be used... the reason behind it is that we don't want the game logic dependent on Love2D's rendering projection.
childonline wrote:great feedback, thanks! i will definitely start using strict.lua more often. Could you please be more specific regarding what i'm doing wrong with the metatables/self-references (examples of best practices /bad practices would be most helpful)
Thanks, I think that you don't need metatables at all.
For example, vrld's Vec light gets the job done without metatables:
http://hump.readthedocs.io/en/latest/vector-light.html
Metatables are only really necessary if you want to have inheritance or you're doing something complicated.
I like to plug my own OO wrapper, as it shows verbosely how inheritance works using metatables:

Code: Select all

local oo = {}

local meta = {}
local rmeta = {}

-- Creates a new class
function oo.class()
  local c = {}
  local mt = { __index = c }
  meta[c] = mt
  rmeta[mt] = c
  return c
end

-- Creates a subclass
function oo.subclass(p)
  assert(meta[p], "undefined parent class")
  local c = oo.class()
  return setmetatable(c, meta[p])
end

-- Creates a new instance
function oo.instance(c)
  assert(meta[c], "undefined class")
  return setmetatable({}, meta[c])
end

-- Gets the class of an instance
function oo.getclass(i)
  local mt = getmetatable(i)
  return rmeta[mt]
end
Example:

Code: Select all

-- Inheritance:

local oo = require 'oo'

local Fruit = oo.class()
Fruit.sweetness_threshold = 5 -- sort of like "static"
function Fruit:initialize(sweetness)
  self.sweetness = sweetness
end
function Fruit:isSweet()
  return self.sweetness > Fruit.sweetness_threshold
end

local Lemon = oo.subclass(Fruit) -- subclassing
function Lemon:initialize()
  Fruit.initialize(self, 1) -- manually invoking the superclass' initializer
end

local lemon = oo.instance(Lemon)
lemon:initialize()
print(lemon:isSweet()) -- false
Again, it's important to emphasize that there is no "standard" way of OO programming in Lua.
There are more sophisticated OO libraries out there with "mixin" support, yet 99% of the time you shouldn't need a library at all.
to be fair i wrote this demo after reading this excellent post: http://natureofcode.com/book/chapter-6- ... us-agents/, i assume the article is strongly based on Reynolds/Bucklands original code
Shiffman strikes again!
At least he mentioned Reynolds in that chapter.