lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


"Nick Trout" <nick@rockstarvancouver.com> wrote in message
911F8C8EB7A8084AAEDD55CEDC54D8F80CB4BC@iggy.rockstarvancouver.com">news:911F8C8EB7A8084AAEDD55CEDC54D8F80CB4BC@iggy.rockstarvancouver.com...

> If you use light userdata you could use a purely functional interface to
> manipulate vectors, e.g.
>
> v1 = vec_new()
> vec_set(v1, 1,2,3)
> vec_setx(v1, vec_gety(v2)+10)


Ouch.. nope that isn't the way to go, I want to access vectors as "wholes"
most of the time, the individual element-like access is for doing "fiddly"
stuff.

So I want:

pos = pos + vec * 10

Where pos and vec are vectors, and "10" is a float that is broadcast
multiplied against vec.

> If you use heavy userdata you could set up an OO style of manipulation.
> E.g.

> v1 = Vector:new()
> v1:set(1,2,3)
> v1.x = v2.y + 10
>
> You could write the above using a metatable which could just wrap the
> functional interface. If you have lots vectors you may not want the
> overhead of all the meta-mechanism stuff (I'm not sure what the overhead
> is TBH).

This is the way I was thinking, but the overhead is a little worrisome and
the data is all too "separate" because its not referenced so I imagine all
these 12-16 byte chunks of data being allocated all over the shop.

> You could have a hybrid where you where a heavy userdata type which is a
> pointer to a vector and you feed it a light userdata vector. The wrapper
> gives it OO features but the data is light. You could reuse the wrapper
> for each vector operation, rather
>
> v1 = vec_new()    -- light
> hv1 = Vector:new() -- vector wrapper
> hv1:setPointer(v1) -- set light ptr in wrapper
> hv1:set(1,2,3)
> hv1:normalize()
> v2 = vec_new() -- new light
> hv1:setPointer(v2)  -- reuse wrapper
> hv1:set(8,7,6)
> hv1:dot(v1)

Hmm.. this is more interesting.. or rather than using the heavy userdata,
simply have a meta-tabled lua table that holds a light userdata structure
and then override all the table's __add/__sub functions to call C routines
that manipulate that light userdata.  Then the vector can still be somewhere
inside a c++ or c structure for all lua cares.

> Another way to add vectors would be to hack the Lua VM and add a new
> data type. This seems a little pointless as Lua is so geared to
> customisation.

This is what I *really* want, purely for speed and ease-of-conversion, its
about time languages added a vector type to their integral types (in C++ you
can do it with templates but compilation slows down terribly).  Most modern
applications have to deal with 3d in some form or other now which wasn't the
case when most of the languages were invented.  ints and floats don't cut
the mustard no more.

Sure you can just about do it in Lua but when almost every operation in a
video game involves a vector of some sort it becomes obvious that optimizing
that one area would create enormous gains. (for the gaming industry)

Modern cpus now have ints, floats *and* vectors as integral types.

Remember, you don't need to have floats as integral types either, you can
implement a floating point system in lua using integers and metatables. ;-)