Implement transform math to rotate movement direction
parent
8db797f0a1
commit
065f63e66d
5
Makefile
5
Makefile
|
@ -4,9 +4,10 @@ BUILDDIR ?= target/$(PLATFORM)
|
|||
SRCDIR ?= src
|
||||
|
||||
CPPFLAGS ::= -iquotesrc/ $(CPPFLAGS)
|
||||
CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror -Wno-error=unused-function $(CFLAGS)
|
||||
CFLAGS ::= -g -std=c99 -Wall -Wextra -Wpedantic -Werror \
|
||||
-Wno-error=unused-function -Wno-error=unused-parameter $(CFLAGS)
|
||||
LDFLAGS ::= $(LDFLAGS)
|
||||
LDLIBS ::= -lGL -lGLEW -lglut -lassimp $(LDLIBS)
|
||||
LDLIBS ::= -lm -lGL -lGLEW -lglut -lassimp $(LDLIBS)
|
||||
|
||||
# ######
|
||||
# Paths
|
||||
|
|
|
@ -1,7 +1,12 @@
|
|||
#include "geometry.h"
|
||||
|
||||
#include <math.h>
|
||||
#include <stddef.h>
|
||||
|
||||
const float TAU = 6.28318530718f;
|
||||
|
||||
|
||||
|
||||
Transform identity() {
|
||||
return (Transform) { .a1 = 1.0f, .a2 = 0.0f, .a3 = 0.0f, .a4 = 0.0f,
|
||||
.b1 = 0.0f, .b2 = 1.0f, .b3 = 0.0f, .b4 = 0.0f,
|
||||
|
@ -34,6 +39,42 @@ void translate(Transform* transform, Vector3D vec) {
|
|||
*transform);
|
||||
}
|
||||
|
||||
void rotate(Transform* transform, Vector3D axis, float angle) {
|
||||
axis = normalized(axis);
|
||||
float l = axis.x;
|
||||
float m = axis.y;
|
||||
float n = axis.z;
|
||||
float sinA = sinf(angle);
|
||||
float cosA = cosf(angle);
|
||||
float omcA = 1 - cosA;
|
||||
*transform = multiply(
|
||||
(Transform) { l*l*omcA + cosA, m*l*omcA - n*sinA, n*l*omcA + m*sinA, 0.0f,
|
||||
l*m*omcA + n*sinA, m*m*omcA + cosA, n*m*omcA - l*sinA, 0.0f,
|
||||
l*n*omcA - m*sinA, m*n*omcA + l*sinA, n*n*omcA + cosA, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f },
|
||||
*transform);
|
||||
}
|
||||
|
||||
Vector3D applyTransform(Transform* transform, Vector3D vec) {
|
||||
GLfloat* a = (GLfloat*) &transform;
|
||||
GLfloat b[4] = { vec.x, vec.y, vec.z, 1.0f };
|
||||
GLfloat c[4];
|
||||
|
||||
for (size_t row = 0; row < 4; ++row) {
|
||||
c[row] =
|
||||
a[(row * 4) + 0] * b[0]
|
||||
+ a[(row * 4) + 1] * b[1]
|
||||
+ a[(row * 4) + 2] * b[2]
|
||||
+ a[(row * 4) + 3] * b[3];
|
||||
}
|
||||
return (Vector3D) { c[0], c[1], c[2] };
|
||||
}
|
||||
|
||||
Vector3D translationOf(Transform transform) {
|
||||
return (Vector3D) { transform.a4, transform.b4, transform.c4 };
|
||||
}
|
||||
|
||||
Vector3D normalized(Vector3D vec) {
|
||||
float magnitude = sqrtf(vec.x * vec.x + vec.y * vec.y + vec.z * vec.z);
|
||||
return (Vector3D) {vec.x / magnitude, vec.y / magnitude, vec.z / magnitude};
|
||||
}
|
||||
|
|
|
@ -19,9 +19,14 @@ struct Transform {
|
|||
GLfloat d1, d2, d3, d4;
|
||||
};
|
||||
|
||||
extern const float TAU;
|
||||
|
||||
Transform identity();
|
||||
Transform multiply(Transform t1, Transform t2);
|
||||
void translate(Transform* transform, Vector3D vec);
|
||||
void rotate(Transform* transform, Vector3D axis, float angle);
|
||||
Vector3D applyTransform(Transform* transform, Vector3D vec);
|
||||
Vector3D translationOf(Transform transform);
|
||||
Vector3D normalized(Vector3D vec);
|
||||
|
||||
#endif // GEOMETRY_H_
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
#include "input.h"
|
||||
|
||||
#include "player.h"
|
||||
|
||||
void onKeyPressed(unsigned char key, int x, int y) {
|
||||
switch (key) {
|
||||
case 'w':
|
||||
case 's':
|
||||
case 'a':
|
||||
case 'd':
|
||||
//playerMovementInput();
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
#ifndef INPUT_H_
|
||||
#define INPUT_H_
|
||||
|
||||
void onKeyPressed(unsigned char key, int x, int y);
|
||||
|
||||
#endif // INPUT_H_
|
|
@ -17,3 +17,10 @@ void spawnPlayer(Transform transform) {
|
|||
playerCharacter->transform = transform;
|
||||
reparentScene(playerCharacter, currentScene);
|
||||
}
|
||||
|
||||
void playerMovementInput(float x, float y) {
|
||||
Transform rotation = identity();
|
||||
rotate(&rotation, (Vector3D) { 0.0f, 1.0f, 0.0f }, TAU / 8.0f);
|
||||
Vector3D movementDirection = (Vector3D) { x, 0.0f, -y };
|
||||
movementDirection = normalized(applyTransform(&rotation, movementDirection));
|
||||
}
|
||||
|
|
|
@ -9,5 +9,6 @@ extern Scene* playerCharacter;
|
|||
|
||||
void initPlayer();
|
||||
void spawnPlayer(Transform transform);
|
||||
void playerMovementInput(float x, float y);
|
||||
|
||||
#endif // PLAYER_H_
|
||||
|
|
Loading…
Reference in New Issue