vector math in Corona?

Hi,

I was just wondering if there is a way to implement some vector math in Lua, as undocumented features or an existing external module or any other way.

I’m about to start writing my own methods for this, but I just thought I would check before doing unnecessary work.

Thanks,
Thomas [import]uid: 70134 topic_id: 17342 reply_id: 317342[/import]

It’s not in the standard library, but you can probably find a library for it if you Google for it. It’s certainly possible to “overload” the arithmetic operators for vector math.

*However*, it’s not necessarily useful:

  1. 2D math means you’ll write at most 2x code for basic arithmetic operations.
  2. Types are implicit in Lua, there’s the risk to forget what “supposed type” the table object is, it’s more clear if you’re accessing x/y members.
  3. Efficiency is a slight concern. To call [meta]methods for operators you’re adding stuff the Lua code does. If you can keep vectors as pairs of local numbers performance will be better (however, whether this matters in practice depends on the amount of math).
  4. I simply end up not writing as much vector math as in C++ games I’ve worked on, and instead depend on the built-in physics engine a lot.

What I miss the most are matrices really, but again, I manage without them in my code for aforementioned reasons. [import]uid: 58849 topic_id: 17342 reply_id: 65635[/import]

Thomas, you will have to write the lua module yourself. Brendon Treb had an example of Boids, swarming agents and it uses vector classes. As me7 rightly suggests, that many developers use the inbuilt Box2D instead of using their own vector math.

However there are cases when vector math can be very useful and quite effective.

Here’s an example post where I have used vector math instead of physics
here

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 17342 reply_id: 65671[/import]

It’d be awesome if Corona had a way to perform fast vector and matrix math. I ended up creating my own modules for steering behaviors in Cannon Cat. Sorry can’t share but wanted to echo me7 concerns on efficiency.

My first naive implementation was slow on device. I was finally satisfied with performance when I used tables as arrays instead of hashes and had to cut out all unnecessary computations, object instantiations and function calls. [import]uid: 27183 topic_id: 17342 reply_id: 65682[/import]