Skip to content

cpml.mat4.from_transform() computes the wrong rotation matrix. #83

@leonardus

Description

@leonardus

cpml/modules/mat4.lua:

local rx, ry, rz, rw = rot.x, rot.y, rot.z, rot.w

local rm = new {
  1-2*(ry*ry+rz*rz), 2*(rx*ry-rz*rw), 2*(rx*rz+ry*rw), 0,
  2*(rx*ry+rz*rw), 1-2*(rx*rx+rz*rz), 2*(ry*rz-rx*rw), 0,
  2*(rx*rz-ry*rw), 2*(ry*rz+rx*rw), 1-2*(rx*rx+ry*ry), 0,
  0, 0, 0, 1
}

Something about how that rotation matrix is calculated is wrong. Because when I replace it with my own function, that gets the rotation matrix the same way mat4.from_quaternion computes the matrix (using q:to_angle_axis()), I get the correct results that I was expecting:

function dbg_from_transform(trans, rot, scale)
	local sm = cpml.mat4.new{
		scale.x, 0,       0,       0,
		0,       scale.y, 0,       0,
		0,       0,       scale.z, 0,
		0,       0,       0,       1,
	}

	local rm = cpml.mat4.from_angle_axis(rot:to_angle_axis())

	local rsm = rm * sm

	rsm[13] = trans.x
	rsm[14] = trans.y
	rsm[15] = trans.z

	return rsm
end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions