mattdesl / glo

dibble-dabbling with a frameworky 3D engine
MIT License
25 stars 0 forks source link

data types: arrays or objects #2

Open mattdesl opened 9 years ago

mattdesl commented 9 years ago

Things like Vector, Color, Matrix4, etc.

It can be super frustrating dealing with code that looks like this:

tmp[0] = 5
tmp[1] = 0
tmp[2] = 0
obj.translate(tmp)

//or
vec3.add(obj.position, obj.position, somethingElse)

Instead of:

tmp.set(x, y, z)
obj.translate(tmp)

//or
obj.translate(somethingElse)

However.. array types have a lot of benefits including:

So I think array types for all these structures is still the best way to go.

But maybe since this is a fairly opinionated "frameworky" tool, there can be some middle ground that has a nicer UX.

chrisdickinson commented 9 years ago

Posted this on Twitter, but on second thought it'd be good to record here: I think the array format is probably the "right way" to write vectors for minimal {un,}marshalling at module boundaries, though it's somewhat uncomfortable. Given the success JSX has seen wrapping a rather unpalatable base-type in syntactic sugar, it might be good to see about writing a JS transform that makes matrix operations on arrays more natural. As a bonus, as things like SIMD land, it could automatically transpile code to use those optimizations.

mattdesl commented 9 years ago

Cool idea, especially SIMD. It would be attractive as a Babel plugin even outside of this framework.

xeolabs commented 9 years ago

I'm totally regretting using objects instead of arrays within SceneJS, since I always need to convert back and forth in order to use standard math functions (eg. glMatrix), which in many cases also incurs garbage collection overhead.

In my next engine I'm using arrays for everything and it's just so much cleaner!

gregtatum commented 9 years ago

The transform idea sounds intriguing. I think it would need enough pay off to justify that overhead though. I'd love to see some use-cases written out. I'm still torn between objects vs. arrays myself. It's so much easier to write and read object syntax, but when I'm thinking in terms of data structures and array buffers I think in terms of arrays. When I'm moving a box around a scene I think in terms of object syntax.

reminyborg commented 9 years ago

+1 for arrays. I remember toji's glmatrix used arrays for speed.

coderitual commented 9 years ago

Don't even think about not using arrays :)