diff options
author | nathansmithsmith <nathansmith7@mailfence.com> | 2023-09-23 18:09:54 -0600 |
---|---|---|
committer | nathansmithsmith <nathansmith7@mailfence.com> | 2023-09-23 18:09:54 -0600 |
commit | 6ba19e2332162de3225d60ae2a377a894ab7299f (patch) | |
tree | da67dc163cafcd980c5bfc979aa66a453d655464 | |
parent | 2dca93d13705ddabfb198680039b839ae15852d8 (diff) |
Better gyroscope rotation
-rw-r--r-- | src/gyroscope.c | 35 | ||||
-rw-r--r-- | src/gyroscope.h | 2 |
2 files changed, 25 insertions, 12 deletions
diff --git a/src/gyroscope.c b/src/gyroscope.c index dba5d8d..980345c 100644 --- a/src/gyroscope.c +++ b/src/gyroscope.c @@ -3,17 +3,6 @@ #include "assets.h" #include "world.h" -void resetGyroscopePosition(Gyroscope * gyroscope) { - float width = GetScreenWidth(); - float height = GetScreenHeight(); - - // Set position on screen. - gyroscope->position = (Vector2){ - (width / 2.0) - (GYROSCOPE_TEXTURE_SIZE / 2.0), - height / 1.8 - }; -} - void initGyroscope(Gyroscope * gyroscope) { resetGyroscopePosition(gyroscope); @@ -35,9 +24,32 @@ void closeGyroscope(Gyroscope * gyroscope) { UnloadRenderTexture(gyroscope->texture); } +void resetGyroscopePosition(Gyroscope * gyroscope) { + float width = GetScreenWidth(); + float height = GetScreenHeight(); + + // Set position on screen. + gyroscope->position = (Vector2){ + (width / 2.0) - (GYROSCOPE_TEXTURE_SIZE / 2.0), + height / 1.8 + }; +} + +void updateGyroscopeRotation(Entity * player, Gyroscope * gyroscope) { + // Instead of rotation the gyroscope ball we rotate the camera around it. + gyroscope->camera.position = Vector3Scale( + Vector3RotateByQuaternion((Vector3){0.0, 0.0, 1.0}, player->rotation), + GYROSCOPE_CAMERA_DISTANCE + ); + + gyroscope->camera.up = Vector3RotateByQuaternion((Vector3){0.0, 1.0, 0.0}, player->rotation); +} + void drawGyroscope(Game * game, Gyroscope * gyroscope) { Entity * player = getEntityFromWorld(game->world, 0); + updateGyroscopeRotation(player, gyroscope); + // Get model Model model = game->assets.models[GYROSCOPE_ASSET]; @@ -47,7 +59,6 @@ void drawGyroscope(Game * game, Gyroscope * gyroscope) { BeginMode3D(gyroscope->camera); // Set transform and draw. - model.transform = QuaternionToMatrix(player->rotation); DrawModel(model, Vector3Zero(), 1, WHITE); EndMode3D(); diff --git a/src/gyroscope.h b/src/gyroscope.h index c8156cc..6a86e11 100644 --- a/src/gyroscope.h +++ b/src/gyroscope.h @@ -5,6 +5,7 @@ #define GYROSCOPE_LINE_THICKNESS 2.0 #define GYROSCOPE_TEXTURE_SIZE 80 +#define GYROSCOPE_CAMERA_DISTANCE 3.0 // The gyroscope is rended in 3d but not in the main scene. // We use a render texture to get the gyroscope render onto the screen. @@ -18,6 +19,7 @@ void initGyroscope(Gyroscope * gyroscope); void closeGyroscope(Gyroscope * gyroscope); void resetGyroscopePosition(Gyroscope * gyroscope); + void drawGyroscope(Game * game, Gyroscope * gyroscope); #endif |