modified camera to a directional camera as requested
This commit is contained in:
parent
5428f4cbed
commit
d0b56e3cf1
38
physics.cc
38
physics.cc
@ -44,25 +44,16 @@ void Physics::takeUpdateStep(float timeDiff)
|
||||
|
||||
|
||||
btVector3 position = cameraBody->getCenterOfMassPosition() - playerBall->getCenterOfMassPosition(); //gets a vector from the player to the camera
|
||||
position = currentDirection;
|
||||
position.normalize();
|
||||
position *= cameraDistance;
|
||||
position += playerBall->getCenterOfMassPosition(); //is the position cameraDistance away from the player in the direction of the camera
|
||||
|
||||
//prevent the camera from being dragged along on the ground
|
||||
if (position.getY() < playerBall->getCenterOfMassPosition().getY() + cameraDistance/2)
|
||||
position.setY(playerBall->getCenterOfMassPosition().getY() + cameraDistance/2);
|
||||
|
||||
btVector3 dir = cameraBody->getCenterOfMassPosition() - position;
|
||||
float str = 50 * dir.length() / cameraBody->getInvMass(); //getInvMass() returns the inverted mass
|
||||
|
||||
|
||||
/*if(dir.length() > -0.1f && dir.length() < 0.1f)
|
||||
{
|
||||
cameraBody->setLinearVelocity(btVector3(0,0,0));
|
||||
world->stepSimulation(timeDiff);
|
||||
return;
|
||||
}*/
|
||||
|
||||
cameraBody->setLinearVelocity(btVector3(0,0,0));
|
||||
cameraBody->applyCentralForce(-dir*str*10) ; //scale the force by camera mass
|
||||
counter=0;
|
||||
@ -117,7 +108,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.1f;
|
||||
info.m_restitution = 0.0f;
|
||||
|
||||
playerBall = new btRigidBody(info); //finally we create the rigid body using the info
|
||||
|
||||
@ -489,15 +480,24 @@ void Physics::updateCameraPos(glm::vec2 mouseMovement, float strength, float dis
|
||||
{
|
||||
this->cameraDistance = distance;
|
||||
//note: in mouseMovement x and y are flipped in contrast to bullet
|
||||
btVector3 change = playerBall->getCenterOfMassPosition()-cameraBody->getCenterOfMassPosition();
|
||||
change.setY(0);
|
||||
change.normalize(); //normalize so that the distance between camera and body does not matter
|
||||
change *= (mouseMovement.y); //we start with left/right movement because this needs to be calculated via a crossproduct, and the up down value would alter that
|
||||
change = btCross(btVector3(0,1,0),change);
|
||||
change.setY(mouseMovement.x); //scaleing because otherwise oup/down much stronger then left right
|
||||
change *= strength / cameraBody->getInvMass();
|
||||
btVector3 dodo = btVector3(0,1,0).cross(btVector3(currentDirection.x(),0,currentDirection.z()));
|
||||
currentDirection = currentDirection.rotate(dodo,-mouseMovement.x / 100);//mathhelper 3.14159265359
|
||||
|
||||
cameraBody->applyCentralForce(change);
|
||||
btVector3 compare = currentDirection;
|
||||
compare.setY(0);
|
||||
compare.normalize();
|
||||
if(currentDirection.angle(compare)>= 3.14159265359/4)
|
||||
{
|
||||
dodo = btVector3(0,1,0).cross(btVector3(currentDirection.x(),0,currentDirection.z()));
|
||||
compare = compare.rotate(-dodo, 3.14159265359/4);
|
||||
if(currentDirection.y()<0)
|
||||
compare.setY(-compare.y());
|
||||
|
||||
currentDirection = compare;
|
||||
}
|
||||
|
||||
currentDirection = currentDirection.rotate(btVector3(0,1,0),mouseMovement.y/100);
|
||||
currentDirection.normalize();
|
||||
}
|
||||
|
||||
//use the crossproduct to correctly apply a torque to the palyer if function called
|
||||
|
Loading…
Reference in New Issue
Block a user