More vector maths functions

Hello!
Vector maths based on quaternions and matrices is really hard for newbies.
Not everyone did lern quaternions in school.
In this example
p = p + vmath.rotate(rot, move_amount)
vmath.rotate(q,v1)
A quaternion is used in case where is possible to create something more simple.
For example:
go.get_look_forward_vector3(current_game_object),
also backward, left, right, up, down.
It would make coding significantly easier!
Also, I think, it’s needed to add euler-based functions that make same thing
like as existing quaternion’s functions.
(Including rotating vectors by euler and geting vectors from euler (look_forward)).
Defold is exposed as a simple game engine. But quaternions is not simple!

In the example, the quaternion used to rotate the movement vector, rot, is calculated earlier in the script, and used to rotate the object. It is not like the kind of vectors you mentioned. Do you have another question about the example?

If you prefer to work with vectors, Defold has vmath.matrix4_from_quat() to change a quaternion into a matrix. The first three rows of that matrix are those vectors you’re asking about (right, up, back). One tricky part is making sure you read the column/row order correctly:

local q = go.get_rotation() -- or whatever quat you're working with
local m = vmath.matrix4_from_quat(q)

local right = vmath.vector3(m.c0.x, m.c1.x, m.c2.x)
local left = -right
local up = vmath.vector3(m.c0.y, m.c1.y, m.c2.y)
local down = -up
local back = vmath.vector3(m.c0.z, m.c1.z, m.c2.z)
local forward = -back
3 Likes

Thanx a lot! I never did face to the matrices before.
So matrix was a something absolutly new for me.
Thanx for help!

1 Like