Skip to content

Commit

Permalink
Cleanup timing across the engine
Browse files Browse the repository at this point in the history
- Moved time functions from WindowBase -> TimeKeeper
- WindowBase now has a `time_keeper` Property
- The Window's TimeKeeper instance is now passed to RigidBodySimulation
- Implemented interpolation in the rigid body controller
- Switched `double` to `float` for all delta time and step values (unnecessary precision)
- Removed `WindowBase::total_time()`, `WindowBase::delta_time()` etc. replaced with `time_keeper->total_elapsed_seconds()` etc.
  • Loading branch information
Kazade committed Mar 13, 2017
1 parent 9fbae53 commit db773e6
Show file tree
Hide file tree
Showing 64 changed files with 247 additions and 165 deletions.
4 changes: 2 additions & 2 deletions disabled/path_following.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Car:
return true;
}

void update(double dt) {
void update(float dt) {
set_velocity(follower_->steer_to_path());
actor()->move_to(position() + (velocity() * dt));
}
Expand Down Expand Up @@ -87,7 +87,7 @@ class PathFollowing: public kglt::Application {
return true;
}

void do_step(double dt) {
void do_step(float dt) {
if(initialized()) {
car_->update(dt);
}
Expand Down
4 changes: 2 additions & 2 deletions disabled/seek_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Dot:
return true;
}

void update(double dt) {
void update(float dt) {
set_velocity(follower_->seek(target));

actor()->move_to(position() + (velocity() * dt));
Expand Down Expand Up @@ -72,7 +72,7 @@ class GameScreen : public kglt::Screen<GameScreen> {
);
}

void do_step(double dt) {
void do_step(float dt) {
dot_->update(dt);
}

Expand Down
2 changes: 1 addition & 1 deletion samples/fleets_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class GameScene : public smlt::Scene<GameScene> {

}

void fixed_update(double dt) override {
void fixed_update(float dt) override {
Vec3 speed(-250, 0, 0.0);

Vec3 avg;
Expand Down
4 changes: 2 additions & 2 deletions samples/joypad_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ class GameScene : public smlt::Scene<GameScene> {
});


auto hat_cb = [=](smlt::HatPosition position, smlt::Hat hat, double dt) mutable {
auto hat_cb = [=](smlt::HatPosition position, smlt::Hat hat, float dt) mutable {
std::cout << "Hat: " << (int) hat << std::endl;
std::cout << "Position " << (int) position << std::endl;
};
Expand All @@ -164,7 +164,7 @@ class GameScene : public smlt::Scene<GameScene> {
}
}

void fixed_update(double dt) {
void fixed_update(float dt) {
auto actor = window->stage(stage_id_)->actor(actor_id);
actor->rotate_x_by(smlt::Degrees(rot.y * dt * 10));
actor->rotate_y_by(smlt::Degrees(rot.x * dt * 10));
Expand Down
6 changes: 3 additions & 3 deletions samples/light_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ class GameScene : public smlt::Scene<GameScene> {
}

float xpos = 0;
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_A, [&](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_A, [&](SDL_Keysym key, float dt) mutable {
xpos -= 20.0 * dt;
window->stage(stage_id_)->camera(camera_id_)->move_to_absolute(xpos, 2, 0);
window->stage(stage_id_)->camera(camera_id_)->look_at(window->stage(stage_id_)->actor(actor_id_)->absolute_position());
});
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_D, [&](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_D, [&](SDL_Keysym key, float dt) mutable {
xpos += 20.0 * dt;
window->stage(stage_id_)->camera(camera_id_)->move_to_absolute(xpos, 2, 0);
window->stage(stage_id_)->camera(camera_id_)->look_at(window->stage(stage_id_)->actor(actor_id_)->absolute_position());
});
}

void fixed_update(double dt) {
void fixed_update(float dt) {
window->stage(stage_id_)->actor(actor_id_)->rotate_x_by(smlt::Degrees(dt * 20.0));
window->stage(stage_id_)->actor(actor_id_)->rotate_y_by(smlt::Degrees(dt * 15.0));
window->stage(stage_id_)->actor(actor_id_)->rotate_z_by(smlt::Degrees(dt * 25.0));
Expand Down
8 changes: 4 additions & 4 deletions samples/physics_demo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,19 @@ class GameScene : public smlt::PhysicsScene<GameScene> {
window->enable_pipeline(pipeline_id_);


window->keyboard->key_while_pressed_connect(SDL_SCANCODE_UP, [=](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_UP, [=](SDL_Keysym key, float dt) mutable {
controller_->accelerate();
});

window->keyboard->key_while_pressed_connect(SDL_SCANCODE_DOWN, [=](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_DOWN, [=](SDL_Keysym key, float dt) mutable {
controller_->decelerate();
});

window->keyboard->key_while_pressed_connect(SDL_SCANCODE_RIGHT, [=](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_RIGHT, [=](SDL_Keysym key, float dt) mutable {
controller_->turn_left();
});

window->keyboard->key_while_pressed_connect(SDL_SCANCODE_LEFT, [=](SDL_Keysym key, double dt) mutable {
window->keyboard->key_while_pressed_connect(SDL_SCANCODE_LEFT, [=](SDL_Keysym key, float dt) mutable {
controller_->turn_right();
});
}
Expand Down
2 changes: 1 addition & 1 deletion samples/rtt_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class GameScene : public smlt::Scene<GameScene> {
).with_clear();
}

void fixed_update(double dt) {
void fixed_update(float dt) {
window->stage(cube_stage_)->actor(cube_)->rotate_y_by(Degrees(dt * 360));
window->stage(rect_stage_)->actor(rect_)->rotate_y_by(Degrees(dt * 180));
}
Expand Down
2 changes: 1 addition & 1 deletion samples/terrain_sample.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Gamescene : public smlt::Scene<Gamescene> {
window->enable_pipeline(pipeline_id_);
}

void fixed_update(double dt) override {
void fixed_update(float dt) override {
auto stage = window->stage(stage_id_);
stage->actor(terrain_actor_id_)->rotate_global_y_by(smlt::Degrees(dt * 5.0));
}
Expand Down
2 changes: 2 additions & 0 deletions simulant.files
Original file line number Diff line number Diff line change
Expand Up @@ -1548,3 +1548,5 @@ simulant/controllers/movement/smooth_follow.h
simulant/controllers/stage_node_controller.h
simulant/controllers/movement/smooth_follow.cpp
simulant/scenes/physics_scene.h
simulant/time_keeper.h
simulant/time_keeper.cpp
2 changes: 1 addition & 1 deletion simulant/animation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ void KeyFrameAnimationState::override_playing_animation_duration(const float new
current_animation_duration_ = new_duration;
}

void KeyFrameAnimationState::update(double dt) {
void KeyFrameAnimationState::update(float dt) {
if(!current_animation_){
return;
}
Expand Down
2 changes: 1 addition & 1 deletion simulant/animation.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class KeyFrameAnimationState {
void play_sequence(const std::string& name);

void override_playing_animation_duration(const float new_duration);
void update(double dt);
void update(float dt);

uint32_t current_frame() const { return current_frame_; }
uint32_t next_frame() const { return next_frame_; }
Expand Down
2 changes: 1 addition & 1 deletion simulant/application.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class Application :
bool initialized_ = false;

virtual bool do_init() = 0;
virtual void do_fixed_update(double dt) {}
virtual void do_fixed_update(float dt) {}
virtual void do_cleanup() {}

virtual bool while_key_pressed(SDL_Keysym key, double) { return false; }
Expand Down
2 changes: 1 addition & 1 deletion simulant/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ void Background::cleanup() {
manager_->window->delete_camera(camera_id_);
}

void Background::update(double dt) {
void Background::update(float dt) {
auto pass = manager_->window->stage(stage_id_)->assets->material(material_id_)->first_pass();
if(pass->texture_unit_count()) {
pass->texture_unit(0).scroll_x(x_rate_ * dt);
Expand Down
2 changes: 1 addition & 1 deletion simulant/background.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Background:

bool init() override;
void cleanup() override;
void update(double dt) override;
void update(float dt) override;

void set_horizontal_scroll_rate(float x_rate);
void set_vertical_scroll_rate(float y_rate);
Expand Down
6 changes: 3 additions & 3 deletions simulant/controllers/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,23 @@ void Controller::disable() {
is_enabled_ = false;
}

void Controller::_update_thunk(double dt) {
void Controller::_update_thunk(float dt) {
if(!is_enabled_) {
return;
}

Updateable::_update_thunk(dt);
}

void Controller::_late_update_thunk(double dt) {
void Controller::_late_update_thunk(float dt) {
if(!is_enabled_) {
return;
}

Updateable::_late_update_thunk(dt);
}

void Controller::_fixed_update_thunk(double step) {
void Controller::_fixed_update_thunk(float step) {
if(!is_enabled_) {
return;
}
Expand Down
12 changes: 6 additions & 6 deletions simulant/controllers/controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ class Controller:
void enable();
void disable();

void _update_thunk(double dt) override;
void _late_update_thunk(double dt) override;
void _fixed_update_thunk(double step) override;
void _update_thunk(float dt) override;
void _late_update_thunk(float dt) override;
void _fixed_update_thunk(float step) override;

private:
bool is_enabled_ = true;
Expand Down Expand Up @@ -111,19 +111,19 @@ class Controllable {
return ret;
}

void fixed_update_controllers(double step) {
void fixed_update_controllers(float step) {
for(auto& controller: controllers_) {
controller->_fixed_update_thunk(step);
}
}

void update_controllers(double dt) {
void update_controllers(float dt) {
for(auto& controller: controllers_) {
controller->_update_thunk(dt);
}
}

void late_update_controllers(double dt) {
void late_update_controllers(float dt) {
for(auto& controller: controllers_) {
controller->_late_update_thunk(dt);
}
Expand Down
10 changes: 5 additions & 5 deletions simulant/controllers/fly.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,19 @@ class Fly:
throw std::logic_error("Tried to attach FlyController to something which wasn't an object");
}

connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_W, [=](SDL_Keysym key, double dt) {
connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_W, [=](SDL_Keysym key, float dt) {
moving_forward_ = true;
}));

connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_S, [=](SDL_Keysym key, double dt) {
connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_S, [=](SDL_Keysym key, float dt) {
moving_backward_ = true;
}));

connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_A, [=](SDL_Keysym key, double dt) {
connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_A, [=](SDL_Keysym key, float dt) {
rotating_left_ = true;
}));

connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_D, [=](SDL_Keysym key, double dt) {
connections_.push_back(window->keyboard->key_while_pressed_connect(SDL_SCANCODE_D, [=](SDL_Keysym key, float dt) {
rotating_right_ = true;
}));

Expand All @@ -86,7 +86,7 @@ class Fly:
const std::string name() const override { return "Fly by Keyboard"; }

private:
void late_update(double dt) override {
void late_update(float dt) override {
if(moving_forward_) {
object_->move_forward_by(600.0 * dt);
}
Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/material/flowing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace smlt {
namespace controllers {
namespace material {

void Flowing::update(double dt) {
void Flowing::update(float dt) {
float scroll = -(time_ - int(time_));

auto pass = material->pass(0);
Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/material/flowing.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Flowing :

const std::string name() const override { return "Flowing Material"; }
private:
void update(double dt) override;
void update(float dt) override;
double time_ = 0.0f;
};

Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/material/warp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace smlt {
namespace controllers {
namespace material {

void Warp::update(double dt) {
void Warp::update(float dt) {
auto pass = material->pass(0);
auto& tex_unit = pass->texture_unit(0);

Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/material/warp.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Warp:
const std::string name() const { return "Warped Material"; }

private:
void update(double dt) override;
void update(float dt) override;
double time_ = 0.0f;
};

Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/movement/smooth_follow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ SmoothFollow::SmoothFollow(Controllable* controllable):

}

void SmoothFollow::late_update(double dt) {
void SmoothFollow::late_update(float dt) {
auto target = target_.lock();

if(!target) {
Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/movement/smooth_follow.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class SmoothFollow:
return "Smooth Follow";
}

void late_update(double dt) override;
void late_update(float dt) override;
void set_target(ActorPtr actor);
void set_target(ParticleSystemPtr ps);

Expand Down
2 changes: 1 addition & 1 deletion simulant/controllers/raycast_vehicle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ bool RaycastVehicle::init() {
}
}

void RaycastVehicle::fixed_update(double dt) {
void RaycastVehicle::fixed_update(float dt) {
RigidBodySimulation* sim = simulation;
if(!sim) {
return;
Expand Down
4 changes: 2 additions & 2 deletions simulant/controllers/raycast_vehicle.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ class RaycastVehicle:
float turn_force_ = 0.0f;
float drive_force_ = 0.0f;

void fixed_update(double dt) override;
void pre_update(double dt) {
void fixed_update(float dt) override;
void pre_update(float dt) {
// Before we read any input or anything, clear the forces
drive_force_ = 0.0f;
turn_force_ = 0.0f;
Expand Down
23 changes: 17 additions & 6 deletions simulant/controllers/rigid_body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ std::pair<Vec3, Vec3> calculate_bounds(const std::vector<Vec3>& vertices) {
}


RigidBodySimulation::RigidBodySimulation() {
RigidBodySimulation::RigidBodySimulation(TimeKeeper *time_keeper):
time_keeper_(time_keeper) {

scene_ = new q3Scene(1.0 / 60.0f);
scene_->SetAllowSleep(true);
scene_->SetGravity(q3Vec3(0, -9.81, 0));
Expand All @@ -94,7 +96,7 @@ void RigidBodySimulation::cleanup() {



void RigidBodySimulation::fixed_update(double dt) {
void RigidBodySimulation::fixed_update(float dt) {
scene_->Step();
}

Expand Down Expand Up @@ -386,15 +388,24 @@ void Body::move_to(const Vec3& position) {
);
}

void Body::update(double dt) {
void Body::update(float dt) {
auto sim = simulation_.lock();
if(!sim) {
return;
}

auto xform = sim->body_transform(this);
object_->move_to(xform.first);
object_->rotate_to(xform.second);
auto prev_state = last_state_;
auto next_state = sim->body_transform(this);

float t = sim->time_keeper_->fixed_step_remainder();

auto new_pos = prev_state.first + ((next_state.first - prev_state.first) * t);
auto new_rot = prev_state.second.slerp(next_state.second, t);

object_->move_to(new_pos);
object_->rotate_to(new_rot);

last_state_ = next_state;
}

void Body::build_collider(ColliderType collider) {
Expand Down
Loading

0 comments on commit db773e6

Please sign in to comment.