[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: Lua script in games? (vectors)
- From: "Dylan Cuthbert" <dylan@...>
- Date: Thu, 26 Jun 2003 10:37:21 +0900
"Nick Trout" <email@example.com> wrote in message
> 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"
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.
> v1 = Vector:new()
> 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
> v2 = vec_new() -- new light
> hv1:setPointer(v2) -- reuse wrapper
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
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. ;-)