2009-02-04 14:43:33 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2009 Daniel Chappuis *
|
|
|
|
****************************************************************************
|
|
|
|
* This file is part of ReactPhysics3D. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
2009-06-05 14:22:40 +00:00
|
|
|
* it under the terms of the GNU Lesser General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or *
|
2009-02-04 14:43:33 +00:00
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
2009-06-05 14:22:40 +00:00
|
|
|
* GNU Lesser General Public License for more details. *
|
2009-02-04 14:43:33 +00:00
|
|
|
* *
|
2009-06-05 14:22:40 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
2009-02-04 14:43:33 +00:00
|
|
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
***************************************************************************/
|
2010-06-08 21:03:35 +00:00
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "Simulation.h"
|
|
|
|
#include "ReactDemo.h"
|
2009-02-13 11:26:51 +00:00
|
|
|
#include <iostream>
|
|
|
|
|
|
|
|
// We want to use the ReactPhysics3D namespace
|
2010-06-08 21:03:35 +00:00
|
|
|
using namespace reactphysics3d;
|
|
|
|
|
|
|
|
// Constructor of the class Simulation
|
2009-07-21 20:28:27 +00:00
|
|
|
Simulation::Simulation()
|
2010-07-19 19:42:47 +00:00
|
|
|
:world(new PhysicsWorld(Vector3D(0.0, -9.8, 0.0))), engine(world, Time(0.01)), scene(this->world) { // TODO : Change the timestep here after debugging
|
2010-06-08 21:03:35 +00:00
|
|
|
simRunning = false;
|
2009-02-04 14:43:33 +00:00
|
|
|
mouseButtonPressed = false;
|
2009-02-13 11:26:51 +00:00
|
|
|
nbFrame = 0;
|
|
|
|
lastFrameTime = 0.0;
|
2010-06-08 21:03:35 +00:00
|
|
|
fps = 0.0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor of the class Simulation
|
|
|
|
Simulation::~Simulation() {
|
2009-02-13 11:26:51 +00:00
|
|
|
// Delete the physics world object
|
2010-06-08 21:03:35 +00:00
|
|
|
delete world;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to start the simulation
|
|
|
|
void Simulation::start() {
|
|
|
|
// Initialisation of the OpenGL settings for the scene
|
|
|
|
scene.init();
|
|
|
|
|
|
|
|
// Reshape the windows for the first time
|
2009-02-13 11:26:51 +00:00
|
|
|
scene.reshape(WINWIDTH, WINHEIGHT);
|
|
|
|
|
|
|
|
// Add every rigid body to the dynamic world
|
|
|
|
for (int i=0; i<context.getNbObjects(); ++i) {
|
|
|
|
world->addBody(context.getObject(i).getRigidBody());
|
2009-02-25 17:18:48 +00:00
|
|
|
}
|
2010-06-08 21:03:35 +00:00
|
|
|
|
|
|
|
// Activation of the simulation
|
2009-02-04 14:43:33 +00:00
|
|
|
simRunning = true;
|
|
|
|
|
|
|
|
// Get the current time
|
2009-02-13 11:26:51 +00:00
|
|
|
lastFrameTime = SDL_GetTicks();
|
|
|
|
|
2010-03-24 21:36:36 +00:00
|
|
|
PhysicsEngine* pEngine = &engine;
|
2009-07-21 20:28:27 +00:00
|
|
|
|
2009-02-13 11:26:51 +00:00
|
|
|
// Initialize the display time
|
2009-07-21 20:28:27 +00:00
|
|
|
pEngine->initializeDisplayTime(Time(SDL_GetTicks()/1000.0));
|
2009-02-13 11:26:51 +00:00
|
|
|
|
|
|
|
// Start the physics simulation
|
2009-11-29 14:14:58 +00:00
|
|
|
pEngine->start();
|
|
|
|
|
2010-06-08 21:03:35 +00:00
|
|
|
//double time = 1.0;
|
|
|
|
|
|
|
|
// Main loop of the simulation
|
|
|
|
while(simRunning) {
|
|
|
|
// Check if an SDL event occured and make the apropriate actions
|
2009-02-13 11:26:51 +00:00
|
|
|
checkEvents();
|
|
|
|
|
|
|
|
double time = SDL_GetTicks()/1000.0;
|
2009-11-29 14:14:58 +00:00
|
|
|
//time += 0.01;
|
|
|
|
|
|
|
|
//std::cout << "************************************************* Time : " << time << std::endl;
|
2009-02-25 17:18:48 +00:00
|
|
|
|
2009-02-13 11:26:51 +00:00
|
|
|
// Update the display time
|
2009-07-21 20:28:27 +00:00
|
|
|
pEngine->updateDisplayTime(Time(time));
|
2009-02-13 11:26:51 +00:00
|
|
|
|
|
|
|
// Update the physics
|
2010-07-19 19:42:47 +00:00
|
|
|
pEngine->update();
|
2010-06-08 21:03:35 +00:00
|
|
|
|
|
|
|
// Display the actual scene
|
2009-02-04 14:43:33 +00:00
|
|
|
scene.display(context);
|
|
|
|
|
|
|
|
// Compute the fps (framerate)
|
|
|
|
computeFps();
|
2009-11-29 14:14:58 +00:00
|
|
|
//std::cout << "FPS : " << fps << std::endl;
|
2009-02-13 11:26:51 +00:00
|
|
|
|
2009-02-25 17:18:48 +00:00
|
|
|
/*
|
2009-02-13 11:26:51 +00:00
|
|
|
BodyState state = context.getObject(0).getRigidBody()->getInterpolatedState();
|
2009-02-13 15:43:10 +00:00
|
|
|
Vector3D velocity = context.getObject(0).getRigidBody()->getInterpolatedState().getAngularVelocity();
|
2009-02-13 11:26:51 +00:00
|
|
|
//std::cout << "Velocity 0 : " << velocity.getX() << ", " << velocity.getY() << ", " << velocity.getZ() << ")" << std::endl;
|
|
|
|
double x = state.getPosition().getX();
|
|
|
|
double y = state.getPosition().getY();
|
|
|
|
double z = state.getPosition().getZ();
|
|
|
|
std::cout << "Position Cube 0 : (" << x << ", " << y << ", " << z << ")" << std::endl;
|
2009-02-13 15:43:10 +00:00
|
|
|
std::cout << "angular velocity 0 : " << velocity.length() << std::endl;;
|
2009-02-04 14:43:33 +00:00
|
|
|
|
2009-02-13 11:26:51 +00:00
|
|
|
BodyState state1 = context.getObject(1).getRigidBody()->getInterpolatedState();
|
2009-02-13 15:43:10 +00:00
|
|
|
Vector3D velocity1 = context.getObject(1).getRigidBody()->getInterpolatedState().getAngularVelocity();
|
2009-02-13 11:26:51 +00:00
|
|
|
//std::cout << "Velocity 1 : " << velocity1.getX() << ", " << velocity1.getY() << ", " << velocity1.getZ() << ")" << std::endl;
|
|
|
|
double x1 = state1.getPosition().getX();
|
|
|
|
double y1 = state1.getPosition().getY();
|
|
|
|
double z1 = state1.getPosition().getZ();
|
|
|
|
std::cout << "Position Cube 1 : (" << x1 << ", " << y1 << ", " << z1 << ")" << std::endl;
|
2009-02-13 15:43:10 +00:00
|
|
|
std::cout << "angular velocity 1 : " << velocity1.length() << std::endl;
|
2009-02-13 11:26:51 +00:00
|
|
|
|
|
|
|
BodyState state2 = context.getObject(2).getRigidBody()->getInterpolatedState();
|
2009-02-13 15:43:10 +00:00
|
|
|
Quaternion velocity2 = context.getObject(2).getRigidBody()->getInterpolatedState().getOrientation();
|
2009-02-13 11:26:51 +00:00
|
|
|
//std::cout << "Velocity 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ")" << std::endl;
|
|
|
|
double x2 = state2.getPosition().getX();
|
|
|
|
double y2 = state2.getPosition().getY();
|
|
|
|
double z2 = state2.getPosition().getZ();
|
|
|
|
std::cout << "Position Cube 2: (" << x2 << ", " << y2 << ", " << z2 << ")" << std::endl;
|
2010-06-08 21:03:35 +00:00
|
|
|
std::cout << "quaternion orientation 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ", " << velocity2.getW() << ")" << std::endl;;
|
2009-02-25 17:18:48 +00:00
|
|
|
*/
|
2009-11-29 14:14:58 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
double a;
|
|
|
|
if (time > 5.0) {
|
|
|
|
std::cin >> a;
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
2010-06-08 21:03:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This method checks if an events occur and call the apropriate method
|
|
|
|
void Simulation::checkEvents() {
|
2009-02-04 14:43:33 +00:00
|
|
|
SDL_Event event; // An SDL event
|
|
|
|
|
|
|
|
// Zoom of the outside camera
|
|
|
|
if (SDL_GetKeyState(NULL)[SDLK_UP]) {
|
|
|
|
scene.getOutSideCamera().decreaseDistance(fps);
|
|
|
|
}
|
|
|
|
else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) {
|
|
|
|
scene.getOutSideCamera().increaseDistance(fps);
|
2010-06-08 21:03:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check in the stack of events
|
|
|
|
while(SDL_PollEvent(&event)) {
|
|
|
|
// Check an event
|
|
|
|
switch(event.type) {
|
|
|
|
// An QUIT event occur
|
|
|
|
case SDL_QUIT: simRunning = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// A keyboard key has been pushed
|
|
|
|
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
|
|
|
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
|
|
|
simRunning = false;
|
|
|
|
break;
|
|
|
|
|
|
|
|
// The size of the windows changed then we reshape the windows
|
|
|
|
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
|
|
|
|
break;
|
|
|
|
|
|
|
|
// If the mouse moved
|
2009-02-04 14:43:33 +00:00
|
|
|
case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
|
|
|
|
// Rotation of the outSideCamera
|
2009-02-13 11:26:51 +00:00
|
|
|
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, fps);
|
|
|
|
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, fps);
|
2010-06-08 21:03:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-02-04 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compute the framerate (fps) of the application
|
|
|
|
void Simulation::computeFps() {
|
|
|
|
|
2009-02-13 11:26:51 +00:00
|
|
|
// Increment the number of frame in the last second
|
|
|
|
nbFrame++;
|
|
|
|
|
|
|
|
// Get the current time
|
|
|
|
double currentTime = SDL_GetTicks();
|
2009-02-04 14:43:33 +00:00
|
|
|
|
2009-02-13 11:26:51 +00:00
|
|
|
// Compute the framerate
|
|
|
|
if (currentTime - lastFrameTime > 1000.0) {
|
|
|
|
fps = nbFrame * 1000.0/(currentTime-lastFrameTime);
|
|
|
|
lastFrameTime = currentTime;
|
|
|
|
nbFrame = 0;
|
|
|
|
}
|
2010-06-08 21:03:35 +00:00
|
|
|
}
|