Created
April 5, 2025 16:15
-
-
Save GermanHoyos/2ca4d73c1c04d40855696427d31042fc to your computer and use it in GitHub Desktop.
Boid Foundational Class / Perfect Unit Circle - Smooth Movement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Written by German Adrian Hoyos | |
// Linkedin: https://www.linkedin.com/in/adrian-i-81a32615b/ | |
// Common c++ headers + custom headers | |
#include "include/MasterHeader.h" | |
class Player { | |
public: | |
Vector2 xy; | |
Vector2 attackVector; | |
float x; | |
float y; | |
float radius; | |
float angle = 90 / 180 * PI; // convert to degrees | |
float thrustAngle = 0 / 180 * PI; | |
float rotation = 0.017; | |
float turnSpeed = 2.5; | |
float acceleration = 0.05; | |
float friction = 0.02; | |
Player(float x, float y, float radius) | |
: x(x), y(y), radius(radius) {} | |
void determineAngle(){ | |
if (IsKeyDown(KEY_A)){ | |
angle -= rotation * turnSpeed; | |
} | |
if (IsKeyDown(KEY_D)){ | |
angle += rotation * turnSpeed; | |
} | |
if (IsKeyDown(KEY_W)){ | |
thrustAngle = angle; | |
attackVector.x += acceleration * cosf(thrustAngle); | |
attackVector.y += acceleration * sinf(thrustAngle); | |
} else if (!IsKeyDown(KEY_W)) | |
{ | |
if (acceleration > 0.0f){ | |
attackVector.x -= friction * attackVector.x; | |
attackVector.y -= friction * attackVector.y; | |
} | |
} | |
} | |
void accelerate(){ | |
x += attackVector.x; | |
y += attackVector.y; | |
} | |
void draw(){ | |
xy.x = x; | |
xy.y = y; | |
DrawCircleSectorLines(xy, radius, 0.0f, 360.0f, 0, GREEN); | |
DrawLineV(xy,{xy.x + radius * cosf(angle), xy.y + radius * sinf(angle)}, RED); | |
determineAngle(); | |
accelerate(); | |
} | |
}; | |
int main() { | |
int screenWidth = 800; | |
int screenHeight = 600; | |
raylib::Window w(screenWidth, screenHeight, "Adrians Boids Project"); | |
SetTargetFPS(60); | |
Player myDrone = Player(500.0f,300.0f,100.0f); | |
while (!w.ShouldClose()) { | |
BeginDrawing(); | |
ClearBackground(BLACK); | |
TimeClass::displayGameTime(); // displays FPS and various other stats | |
myDrone.draw(); | |
EndDrawing(); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment