Add acceleration

master
John Montagu, the 4th Earl of Sandvich 2026-02-22 21:57:27 -08:00
parent e69398d87f
commit 7985ce395c
Signed by: sandvich
GPG Key ID: 9A39BE37E602B22D
1 changed files with 45 additions and 12 deletions

View File

@ -6,6 +6,7 @@
#include "Model.hpp"
#include "raylib.h"
#include <raylib-cpp.hpp>
#include <cmath>
#include <iostream>
#define SKYBOX_IMPLEMENTATION
@ -44,7 +45,6 @@ int main() {
raylib::AudioDevice audio;
raylib::Model penguin("models/penguin.glb");
raylib::Model eagle("models/eagle.glb");
raylib::Camera3D camera(
{ 0, 120, 500 },
@ -56,35 +56,68 @@ int main() {
cs381::SkyBox skybox("textures/skybox.png");
raylib::Vector3 position = { 0, 0, 0 };
raylib::Vector3 velocity = { 5, 0, 0 };
raylib::Vector3 velocity = { 0, 0, 0 };
float heading = 0.0f;
float speed = 0.0f;
// units/s
const float ACCELERATION = 100.0f;
// in radians
const float ANGULAR_VELOCITY = 3.14f;
window.SetTargetFPS(60); // save cpu cycles
while (!window.ShouldClose()) {
window.BeginDrawing();
window.ClearBackground(raylib::Color::Gray());
float dt = window.GetFrameTime();
position += velocity * dt;
position += velocity * dt * 0.5f;
if (IsKeyDown(KEY_W)) {
speed += ACCELERATION * dt;
}
if (IsKeyDown(KEY_S)) {
speed -= ACCELERATION * dt;
}
if (IsKeyDown(KEY_A)) {
heading += ANGULAR_VELOCITY * dt;
}
if (IsKeyDown(KEY_D)) {
heading -= ANGULAR_VELOCITY * dt;
}
if (IsKeyDown(KEY_SPACE)) {
speed = 0.0f;
}
velocity = raylib::Vector3 {
std::sin(heading) * speed,
0.0f,
std::cos(heading) * speed
};
// ds = 1/2 * (v0 + v1) * dt
position += velocity * dt * 0.5f;
camera.BeginMode();
skybox.Draw();
// in addition to the required transforms, the models will rotate 10
// degrees per second (extra credit), and the penguin is scaled by 30
// eagle scaled by 10
ground.Draw({ 0, 0, 0 }, 1.0f, raylib::Color::White());
// Draw one eagle located at (0, 0, 0), default scale, and with default
// orientation (10 points).
DrawBoundedModel(penguin, [&position](raylib::Matrix transform) {
DrawBoundedModel(penguin, [&position, &heading](raylib::Matrix transform) {
return transform
.RotateY(heading)
.Scale(40, 40, 40)
.Translate(position);
});
camera.EndMode();
window.EndDrawing();
}