|
| 1 | +/* |
| 2 | +Raylib thruster example |
| 3 | +Simple example showing the math for thrust based motion |
| 4 | +*/ |
| 5 | + |
| 6 | +#include "raylib.h" |
| 7 | +#include "raymath.h" |
| 8 | + |
| 9 | +bool SearchAndSetResourceDir(const char* folderName); |
| 10 | + |
| 11 | +int main() |
| 12 | +{ |
| 13 | + // set up the window |
| 14 | + InitWindow(1280, 800, "In Thust We Trust"); |
| 15 | + SetTargetFPS(240); |
| 16 | + SearchAndSetResourceDir("resources"); |
| 17 | + Texture ship = LoadTexture("Ship.png"); |
| 18 | + |
| 19 | + float shipAngle = -90; // default graphic faces in the Y- direction so that is -90 degrees |
| 20 | + Vector2 shipPosition = { GetScreenWidth() * 0.5f, GetScreenHeight() * 0.5f }; // center us in the screen |
| 21 | + Vector2 shipVelocity = { 0,0 }; // we start not moving |
| 22 | + |
| 23 | + // game loop |
| 24 | + while (!WindowShouldClose()) |
| 25 | + { |
| 26 | + // rotation |
| 27 | + float rotationDelta = 180.0f * GetFrameTime(); |
| 28 | + if (IsKeyDown(KEY_A)) |
| 29 | + shipAngle -= rotationDelta; |
| 30 | + if (IsKeyDown(KEY_D)) |
| 31 | + shipAngle += rotationDelta; |
| 32 | + |
| 33 | + // compute the facing vector of the ship from the angle, the |
| 34 | + Vector2 shipFacing = (Vector2){ cosf((shipAngle) * DEG2RAD), sinf((shipAngle) * DEG2RAD) }; |
| 35 | + |
| 36 | + // thrust |
| 37 | + // compute the thrust(acceleration) to add this frame (use time) |
| 38 | + float thrustAmount = 0; |
| 39 | + if (IsKeyDown(KEY_W)) |
| 40 | + thrustAmount = 300 * GetFrameTime(); |
| 41 | + |
| 42 | + // see how much thrust we are going to add to our motion vector |
| 43 | + Vector2 thrustVector = Vector2Scale(shipFacing, thrustAmount); |
| 44 | + |
| 45 | + // add thrust to our velocity (units/second) |
| 46 | + shipVelocity = Vector2Add(shipVelocity, thrustVector); |
| 47 | + |
| 48 | + // clamp the velocity to some sane limit |
| 49 | + if (Vector2Length(shipVelocity) > 1000) |
| 50 | + shipVelocity = Vector2Scale(Vector2Normalize(shipVelocity), 1000); |
| 51 | + |
| 52 | + // if you want to add some friction, do it here, slowly reduce the lenght of shipVelocity |
| 53 | + |
| 54 | + // position |
| 55 | + // update our position based on our velocity for this frame |
| 56 | + shipPosition = Vector2Add(shipPosition, Vector2Scale(shipVelocity, GetFrameTime())); |
| 57 | + |
| 58 | + // collisions |
| 59 | + float radius = 25; |
| 60 | + |
| 61 | + if (shipPosition.x - radius < 0) |
| 62 | + { |
| 63 | + // we hit the left edge, bounce |
| 64 | + shipPosition.x = 0 + radius; |
| 65 | + shipVelocity.x *= -1; |
| 66 | + } |
| 67 | + |
| 68 | + if (shipPosition.x + radius > GetScreenWidth()) |
| 69 | + { |
| 70 | + // we hit the right edge, bounce |
| 71 | + shipPosition.x = GetScreenWidth() - radius; |
| 72 | + shipVelocity.x *= -1; |
| 73 | + } |
| 74 | + |
| 75 | + if (shipPosition.y - radius < 0) |
| 76 | + { |
| 77 | + // we hit the top edge, bounce |
| 78 | + shipPosition.y = 0 + radius; |
| 79 | + shipVelocity.y *= -1; |
| 80 | + } |
| 81 | + |
| 82 | + if (shipPosition.y + radius > GetScreenHeight()) |
| 83 | + { |
| 84 | + // we hit the top edge, bounce |
| 85 | + shipPosition.y = GetScreenHeight() - radius; |
| 86 | + shipVelocity.y *= -1; |
| 87 | + } |
| 88 | + |
| 89 | + BeginDrawing(); |
| 90 | + ClearBackground(DARKGRAY); |
| 91 | + |
| 92 | + DrawRectangleLinesEx((Rectangle){ 1,1,GetScreenWidth()-1,GetScreenHeight()-1 }, 3, RED); |
| 93 | + |
| 94 | + // draw the ship centered |
| 95 | + DrawTexturePro(ship, |
| 96 | + (Rectangle) { 0, 0, ship.width, ship.height }, |
| 97 | + (Rectangle) { shipPosition.x, shipPosition.y, ship.width, ship.height }, |
| 98 | + (Vector2) { ship.width * 0.5f, ship.height * 0.5f }, |
| 99 | + shipAngle+90 /*compensate for our rotated ship graphic*/, |
| 100 | + WHITE); |
| 101 | + |
| 102 | + DrawLineEx(shipPosition, Vector2Add(shipPosition, Vector2Scale(shipFacing, 100)), 2, PURPLE); |
| 103 | + |
| 104 | + EndDrawing(); |
| 105 | + } |
| 106 | + |
| 107 | + UnloadTexture(ship); |
| 108 | + // cleanup |
| 109 | + CloseWindow(); |
| 110 | + return 0; |
| 111 | +} |
| 112 | + |
| 113 | + |
| 114 | +bool SearchAndSetResourceDir(const char* folderName) |
| 115 | +{ |
| 116 | + // check the working dir |
| 117 | + if (DirectoryExists(folderName)) |
| 118 | + { |
| 119 | + ChangeDirectory(TextFormat("%s/%s", GetWorkingDirectory(), folderName)); |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + const char* appDir = GetApplicationDirectory(); |
| 124 | + |
| 125 | + // check the applicationDir |
| 126 | + const char* dir = TextFormat("%s%s", appDir, folderName); |
| 127 | + if (DirectoryExists(dir)) |
| 128 | + { |
| 129 | + ChangeDirectory(dir); |
| 130 | + return true; |
| 131 | + } |
| 132 | + |
| 133 | + // check one up from the app dir |
| 134 | + dir = TextFormat("%s../%s", appDir, folderName); |
| 135 | + if (DirectoryExists(dir)) |
| 136 | + { |
| 137 | + ChangeDirectory(dir); |
| 138 | + return true; |
| 139 | + } |
| 140 | + |
| 141 | + // check two up from the app dir |
| 142 | + dir = TextFormat("%s../../%s", appDir, folderName); |
| 143 | + if (DirectoryExists(dir)) |
| 144 | + { |
| 145 | + ChangeDirectory(dir); |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + // check three up from the app dir |
| 150 | + dir = TextFormat("%s../../../%s", appDir, folderName); |
| 151 | + if (DirectoryExists(dir)) |
| 152 | + { |
| 153 | + ChangeDirectory(dir); |
| 154 | + return true; |
| 155 | + } |
| 156 | + |
| 157 | + return false; |
| 158 | +} |
0 commit comments