transform.c (838B)
1 #include "transform.h" 2 3 #include <math.h> 4 5 const Vec3 WORLD_UP = { 0, 1, 0 }; 6 7 void Transform_init( 8 Transform *t, 9 f32 x, 10 f32 y, 11 f32 z, 12 f32 yaw, 13 f32 pitch 14 ) 15 { 16 t->yaw = yaw; 17 t->pitch = pitch; 18 Vec3_set(x, y, z, t->position); 19 Transform_updateVectors(t); 20 } 21 22 // Set forward, up, and right vectors based on pitch and yaw. 23 void Transform_updateVectors(Transform *t) 24 { 25 Vec3_set( 26 cosf(radians(t->yaw)) * cosf(radians(t->pitch)), 27 sinf(radians(t->pitch)), 28 sinf(radians(t->yaw)) * cosf(radians(t->pitch)), 29 t->forward 30 ); 31 Vec3_normalize(t->forward, t->forward); 32 33 Vec3_mul(t->forward, WORLD_UP, t->right); 34 Vec3_normalize(t->right, t->right); 35 36 Vec3_mul(t->right, t->forward, t->up); 37 Vec3_normalize(t->up, t->up); 38 }