Fix clock being real and not user clock
This commit is contained in:
parent
2f5c423cc7
commit
0bae04d802
4 changed files with 34 additions and 15 deletions
|
@ -12,9 +12,14 @@ int main(int argc, char** argv) {
|
|||
GlutRender& render = GlutRender::get_instance();
|
||||
render.init(&argc, argv, 640, 480, "Bouncing stuff");
|
||||
|
||||
Ball ball(Point(0, 0, 10), 0, .25, 1, 1);
|
||||
Ball ball(Point(0, 10, 0), 0.25, 0.25, 0, 1, 1);
|
||||
|
||||
Cuboid bbox = ball.get_surface()->max_bounding_box();
|
||||
printf("%.2lf %.2lf %.2lf | %.2lf %.2lf %.2lf\n",
|
||||
bbox.low(0), bbox.low(1), bbox.low(2),
|
||||
bbox.high(0), bbox.high(1), bbox.high(2));
|
||||
render.add_surface(ball.get_surface(),
|
||||
Cuboid(Point(-2, -2, -2), Point(2, 2, 2)));
|
||||
ball.get_surface()->max_bounding_box());
|
||||
|
||||
render.set_idle_func(periodic_update);
|
||||
init_periodic_static(&ball);
|
||||
|
|
|
@ -1,24 +1,38 @@
|
|||
#include "periodic_updates.hpp"
|
||||
|
||||
#include <ctime>
|
||||
#include <chrono>
|
||||
#include <GL/glut.h>
|
||||
|
||||
static Ball* _ball = nullptr;
|
||||
static clock_t _last_time = 0;
|
||||
|
||||
double time_of_clocks(clock_t time) {
|
||||
return ((double)time) / ((double)CLOCKS_PER_SEC);
|
||||
}
|
||||
static std::chrono::time_point<std::chrono::steady_clock>
|
||||
_last_time, _init_clocks;
|
||||
|
||||
void init_periodic_static(Ball* ball) {
|
||||
_last_time = clock();
|
||||
_last_time = std::chrono::steady_clock::now();
|
||||
_init_clocks = std::chrono::steady_clock::now();
|
||||
_ball = ball;
|
||||
}
|
||||
|
||||
void periodic_update() {
|
||||
clock_t now = clock();
|
||||
_ball->update_pos(time_of_clocks(now - _last_time));
|
||||
_last_time = now;
|
||||
double ellapsed_double(
|
||||
std::chrono::time_point<std::chrono::steady_clock> beg,
|
||||
std::chrono::time_point<std::chrono::steady_clock> end)
|
||||
{
|
||||
std::chrono::duration<double> ellapsed_db = end - beg;
|
||||
return ellapsed_db.count();
|
||||
}
|
||||
|
||||
void periodic_update() {
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
_ball->update_pos(ellapsed_double(_last_time, now));
|
||||
|
||||
fprintf(stderr, "dt = %lf, tot = %lf; center: %.2lf, %.2lf, %.2lf\n",
|
||||
ellapsed_double(_last_time, now),
|
||||
ellapsed_double(_init_clocks, now),
|
||||
_ball->getCenter().x,
|
||||
_ball->getCenter().y,
|
||||
_ball->getCenter().z);
|
||||
|
||||
_last_time = now;
|
||||
glutPostRedisplay();
|
||||
}
|
||||
|
|
|
@ -103,8 +103,8 @@ void GlutRender::display() {
|
|||
// Camera position and orientation
|
||||
glLoadIdentity();
|
||||
//glTranslatef(1., 2., -10.);
|
||||
gluLookAt(0, 0, -10,
|
||||
0, 0, 10,
|
||||
gluLookAt(0, 5, -20,
|
||||
0, 5, 0,
|
||||
0, 1, 0);
|
||||
|
||||
for(Mesh* mesh: meshes) {
|
||||
|
|
|
@ -52,7 +52,7 @@ void Spheroid::check_vertical_plan_collision(double& abscissa) {
|
|||
}
|
||||
|
||||
Cuboid Spheroid::max_bounding_box() const {
|
||||
double max_radius = sqrt((3/4) * V / PI / min_p);
|
||||
double max_radius = sqrt((3./4.) * V / PI / min_p);
|
||||
double max_height = init_p;
|
||||
Point _bd1(max_radius, max_radius, max_height);
|
||||
Point _bd2(-max_radius, -max_radius, -max_height);
|
||||
|
|
Loading…
Reference in a new issue