vec2 usage

Probably a simple Q: what is vec2 used for? I’ve seen it used to specify a location for buttons, minesweeper cells, etc – why not just use a x,y position? What am I (conceptually) missing?

TIA

Sometimes a vec2 is just used as a glorified x/y structure and because it is readily available. Some code could indeed be rewritten without vec2. You’re missing nothing.

The vec2 userdata values comes with built-in functionality that would be a chore to code from scratch and other parts of the Codea API expect or return vec2 userdata values.

I saw the vec2:angleBetween() function but not for vec3.

To clarify akiva’s question here’s an example from CiderControls for uses of vec2 for no other reason than to have x and y in one object:


function Frame:roundRect(r)
    pushStyle()
    insetPos = vec2(self.left + r,self.bottom + r)
    insetSize = vec2(self:width() - 2 * r,self:height() - 2 * r)

    rectMode(CORNER)
    rect(insetPos.x, insetPos.y, insetSize.x, insetSize.y)

    if r > 0 then
        smooth()
        lineCapMode(ROUND)
        strokeWidth(r * 2)

        line(insetPos.x, insetPos.y,
             insetPos.x + insetSize.x, insetPos.y)
        line(insetPos.x, insetPos.y,
             insetPos.x, insetPos.y + insetSize.y)
        line(insetPos.x, insetPos.y + insetSize.y,
             insetPos.x + insetSize.x, insetPos.y + insetSize.y)
        line(insetPos.x + insetSize.x, insetPos.y,
             insetPos.x + insetSize.x, insetPos.y + insetSize.y)
    end
    popStyle()
end

You can see that the code can be rewritten without vec2 and without losing expressiveness.

By the way, I would argue that naming the objects pos and size and leaving the inset for a comment would add expressiveness by readability. I would even go further and - while eradicating vec2 - name the components like this:


function Frame:roundRect(r)
    pushStyle()
    -- 'p'osition and 's'ize of the inset
    local px, py = self.left + r, self.bottom + r
    local sx, sy = self:width() - 2 * r,self:height() - 2 * r

    rectMode(CORNER)
    rect(px, py, sx, sy)

    if r > 0 then
        smooth()
        lineCapMode(ROUND)
        strokeWidth(r * 2)

        line(px     , py     , px + sx, py     )
        line(px     , py     , px     , py + sy)
        line(px     , py + sy, px + sx, py + sy)
        line(px + sx, py     , px + sx, py + sy)
    end
    popStyle()
end

I might further argue to use x, y, w and h, but the above is closer to the original code.

I might further think about posting this into the Cider thread as I like the readability.

.@Jmv38: Indeed vec3 (and vec4) lack angleBetween(), but that can easily be rectified:

do
    local v3,v4=getmetatable(vec3()),getmetatable(vec4())
    function v3:angleBetween(v)
        return math.acos(self.dot(self,v)/(self.len(self)*v.len(v)))
    end
    function v4:angleBetween(v)
        return math.acos(self.dot(self,v)/(self.len(self)*v.len(v)))
    end
end

Thanks for the tip @Olaf.

Thanks Codeslinger – That’s exactly what I was talking about. I agree with your obeservations – I much prefer the x,y,w,h style of coding – I find it much more obvious and self-documenting.

On the other hand, once you start working with matrices and transformations then vectors are pretty useful things.