polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit ab66e332265639a252e3d3c9b679b64b04836ac1
parent e73ab2ac56a742e586fb72efa6934a750d846565
Author: William Casarin <jb55@jb55.com>
Date:   Mon, 30 Apr 2018 14:27:47 -0700

initial quaternion stuff

Diffstat:
Asrc/quat.c | 38++++++++++++++++++++++++++++++++++++++
Asrc/quat.h | 12++++++++++++
2 files changed, 50 insertions(+), 0 deletions(-)

diff --git a/src/quat.c b/src/quat.c @@ -0,0 +1,38 @@ + +#include "quat.h" +#include <math.h> + +void quat_id(quat *dest) { + dest[0] = 0; + dest[1] = 0; + dest[2] = 0; + dest[3] = 1; +} + +void quat_multiply(quat *a, quat *b, quat *dest) { + float ax = a[0]; + float ay = a[1]; + float az = a[2]; + float aw = a[3]; + + float bx = b[0]; + float by = b[1]; + float bz = b[2]; + float bw = b[3]; + + dest[0] = ax * bw + aw * bx + ay * bz - az * by; + dest[1] = ay * bw + aw * by + az * bx - ax * bz; + dest[2] = az * bw + aw * bz + ax * by - ay * bx; + dest[3] = aw * bw - ax * bx - ay * by - az * bz; +} + + +void quat_axis_angle(float *axis, float angle, quat *dest) { + float half_angle = angle / 2.0; + float s = sin(half_angle); + + dest[0] = axis[0] * s; + dest[1] = axis[1] * s; + dest[2] = axis[2] * s; + dest[3] = cos(half_angle); +} diff --git a/src/quat.h b/src/quat.h @@ -0,0 +1,12 @@ + + +#ifndef POLYADVENT_QUAT4_ +#define POLYADVENT_QUAT4_ + +typedef float quat; + +void quat_id(quat *dest); +void quat_multiply(quat *a, quat *b, quat *dest); +void quat_axis_angle(float *axis, float angle, quat *dest); + +#endif /* POLYADVENT_QUAT4_ */