Apply rotations in the correct order

This commit is contained in:
Dane Johnson 2021-01-20 22:17:33 -06:00
parent 7ac961ea42
commit 6f03ed7295
2 changed files with 10 additions and 6 deletions

View File

@ -27,8 +27,11 @@ void Transform::Translate(cfloat x, cfloat y, cfloat z) {
Vector3 Transform::Forward() {
Vector3 forward(0.0f, 0.0f, -1.0f);
Matrix mat(1.0f);
mat = glm::rotate(mat, this->rotation.x, Vector3(1.0f, 0.0f, 0.0f));
mat = glm::rotate(mat, this->rotation.y, Vector3(0.0f, 1.0f, 0.0f));
mat = glm::rotate(mat, this->rotation.z, Vector3(0.0f, 0.0f, 1.0f));
return mat * glm::vec4(forward, 1.0f);
mat = glm::rotate(mat, this->rotation.y, Vector3(0.0f, 1.0f, 0.0f));
mat = glm::rotate(mat, this->rotation.x, Vector3(1.0f, 0.0f, 0.0f));
return glm::vec3(mat * glm::vec4(forward, 1.0f));
}

View File

@ -61,6 +61,9 @@ end
function update(delta)
local cam_forwards = camera.transform:Forward()
local cam_rotation = camera.transform.rotation
print (cam_forwards.x, cam_forwards.y, cam_forwards.z)
print (cam_rotation.x, cam_rotation.y, cam_rotation.z)
local move_vec = couch.Vector3()
move_vec = camera.transform.position + cam_forwards * delta * vz * SPEED
@ -69,7 +72,7 @@ function update(delta)
camera.transform.rotation.y = camera.transform.rotation.y - cam_rot_x * delta
cam_rot_x = 0.0
camera.transform.rotation.x = camera.transform.rotation.x - cam_rot_y * delta
camera.transform.rotation.x = min(max(camera.transform.rotation.x, -3.14), 3.14)
camera.transform.rotation.x = min(max(camera.transform.rotation.x, -3.14 / 2.0), 3.14 / 2.0)
cam_rot_y = 0.0
local loc = ball1.transform.position
@ -81,8 +84,6 @@ function update(delta)
ball1.transform.position.y = ball1.transform.position.y + ballvy * delta
ball.transform.rotation.z = ball.transform.rotation.z + 1.0 * delta;
ball.transform.rotation.y = ball.transform.rotation.y + 1.0 * delta;
ball.transform.rotation.x = ball.transform.rotation.x + 1.0 * delta;
end
function onkey(key, code, action, mod)