Add acceleration
parent
e69398d87f
commit
7985ce395c
57
as3/as3.cpp
57
as3/as3.cpp
|
|
@ -6,6 +6,7 @@
|
||||||
#include "Model.hpp"
|
#include "Model.hpp"
|
||||||
#include "raylib.h"
|
#include "raylib.h"
|
||||||
#include <raylib-cpp.hpp>
|
#include <raylib-cpp.hpp>
|
||||||
|
#include <cmath>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#define SKYBOX_IMPLEMENTATION
|
#define SKYBOX_IMPLEMENTATION
|
||||||
|
|
@ -44,7 +45,6 @@ int main() {
|
||||||
raylib::AudioDevice audio;
|
raylib::AudioDevice audio;
|
||||||
|
|
||||||
raylib::Model penguin("models/penguin.glb");
|
raylib::Model penguin("models/penguin.glb");
|
||||||
raylib::Model eagle("models/eagle.glb");
|
|
||||||
|
|
||||||
raylib::Camera3D camera(
|
raylib::Camera3D camera(
|
||||||
{ 0, 120, 500 },
|
{ 0, 120, 500 },
|
||||||
|
|
@ -56,35 +56,68 @@ int main() {
|
||||||
cs381::SkyBox skybox("textures/skybox.png");
|
cs381::SkyBox skybox("textures/skybox.png");
|
||||||
|
|
||||||
raylib::Vector3 position = { 0, 0, 0 };
|
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
|
window.SetTargetFPS(60); // save cpu cycles
|
||||||
|
|
||||||
while (!window.ShouldClose()) {
|
while (!window.ShouldClose()) {
|
||||||
window.BeginDrawing();
|
window.BeginDrawing();
|
||||||
|
|
||||||
window.ClearBackground(raylib::Color::Gray());
|
window.ClearBackground(raylib::Color::Gray());
|
||||||
|
|
||||||
float dt = window.GetFrameTime();
|
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();
|
camera.BeginMode();
|
||||||
|
|
||||||
skybox.Draw();
|
skybox.Draw();
|
||||||
|
|
||||||
// in addition to the required transforms, the models will rotate 10
|
ground.Draw({ 0, 0, 0 }, 1.0f, raylib::Color::White());
|
||||||
// degrees per second (extra credit), and the penguin is scaled by 30
|
|
||||||
// eagle scaled by 10
|
|
||||||
|
|
||||||
// Draw one eagle located at (0, 0, 0), default scale, and with default
|
DrawBoundedModel(penguin, [&position, &heading](raylib::Matrix transform) {
|
||||||
// orientation (10 points).
|
|
||||||
DrawBoundedModel(penguin, [&position](raylib::Matrix transform) {
|
|
||||||
return transform
|
return transform
|
||||||
|
.RotateY(heading)
|
||||||
|
.Scale(40, 40, 40)
|
||||||
.Translate(position);
|
.Translate(position);
|
||||||
});
|
});
|
||||||
|
|
||||||
camera.EndMode();
|
camera.EndMode();
|
||||||
|
|
||||||
window.EndDrawing();
|
window.EndDrawing();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue