From 477da054646ed618077e0f3475123f92a295807b Mon Sep 17 00:00:00 2001 From: Jasper Date: Thu, 29 Jan 2015 14:10:28 +0100 Subject: [PATCH] implimented a rough version of a physics driven camera --- graphics.cc | 2 +- level.cc | 4 ++-- physics.cc | 28 ++++++++++++++++------------ 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/graphics.cc b/graphics.cc index 5384fc4..b985123 100644 --- a/graphics.cc +++ b/graphics.cc @@ -213,7 +213,7 @@ void Graphics::resize(glm::uvec2 windowSize) { glm::mat4 Graphics::buildViewMatrix(Level* level) { //construct lookAt (cameraPosition = cameraCenter + cameraVector) - //return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); + return glm::lookAt(level->getCamera()->getPosition(), level->getCamera()->getPosition() + level->getCamera()->getDirection(), glm::vec3(0.0f, 1.0f, 0.0f)); return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()), level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f)); diff --git a/level.cc b/level.cc index 42ef7e5..d5685f6 100644 --- a/level.cc +++ b/level.cc @@ -457,12 +457,12 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre else { mouseDelta.x = -mouseDelta.x; camera.updateRotation(mouseDelta/100.0f); - physics.updateCameraPos(mouseDelta, 0.01f); + physics.updateCameraPos(mouseDelta, 100); camera.setPosition(physics.getCameraPosition()); camera.setDirection(physics.getCameraToPlayer()); } - + strength = 50; if(wPressed){ physics.rollForward(camera.getVector(),strength); } diff --git a/physics.cc b/physics.cc index 2a5d4b3..7c9f521 100644 --- a/physics.cc +++ b/physics.cc @@ -37,7 +37,7 @@ void Physics::takeUpdateStep(float timeDiff) btVector3 dir = cameraBody->getCenterOfMassPosition() - position; - cameraBody->applyCentralForce(dir*cameraBody->getInvMass());//scale the force by camera mass + cameraBody->applyCentralForce(-dir*cameraBody->getInvMass()*10);//scale the force by camera mass cameraBody->applyCentralForce(btVector3(0,1,0)); //applies a force to the camera keeping it in a correct position } @@ -80,7 +80,7 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia); //next we process all data for the rigid body into info info.m_friction = friction*2; //here we modify the friction and restitution (bounciness) of the object - info.m_restitution = 0.2f; + info.m_restitution = 0.8f; playerBall = new btRigidBody(info); //finally we create the rigid body using the info @@ -289,11 +289,11 @@ void Physics::addCamera() btVector3 inertia(0,0,0); btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),playerBall->getCenterOfMassPosition()+btVector3(5,5,5))); - btRigidBody::btRigidBodyConstructionInfo info(1/(playerBall->getInvMass()/100),motion,sphere,inertia); + btRigidBody::btRigidBodyConstructionInfo info(1,motion,sphere,inertia); cameraBody = new btRigidBody(info); - cameraBody->setDamping(1,0.5); + cameraBody->setDamping(0.8,0.5); world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision); @@ -343,13 +343,13 @@ glm::mat4 Physics::getRotation(int i) void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength) { - btVector3 change = playerBall->getCenterOfMassPosition()-cameraBody->getCenterOfMassPosition();; + btVector3 change = playerBall->getCenterOfMassPosition()-cameraBody->getCenterOfMassPosition(); change.setY(0); change.normalize(); - change *= mouseMovement.x; + change *= mouseMovement.y; change = btCross(btVector3(0,1,0),change); - change.setY(mouseMovement.y); - change*=strength; + change.setY(mouseMovement.x); + change*= strength; cameraBody->applyCentralForce(change); @@ -357,7 +357,8 @@ void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength) void Physics::rollForward(glm::vec3 camPos,float strength) { - btVector3 pos(camPos.x,0,camPos.z); + btVector3 pos = cameraBody->getCenterOfMassPosition() - playerBall->getCenterOfMassPosition(); + pos.setY(0); pos.normalize(); pos = btCross(pos,btVector3(0,1,0)); pos *= strength; @@ -366,7 +367,8 @@ void Physics::rollForward(glm::vec3 camPos,float strength) void Physics::rollBack(glm::vec3 camPos,float strength) { - btVector3 pos(camPos.x,0,camPos.z); + btVector3 pos = cameraBody->getCenterOfMassPosition() - playerBall->getCenterOfMassPosition(); + pos.setY(0); pos.normalize(); pos = btCross(btVector3(0,1,0),pos); pos *= strength; @@ -375,7 +377,8 @@ void Physics::rollBack(glm::vec3 camPos,float strength) void Physics::rollLeft(glm::vec3 camPos,float strength) { - btVector3 pos(camPos.x,0,camPos.z); + btVector3 pos = cameraBody->getCenterOfMassPosition() - playerBall->getCenterOfMassPosition(); + pos.setY(0); pos.normalize(); pos *= strength; playerBall->applyTorque(pos); @@ -383,7 +386,8 @@ void Physics::rollLeft(glm::vec3 camPos,float strength) void Physics::rollRight(glm::vec3 camPos,float strength) { - btVector3 pos(camPos.x,0,camPos.z); + btVector3 pos = cameraBody->getCenterOfMassPosition() - playerBall->getCenterOfMassPosition(); + pos.setY(0); pos.normalize(); pos *= strength; playerBall->applyTorque(-pos);