diff --git a/level.cc b/level.cc index f2d922f..17c64d6 100644 --- a/level.cc +++ b/level.cc @@ -78,6 +78,9 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) { glm::vec3(0.0f, 0.0f, 0.0f), shader); objects.push_back(terrainObject); cameraCenter = &objects[0]; + + //addTerrainPhysic + physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap()); } void Level::render() { @@ -86,7 +89,7 @@ void Level::render() { } } -void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool dPressed, bool sPressed) { +void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed) { // rotate bunny //cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f)); // Ignore first two mouse updates, because they are incorrect @@ -98,14 +101,23 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre camera.updateRotation(mouseDelta/100.0f); } - if(wPressed) - { - //physics.rollForward(camera.getRotation); + if(wPressed){ + physics.rollForward(camera.getVector(),1.0f); + } + if(aPressed) { + physics.rollLeft(camera.getVector(),1.0f); + } + if(sPressed) { + physics.rollBack(camera.getVector(),1.0f); + } + if(dPressed){ + physics.rollRight(camera.getVector(),1.0f); } physics.takeUpdateStep(runTime); objects[0].setPosition(physics.getPos(0)); + objects[0].setRotation(physics.getRotation(0)); lights[2].setPosition(physics.getPos(0)); } diff --git a/main.cc b/main.cc index 90e764d..ab84706 100644 --- a/main.cc +++ b/main.cc @@ -124,15 +124,16 @@ int main( int argc, char *argv[] ) 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()- startTimeInSeconds; + 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); + if (showNextFPS <= now) { std::stringstream sstream (std::stringstream::in | std::stringstream::out); sstream << std::setprecision(1) << std::fixed @@ -160,6 +161,7 @@ int main( int argc, char *argv[] ) glfwSwapBuffers(app.getGraphics()->getWindow()); glfwPollEvents(); frameCount++; + } // Check if the window was closed while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) ); diff --git a/physics.cc b/physics.cc index d16d490..b9d132f 100644 --- a/physics.cc +++ b/physics.cc @@ -32,30 +32,41 @@ void Physics::init() void Physics::takeUpdateStep(float timeDiff) { + world->stepSimulation(timeDiff); } void Physics::addTerrain(int width, int length, float** heightData) { + float* heightfield = new float[width * length]; int highest = -999999, j = 0, i = 0; for (i = 0; i < width; i++) + { for (j = 0; j < length; j++) { - heightfield[j*length+i] = heightData[i][j]; + heightfield[i*length+j] = heightData[j][i]; + + if (heightData[j][i] > highest) + highest = heightData[j][i]; + } + } + + /* + heightfield[j*length+i] = heightData[i][j]; if (heightData[i][j] > highest) highest = heightData[i][j]; - } + */ - btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(width,length,heightData,highest,1,true,false); + btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false); btRigidBody* tBody = new btRigidBody(0,new btDefaultMotionState(),terrianShape); - tBody->getWorldTransform().setOrigin(btVector3(0,highest/2,0)); + tBody->getWorldTransform().setOrigin(btVector3(0,5*highest/10,0)); //tBody->getWoorldTransform().setRotation(btQuaternion(0,0,0,1)); terrainBody = tBody; - + world->addRigidBody(terrainBody); /* @@ -101,11 +112,14 @@ void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsign btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); playerBall = new btRigidBody(info); - + + playerBall->setDamping(0.1f,0.3f); + world->addRigidBody(playerBall); bodies.push_back(playerBall); - + + playerBall->setSleepingThresholds(0,0); if(bodies.size() == indice) throw std::invalid_argument( "Bodies out of Sync" ); @@ -127,13 +141,15 @@ void Physics::addSphere(float rad, float x, float y, float z, float mass, unsign btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(x,y,z))); btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); + //info. btRigidBody* body = new btRigidBody(info); world->addRigidBody(body); bodies.push_back(body); - + + body->setSleepingThresholds(0,0); if(bodies.size() == indice) throw std::invalid_argument( "Bodies out of Sync" ); @@ -158,16 +174,30 @@ glm::mat4 Physics::getRotation(int i) return matrix; } -void Physics::rollForward(glm::vec3 camPos) +void Physics::rollForward(glm::vec3 camPos,float strength) { - btVector3 pos(camPos.x,camPos.y,camPos.z); - pos -= playerBody->getCenterOfMassPosition(); - pos.cross(btVector3(0,1,0)); + btVector3 pos(camPos.x,0,camPos.z); + pos = btCross(pos,btVector3(0,1,0)); playerBall->applyTorque(pos); +} -/* glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera; - saveVector = glm::cross(glm::vec3(0,1,0),saveVector); - playerBall->applyTorque(btVector3(saveVector[0],saveVector[1],saveVector[2]));*/ +void Physics::rollBack(glm::vec3 camPos,float strength) +{ + btVector3 pos(camPos.x,0,camPos.z); + pos = btCross(btVector3(0,1,0),pos); + playerBall->applyTorque(pos); +} + +void Physics::rollLeft(glm::vec3 camPos,float strength) +{ + btVector3 pos(camPos.x,0,camPos.z); + playerBall->applyTorque(pos); +} + +void Physics::rollRight(glm::vec3 camPos,float strength) +{ + btVector3 pos(camPos.x,0,camPos.z); + playerBall->applyTorque(-pos); } /* diff --git a/physics.hh b/physics.hh index c2d7774..42abad2 100644 --- a/physics.hh +++ b/physics.hh @@ -37,6 +37,9 @@ class Physics { void init(); void takeUpdateStep(float timeDiff); void rollForward(glm::vec3 camPos, float strength); + void rollLeft(glm::vec3 camPos, float strength); + void rollRight(glm::vec3 camPos, float strength); + void rollBack(glm::vec3 camPos, float strength); glm::vec3 getPos(int i); glm::mat4 getRotation(int i); void rollForward(glm::vec3 camPos);