Add eagle type

master
John Montagu, the 4th Earl of Sandvich 2026-03-01 23:17:40 -08:00
parent cdd9a7e353
commit 76e147e244
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
1 changed files with 40 additions and 50 deletions

View File

@ -54,6 +54,10 @@ struct Entity {
float speed = 0; float speed = 0;
raylib::Degree heading = 0; raylib::Degree heading = 0;
raylib::Model *model; raylib::Model *model;
enum EntityType {
Penguin = 0,
Eagle = 1,
} type = EntityType::Penguin;
}; };
raylib::Degree angle_normalize(raylib::Degree angle) { raylib::Degree angle_normalize(raylib::Degree angle) {
@ -73,10 +77,19 @@ raylib::Vector3 from_magnitude_and_heading(
}; };
} }
// units/s // this is for the eagle to move in the xy plane
const float ACCELERATION = 100.0f; raylib::Vector3 from_magnitude_and_heading_xy(
float magnitude,
raylib::Degree heading
) {
return raylib::Vector3 {
magnitude * std::sin(heading.RadianValue()),
magnitude * std::cos(heading.RadianValue()),
0
};
}
// in radians const float ACCELERATION = 100.0f;
const raylib::Degree ANGULAR_VELOCITY = raylib::Degree(180.0f); const raylib::Degree ANGULAR_VELOCITY = raylib::Degree(180.0f);
int main() { int main() {
@ -87,19 +100,13 @@ int main() {
raylib::Model penguin("models/penguin.glb"); raylib::Model penguin("models/penguin.glb");
raylib::Model eagle("models/eagle.glb"); raylib::Model eagle("models/eagle.glb");
// behind and above the penguin (in penguin-local space)
const float CAM_DIST = 512.0f;
const float CAM_HEIGHT = 256.0f;
const float CAM_ANGULAR_VELOCITY = 2.0f;
const float CAM_PITCH_MIN = -0.5f;
const float CAM_PITCH_MAX = 1.5f;
float camYaw = 3.14f; // offset by 90 deg so it faces in the proper direction float camYaw = 3.14f; // offset by 90 deg so it faces in the proper direction
float camPitch = 0; float camPitch = 0;
raylib::Camera3D camera( raylib::Camera3D camera(
{ 0, CAM_DIST * std::sin(camPitch), CAM_DIST * std::cos(camPitch) }, //{ 0, CAM_DIST * std::sin(camPitch), CAM_DIST * std::cos(camPitch) },
{ 0, 0, 0 }, { 0, 200, -768 },
{ 0, -8, 0 },
{ 0, 1, 0 }, { 0, 1, 0 },
45.0f); 45.0f);
@ -114,9 +121,13 @@ int main() {
Entity &p1 = entities.emplace_back(); Entity &p1 = entities.emplace_back();
p1.model = &penguin; p1.model = &penguin;
p1.type = Entity::EntityType::Penguin;
p1.position = raylib::Vector3(0, 0, 0);
Entity &p2 = entities.emplace_back(); Entity &p2 = entities.emplace_back();
p2.model = &penguin; p2.model = &eagle;
p2.type = Entity::EntityType::Eagle;
p2.position = raylib::Vector3(200, 200, 0);
// penguin physics // penguin physics
raylib::Vector3 position = { 0, 0, 0 }; raylib::Vector3 position = { 0, 0, 0 };
@ -166,38 +177,6 @@ int main() {
e.heading = angle_normalize(e.heading); e.heading = angle_normalize(e.heading);
} }
// ds = 1/2 * (v0 + v1) * dt
position += velocity * dt * 0.5f;
// movement for camera
if (IsKeyDown(KEY_LEFT)) {
camYaw += CAM_ANGULAR_VELOCITY * dt;
}
if (IsKeyDown(KEY_RIGHT)) {
camYaw -= CAM_ANGULAR_VELOCITY * dt;
}
if (IsKeyDown(KEY_UP)) {
camPitch += CAM_ANGULAR_VELOCITY * dt;
}
if (IsKeyDown(KEY_DOWN)) {
camPitch -= CAM_ANGULAR_VELOCITY * dt;
}
// clamp the angle between
camPitch = std::clamp(camPitch, CAM_PITCH_MIN, CAM_PITCH_MAX);
// x = cos(pitch) * sin(yaw)
// y = sin(pitch)
// z = cos(pitch) * cos(yaw)
float yaw = camYaw + heading; // follow penguin
raylib::Vector3 camOffset = {
CAM_DIST * std::cos(camPitch) * std::sin(yaw),
CAM_DIST * std::sin(camPitch) + CAM_HEIGHT,
CAM_DIST * std::cos(camPitch) * std::cos(yaw)
};
camera.SetPosition(position + camOffset);
camera.SetTarget(position);
camera.BeginMode(); camera.BeginMode();
skybox.Draw(); skybox.Draw();
@ -205,15 +184,26 @@ int main() {
for (size_t i = 0; i < entities.size(); ++i) { for (size_t i = 0; i < entities.size(); ++i) {
Entity &e = entities[i]; Entity &e = entities[i];
if (e.type == Entity::EntityType::Eagle) {
e.position += from_magnitude_and_heading_xy(e.speed, e.heading) * dt;
} else {
e.position += from_magnitude_and_heading(e.speed, e.heading) * dt; e.position += from_magnitude_and_heading(e.speed, e.heading) * dt;
}
bool drawBox = (int)i == selectedIdx; bool drawBox = (int)i == selectedIdx;
DrawBoundedModel(*e.model, drawBox, [&e](raylib::Matrix transform) { DrawBoundedModel(*e.model, drawBox, [&e](raylib::Matrix transform) {
if (e.type == Entity::EntityType::Eagle) {
return transform
.RotateZ(raylib::Degree(0) - e.heading)
.Scale(40, 40, 40)
.Translate(e.position);
} else {
return transform return transform
.RotateY(e.heading) .RotateY(e.heading)
.Scale(40, 40, 40) .Scale(40, 40, 40)
.Translate(e.position); .Translate(e.position);
}
}); });
} }