examples

Toy examples in single C files.
git clone git://henryandlizzy.uk/examples
Log | Files | Refs

commit 9636e425c8ecf42cd00f574ced88656a5d528ac2
parent 81027f9fa9973e5b56e10200396492372a294d0f
Author: Henry Wilson <henry@henryandlizzy.uk>
Date:   Thu, 28 Apr 2022 19:38:44 +0100

gl-asteroids: Factor coord_from_rotation

Diffstat:
Msrc/gl-asteroids.cpp | 12+++++++-----
1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/gl-asteroids.cpp b/src/gl-asteroids.cpp @@ -176,6 +176,11 @@ coord rotate(coord o, float a) return {o.x*c + o.y*s, o.y*c - o.x*s}; } +coord coord_from_rotation(float a) +{ + return {(float)sin(a), (float)cos(a)}; +} + struct physics { coord pos, vel; @@ -479,7 +484,7 @@ int main(int argc, char* argv[]) b.ang_mom = 0; b.scale /= 1.25; - auto vel = coord{(float)sin(b.rot), (float)cos(b.rot)} * bullet_speed; + auto vel = coord_from_rotation(b.rot) * bullet_speed; b.vel += vel; ship_phys.vel -= vel * .05f; } @@ -497,10 +502,7 @@ int main(int argc, char* argv[]) up = is_pressed(GLFW_KEY_UP); if (up) - { - float sr = sin(ship_phys.rot), cr = cos(ship_phys.rot); - ship_phys.vel += coord{sr, cr} * dt; - } + ship_phys.vel += coord_from_rotation(ship_phys.rot) * dt; // resolve motion