Add 3D eagle movement

master
John Montagu, the 4th Earl of Sandvich 2026-03-01 23:39:40 -08:00
parent c794912def
commit 98087e3642
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
1 changed files with 27 additions and 2 deletions

View File

@ -53,6 +53,7 @@ struct Entity {
raylib::Vector3 position = raylib::Vector3::Zero(); raylib::Vector3 position = raylib::Vector3::Zero();
float speed = 0; float speed = 0;
raylib::Degree heading = 0; raylib::Degree heading = 0;
raylib::Degree pitch = 0;
raylib::Model *model; raylib::Model *model;
enum EntityType { enum EntityType {
Penguin = 0, Penguin = 0,
@ -89,6 +90,19 @@ raylib::Vector3 from_magnitude_and_heading_xy(
}; };
} }
raylib::Vector3 from_magnitude_heading_pitch(
float magnitude,
raylib::Degree heading,
raylib::Degree pitch
) {
float cos_pitch = std::cos(pitch.RadianValue());
return raylib::Vector3 {
magnitude * cos_pitch * std::sin(heading.RadianValue()),
magnitude * std::sin(pitch.RadianValue()),
magnitude * cos_pitch * std::cos(heading.RadianValue())
};
}
const float ACCELERATION = 100.0f; const float ACCELERATION = 100.0f;
const raylib::Degree ANGULAR_VELOCITY = raylib::Degree(180.0f); const raylib::Degree ANGULAR_VELOCITY = raylib::Degree(180.0f);
@ -178,8 +192,18 @@ int main() {
selected.speed = 0.0f; selected.speed = 0.0f;
} }
if (selected.type == Entity::EntityType::Eagle) {
if (IsKeyDown(KEY_Q)) {
selected.pitch += dt * ANGULAR_VELOCITY;
}
if (IsKeyDown(KEY_Z)) {
selected.pitch -= dt * ANGULAR_VELOCITY;
}
}
for (Entity &e : entities) { for (Entity &e : entities) {
e.heading = angle_normalize(e.heading); e.heading = angle_normalize(e.heading);
e.pitch = angle_normalize(e.pitch);
} }
camera.BeginMode(); camera.BeginMode();
@ -190,7 +214,7 @@ 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) { if (e.type == Entity::EntityType::Eagle) {
e.position += from_magnitude_and_heading_xy(e.speed, e.heading) * dt; e.position += from_magnitude_heading_pitch(e.speed, e.heading, e.pitch) * dt;
} else { } else {
e.position += from_magnitude_and_heading(e.speed, e.heading) * dt; e.position += from_magnitude_and_heading(e.speed, e.heading) * dt;
} }
@ -200,7 +224,8 @@ int main() {
DrawBoundedModel(*e.model, drawBox, [&e](raylib::Matrix transform) { DrawBoundedModel(*e.model, drawBox, [&e](raylib::Matrix transform) {
if (e.type == Entity::EntityType::Eagle) { if (e.type == Entity::EntityType::Eagle) {
return transform return transform
.RotateZ(raylib::Degree(0) - e.heading) .RotateX(raylib::Degree(0) - e.pitch)
.RotateY(e.heading)
.Scale(40, 40, 40) .Scale(40, 40, 40)
.Translate(e.position); .Translate(e.position);
} else { } else {