wrapper for the vector class

#32 Declined
Repository
gim913
Branch
math-vector2d
Repository
rude
Branch
default
Author
  1. Mefi Stofeles
Reviewers
Description

I'm not sure if that's a good idea, but I've created wrapper for the vector class (Vector was already done in src/common

At the bottom there is sample code using it, but there are few issues.

  • methods that don't return vector don't have get prefix (i.e. length, dot, cross), that probably doesn't conform with current love api

  • I'm not sure if doing such a wrapper is a good idea, as creating lot's of instances will probably have bad impact on memory and lua's GC

still it seems much nicer to write: foo = baz - bar instead of foo.x = baz.x - bar.x foo.y = baz.y - bar.y

local Vec2d = love.math.newVector2d
local vecA = nil
local vecB = nil
function love.load()
    vecA = Vec2d(6, 8)
    vecB = Vec2d(12, 12)
end

function love.keypressed(k)
    if k == "escape" then
        love.event.quit()
    end
end

function love.draw()
    local x1,y1 = (vecA - vecB):xy()
    local x2,y2 = (vecA + vecB):xy()
    local x3,y3 = (vecA / 3):xy()
    local x4,y4 = (vecA * 0.5):xy()
    local x5,y5 = (vecA:getNormal()):xy()
    local x6,y6 = (vecA:getNormalized()):xy()
    love.graphics.print(('sub: %d,%d'):format(x1,y1), 20, 20)
    love.graphics.print(('add: %d,%d'):format(x2,y2), 20, 40)
    love.graphics.print(('div: %d,%d'):format(x3,y3), 20, 60)
    love.graphics.print(('mul: %d,%d'):format(x4,y4), 20, 80)

    love.graphics.print(('cross: %f'):format(vecA:cross(vecB)), 20, 120)
    love.graphics.print(('dot: %f'):format(vecA:dot(vecB)), 20, 140)
    love.graphics.print(('len: %3.1f'):format(vecA:length()), 20, 160)
    love.graphics.print(('angleA: %f'):format(vecA:angle()), 20, 180)
    love.graphics.print(('angleB: %f'):format(vecB:angle()), 20, 200)
    love.graphics.print(('angleAB: %f'):format(vecA:angle(vecB)), 20, 220)

    love.graphics.print(('normal: %d,%d'):format(x5,y5), 20, 260)
    love.graphics.print(('normalized: %d,%d'):format(x6,y6), 20, 280)
    love.graphics.print(('eq1: %d'):format(vecA == vecB and 1 or 0), 20, 300)
    love.graphics.print(('eq2: %d'):format(vecA:getNormalized() == vecB:getNormalized() and 1 or 0), 20, 320)

end

Comments (3)

  1. Alex Szpakowski

    I mentioned this in IRC already, but I'm not really sold on having vector objects in a LÖVE module implemented in C. It'll nearly always be slower than a pure Lua (or Lua+FFI) solution when using LuaJIT, there are good pure Lua vector libraries out there already, and having vectors integrated into LÖVE's API raises questions about whether the rest of the API should accept vector arguments and use vector return values.

    Some thoughts on the actual code (I also mentioned most of this in IRC):

    • You could probably make a LuaVector class in the math module which inherits from both Vector and Object, instead of making all of love's internal Vectors be Objects.

    • I like the names newVector and Vector much better than newVector2d and Vector2d. LÖVE is already a 2D framework, so the dimensionality of the vectors is already implied there - and if 3D vectors are ever wanted, things can always change when that bridge is crossed.

    • You could have default values of 0, 0 for the x, y arguments of newVector.

    • There should probably be a way to set the x and y components of a Vector after it's been created.

    • I agree with most of the comments about the metamethods - special vector metamethods should mostly only be used if they're obvious, IMO.

    • I think some functionality is missing, e.g. it might be nice to be able to easily add a single number to both components of a vector.

    Of course, the above points don't invalidate my first paragraph. :)