Add 3D eagle movement
parent
c794912def
commit
98087e3642
29
as4/as4.cpp
29
as4/as4.cpp
|
|
@ -53,6 +53,7 @@ struct Entity {
|
|||
raylib::Vector3 position = raylib::Vector3::Zero();
|
||||
float speed = 0;
|
||||
raylib::Degree heading = 0;
|
||||
raylib::Degree pitch = 0;
|
||||
raylib::Model *model;
|
||||
enum EntityType {
|
||||
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 raylib::Degree ANGULAR_VELOCITY = raylib::Degree(180.0f);
|
||||
|
||||
|
|
@ -178,8 +192,18 @@ int main() {
|
|||
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) {
|
||||
e.heading = angle_normalize(e.heading);
|
||||
e.pitch = angle_normalize(e.pitch);
|
||||
}
|
||||
|
||||
camera.BeginMode();
|
||||
|
|
@ -190,7 +214,7 @@ int main() {
|
|||
for (size_t i = 0; i < entities.size(); ++i) {
|
||||
Entity &e = entities[i];
|
||||
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 {
|
||||
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) {
|
||||
if (e.type == Entity::EntityType::Eagle) {
|
||||
return transform
|
||||
.RotateZ(raylib::Degree(0) - e.heading)
|
||||
.RotateX(raylib::Degree(0) - e.pitch)
|
||||
.RotateY(e.heading)
|
||||
.Scale(40, 40, 40)
|
||||
.Translate(e.position);
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Reference in New Issue