implimented a rough version of a physics driven camera
This commit is contained in:
parent
b21b9e3636
commit
477da05464
@ -213,7 +213,7 @@ void Graphics::resize(glm::uvec2 windowSize) {
|
|||||||
|
|
||||||
glm::mat4 Graphics::buildViewMatrix(Level* level) {
|
glm::mat4 Graphics::buildViewMatrix(Level* level) {
|
||||||
//construct lookAt (cameraPosition = cameraCenter + cameraVector)
|
//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()),
|
return glm::lookAt((level->getCameraCenter()->getPosition() + level->getCamera()->getVector()),
|
||||||
level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
|
level->getCameraCenter()->getPosition(), glm::vec3(0.0f, 1.0f, 0.0f));
|
||||||
|
4
level.cc
4
level.cc
@ -457,12 +457,12 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre
|
|||||||
else {
|
else {
|
||||||
mouseDelta.x = -mouseDelta.x;
|
mouseDelta.x = -mouseDelta.x;
|
||||||
camera.updateRotation(mouseDelta/100.0f);
|
camera.updateRotation(mouseDelta/100.0f);
|
||||||
physics.updateCameraPos(mouseDelta, 0.01f);
|
physics.updateCameraPos(mouseDelta, 100);
|
||||||
|
|
||||||
camera.setPosition(physics.getCameraPosition());
|
camera.setPosition(physics.getCameraPosition());
|
||||||
camera.setDirection(physics.getCameraToPlayer());
|
camera.setDirection(physics.getCameraToPlayer());
|
||||||
}
|
}
|
||||||
|
strength = 50;
|
||||||
if(wPressed){
|
if(wPressed){
|
||||||
physics.rollForward(camera.getVector(),strength);
|
physics.rollForward(camera.getVector(),strength);
|
||||||
}
|
}
|
||||||
|
28
physics.cc
28
physics.cc
@ -37,7 +37,7 @@ void Physics::takeUpdateStep(float timeDiff)
|
|||||||
|
|
||||||
|
|
||||||
btVector3 dir = cameraBody->getCenterOfMassPosition() - position;
|
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
|
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
|
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_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
|
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);
|
btVector3 inertia(0,0,0);
|
||||||
btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),playerBall->getCenterOfMassPosition()+btVector3(5,5,5)));
|
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 = new btRigidBody(info);
|
||||||
|
|
||||||
cameraBody->setDamping(1,0.5);
|
cameraBody->setDamping(0.8,0.5);
|
||||||
|
|
||||||
world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision);
|
world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision);
|
||||||
|
|
||||||
@ -343,13 +343,13 @@ glm::mat4 Physics::getRotation(int i)
|
|||||||
|
|
||||||
void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength)
|
void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength)
|
||||||
{
|
{
|
||||||
btVector3 change = playerBall->getCenterOfMassPosition()-cameraBody->getCenterOfMassPosition();;
|
btVector3 change = playerBall->getCenterOfMassPosition()-cameraBody->getCenterOfMassPosition();
|
||||||
change.setY(0);
|
change.setY(0);
|
||||||
change.normalize();
|
change.normalize();
|
||||||
change *= mouseMovement.x;
|
change *= mouseMovement.y;
|
||||||
change = btCross(btVector3(0,1,0),change);
|
change = btCross(btVector3(0,1,0),change);
|
||||||
change.setY(mouseMovement.y);
|
change.setY(mouseMovement.x);
|
||||||
change*=strength;
|
change*= strength;
|
||||||
|
|
||||||
cameraBody->applyCentralForce(change);
|
cameraBody->applyCentralForce(change);
|
||||||
|
|
||||||
@ -357,7 +357,8 @@ void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength)
|
|||||||
|
|
||||||
void Physics::rollForward(glm::vec3 camPos,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.normalize();
|
||||||
pos = btCross(pos,btVector3(0,1,0));
|
pos = btCross(pos,btVector3(0,1,0));
|
||||||
pos *= strength;
|
pos *= strength;
|
||||||
@ -366,7 +367,8 @@ void Physics::rollForward(glm::vec3 camPos,float strength)
|
|||||||
|
|
||||||
void Physics::rollBack(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.normalize();
|
||||||
pos = btCross(btVector3(0,1,0),pos);
|
pos = btCross(btVector3(0,1,0),pos);
|
||||||
pos *= strength;
|
pos *= strength;
|
||||||
@ -375,7 +377,8 @@ void Physics::rollBack(glm::vec3 camPos,float strength)
|
|||||||
|
|
||||||
void Physics::rollLeft(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.normalize();
|
||||||
pos *= strength;
|
pos *= strength;
|
||||||
playerBall->applyTorque(pos);
|
playerBall->applyTorque(pos);
|
||||||
@ -383,7 +386,8 @@ void Physics::rollLeft(glm::vec3 camPos,float strength)
|
|||||||
|
|
||||||
void Physics::rollRight(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.normalize();
|
||||||
pos *= strength;
|
pos *= strength;
|
||||||
playerBall->applyTorque(-pos);
|
playerBall->applyTorque(-pos);
|
||||||
|
Loading…
Reference in New Issue
Block a user