deanm / plask

Plask is a multimedia programming environment.
www.plask.org
Other
443 stars 38 forks source link

Add axis + angle rotation for Vec3 #12

Closed notlion closed 10 years ago

notlion commented 13 years ago

Should be a bit faster than making a Mat4, and more convenient sometimes. I made a test here to show that it works:

https://gist.github.com/1267219

deanm commented 13 years ago

I was thinking, even though it's nice to just have this on vec3, isn't the way you should really go about this:

mat4 = new plask.Mat4(); mat4.rotate(angle, x, y, z); mat4.mulVec3(vec)

?

notlion commented 13 years ago

IMO it's mostly a convenience issue, but with a performance bonus. Using a Mat4 incurs the cost of creating the identity and rotation matrices, multiplying them, and then actually transforming your Vec3. It makes sense if you use that Mat4 repeatedly, but for certain things I want to be able to chain a lightweight rotation onto some other vector operations.

I think all the issues you addressed should be fixed with my latest commit.

deanm commented 13 years ago

But there are loads of things you can do with a mat4, where do you draw the line to what goes into vec3?

notlion commented 13 years ago

I guess the best answer I can give is "common operations", which to me means translate (add), rotate, scale. Those are at least the ones that could be performed with a matrix, but are simpler computationally and code-wise to do in-place.

There might be an alternative to this if we changed the way Mat4 works. For example..

vec3.rotate(theta, x, y, z)

vs

vec3.mulMat4(Mat4.createRotation(theta, x, y, z))

..if Mat4.createRotation() didn't incur the call to reset() and mul4x4r, that would probably be a more general solution and would allow matrix multiplication to be chained with other vector operations.

deanm commented 13 years ago

You can write (new plask.Mat4()).rotate(theta, x, y, z).mulVec3(vec)

notlion commented 13 years ago

true, but you'd break the flow of a statement like this..

v2.subbed(v1).rotate(theta, x, y, z).normalize()

..and besides would still incur the matrix creation and 2x multiplication cost.

deanm commented 13 years ago

Yeah, but when you talk about performance that goes for any/all operations, it would always be cheaper if we rolled multiple operations into one, or specialized everything in mat4. I dunno, I guess I'm just not that convinced about this specific case... How often do you really do it? How important is the performance really?

notlion commented 13 years ago

Hm. The specific use case I have in mind i'm doing twice per edge over a mesh.. It's a one time process though, so performance isn't a huge concern. I just find it convenient to have a rotate function on Vec3. I'll try it with matrices for now.

It might be nice to think about static matrix creation functions like Mat4.createRotation() for the future though.