From cac4672c7c84347d8cda9716d028a878079ab358 Mon Sep 17 00:00:00 2001 From: Jasper Date: Mon, 17 Nov 2014 12:57:16 +0100 Subject: [PATCH] Implimented a basic version of Physics into level with a ball colliding with a static plain. --- level.cc | 27 ++++++++++++++++++++++----- level.hh | 4 +++- main.cc | 19 +++++++++++++++---- physics.cc | 42 ++++++++++++++++++++++++++++-------------- physics.hh | 5 ++++- 5 files changed, 72 insertions(+), 25 deletions(-) diff --git a/level.cc b/level.cc index 8832776..fda5982 100644 --- a/level.cc +++ b/level.cc @@ -1,5 +1,7 @@ #include "level.hh" + + Level::Level(std::string filePath){ this->filePath = filePath; this->terrain = Terrain(filePath + "/terrain"); @@ -12,6 +14,10 @@ Level::~Level() { } void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { + + this->physics = Physics(); + this->physics.init(); + // currently hard coded should later read this stuff out of a file this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f); // load the geometry of the stanford bunny and build a VAO: @@ -19,16 +25,21 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { // load a texture: Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f); //Create object - Object object = Object(model, material, glm::vec3(0.0f, 1.0f, -2.0f), + Object object = Object(model, material, glm::vec3(0.0f, 5.0f, 0.0f), glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), shader); + //add player to phy + this->physics.addPlayer(0.75f,0.0f,5.0f,0.0f,1.0f,0); + + physics.addStaticGroundPlane(); + //set lighting parameters ambientLight = glm::vec3(1.0f, 1.0f, 1.0f); - Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); + Light light = Light(glm::vec3(-10.0f, 20.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); lights.push_back(light); - Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); + Light light2 = Light(glm::vec3(10.0f, 20.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f); lights.push_back(light2); @@ -53,7 +64,7 @@ void Level::render() { } } -void Level::update(float runTime, glm::vec2 mouseDelta) { +void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool dPressed, bool sPressed) { // rotate bunny //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); // Ignore first two mouse updates, because they are incorrect @@ -63,7 +74,13 @@ void Level::update(float runTime, glm::vec2 mouseDelta) { } else { camera.updateRotation(mouseDelta/100.0f); - } + } + + physics.takeUpdateStep(runTime); + + objects[0].setPosition(physics.getPos(0)); + + } glm::vec3 Level::getAmbientLight() { diff --git a/level.hh b/level.hh index 8784be2..784b4e2 100644 --- a/level.hh +++ b/level.hh @@ -8,6 +8,7 @@ #include "terrain.hh" #include "material.hh" #include "camera.hh" +#include "physics.hh" class Level { public: @@ -15,7 +16,7 @@ class Level { Level(); ~Level(); void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning - void update(float runTime, glm::vec2 mouseDelta); + void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed, bool dPressed, bool sPressed); void render(); glm::vec3 getAmbientLight(); std::vector getLights(); @@ -27,6 +28,7 @@ class Level { std::vector lights; glm::vec3 ambientLight; Object* cameraCenter; + Physics physics; Camera camera; Terrain terrain; }; diff --git a/main.cc b/main.cc index ad8a43a..b34583b 100644 --- a/main.cc +++ b/main.cc @@ -119,10 +119,18 @@ int main( int argc, char *argv[] ) const double FPSdelay = 2.0; double startTimeInSeconds = glfwGetTime(); double showNextFPS = startTimeInSeconds + FPSdelay; + + double lastUpdate=0.0f; + + int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W); + int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A); + int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S); + int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D); do { - double now = glfwGetTime(); + double now = glfwGetTime()- startTimeInSeconds; + if (showNextFPS <= now) { std::stringstream sstream (std::stringstream::in | std::stringstream::out); sstream << std::setprecision(1) << std::fixed @@ -136,10 +144,13 @@ int main( int argc, char *argv[] ) glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos); glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2); - app.getLevel()->update(now - startTimeInSeconds, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, - (float)xpos-app.getGraphics()->getWindowSize().x/2)); + app.getLevel()->update(now - lastUpdate, + glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2, + (float)xpos-app.getGraphics()->getWindowSize().x/2), + stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS); + lastUpdate = now; app.getGraphics()->render(app.getLevel(), app.getShader()); - + openGLCriticalError(); // MacOS X will not swap correctly is another FBO is bound: diff --git a/physics.cc b/physics.cc index 58d7d0f..d270266 100644 --- a/physics.cc +++ b/physics.cc @@ -1,7 +1,5 @@ #include "physics.hh" -#include - btDynamicsWorld* world; //contains physical attributes of the world. btDispatcher* dispatcher; // @@ -12,8 +10,17 @@ btConstraintSolver* solver; //solver for forces and impulses. std::vector bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process. btRigidBody* playerBall; btRigidBody* terrainBody; +btRigidBody* staticGroundBody; -void init() + +Physics::Physics() { + //init(); +} + +Physics::~Physics() { +} + +void Physics::init() { colConfig = new btDefaultCollisionConfiguration(); dispatcher = new btCollisionDispatcher(colConfig); @@ -21,17 +28,15 @@ void init() solver = new btSequentialImpulseConstraintSolver(); world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig); - world->setGravity(btVector3(0,-10,-0)); - - + world->setGravity(btVector3(0,-10,-0)); } -void takeUpdateStep(float timeDiff) +void Physics::takeUpdateStep(float timeDiff) { world->stepSimulation(timeDiff); } -void addTerrain(int width, int length, float** heightData) +void Physics::addTerrain(int width, int length, float** heightData) { float* heightfield = new float[width * length]; int highest = -999999, j = 0, i = 0; @@ -70,7 +75,17 @@ void addTerrain(int width, int length, float** heightData) } -void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice) +void Physics::addStaticGroundPlane() +{ + btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0); + btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0))); + btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0)); + staticGroundBody = new btRigidBody(groundRigidBodyCI); + + world->addRigidBody(staticGroundBody); +} + +void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsigned indice) { if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); @@ -98,7 +113,7 @@ void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice } -void addSphere(float rad, float x, float y, float z, float mass, unsigned indice) +void Physics::addSphere(float rad, float x, float y, float z, float mass, unsigned indice) { if(bodies.size() != indice) throw std::invalid_argument( "Bodies out of Sync" ); @@ -126,26 +141,25 @@ void addSphere(float rad, float x, float y, float z, float mass, unsigned indice } -glm::vec3 getPos(int i) +glm::vec3 Physics::getPos(int i) { btVector3 origin = bodies[i]->getCenterOfMassPosition(); glm::vec3 save(origin.getX(),origin.getY(),origin.getZ()); return save; } -glm::mat4 getRotation(int i) +glm::mat4 Physics::getRotation(int i) { btQuaternion quat = bodies[i]->getOrientation(); glm::mat4 matrix = glm::rotate( - matrix, quat.getAngle(), glm::vec3(quat.getAxis().getX(), quat.getAxis().getY(), quat.getAxis().getZ()) ); return matrix; } -void rollForward(glm::mat3 rotCamera) +void Physics::rollForward(glm::mat3 rotCamera) { glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera; saveVector = glm::cross(glm::vec3(0,1,0),saveVector); diff --git a/physics.hh b/physics.hh index afb3b5c..c3cbaca 100644 --- a/physics.hh +++ b/physics.hh @@ -11,6 +11,7 @@ #include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h" #include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h" +#include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h" #include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY! @@ -31,7 +32,7 @@ class Physics { public: - Physics(); + Physics(); ~Physics(); void init(); void takeUpdateStep(float timeDiff); @@ -39,6 +40,7 @@ class Physics { glm::vec3 getPos(int i); glm::mat4 getRotation(int i); void rollForward(glm::mat3 rotCamera); + void addStaticGroundPlane(); void addTerrain(int width, int length, float** heightData); void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice); void addSphere(float rad, float x, float y, float z, float mass, unsigned indice); @@ -47,6 +49,7 @@ class Physics { btRigidBody* playerBody; btRigidBody* terrainBody; std::vector bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process. + btRigidBody* staticGroundBody; btDynamicsWorld* world; //contains physical attributes of the world.