Added smooth respawn in the forcePlayer function

This commit is contained in:
Jasper 2015-03-06 15:01:22 +01:00
parent 23f8ea8fa7
commit 1399f6ad7a
2 changed files with 85 additions and 43 deletions

View File

@ -24,47 +24,79 @@ void Physics::init(std::string geometryPath) //prepares bullet by creating all i
void Physics::takeUpdateStep(float timeDiff)
{
counter++;
if(counter<1)
if(simulationActive)
{
world->stepSimulation(timeDiff); //allows the world to be simmulated correctly indipendant of the timedifferences between frames
return;
}
for(unsigned i = 0; i < allPositionConstraints.size();i++) //this handles the spring constraints
{
if(allPositionConstraints[i].position != allPositionConstraints[i].body->getCenterOfMassPosition()) //if constraint != position of the body because otherwise dir = 0
counter++;
if(counter<1)
{
btVector3 dir = allPositionConstraints[i].position - allPositionConstraints[i].body->getCenterOfMassPosition();
dir = dir*allPositionConstraints[i].strength - allPositionConstraints[i].body->getLinearVelocity()
*allPositionConstraints[i].body->getLinearVelocity().length();
allPositionConstraints[i].body->applyCentralForce(dir*allPositionConstraints[i].strength); //apply a foce upon the object pushing it towards the constraint position
world->stepSimulation(timeDiff); //allows the world to be simmulated correctly indipendant of the timedifferences between frames
return;
}
for(unsigned i = 0; i < allPositionConstraints.size();i++) //this handles the spring constraints
{
if(allPositionConstraints[i].position != allPositionConstraints[i].body->getCenterOfMassPosition()) //if constraint != position of the body because otherwise dir = 0
{
btVector3 dir = allPositionConstraints[i].position - allPositionConstraints[i].body->getCenterOfMassPosition();
dir = dir*allPositionConstraints[i].strength - allPositionConstraints[i].body->getLinearVelocity()
*allPositionConstraints[i].body->getLinearVelocity().length();
allPositionConstraints[i].body->applyCentralForce(dir*allPositionConstraints[i].strength); //apply a foce upon the object pushing it towards the constraint position
}
}
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
btVector3 dir = cameraBody->getCenterOfMassPosition() - position;
float str = 50 * dir.length() / cameraBody->getInvMass(); //getInvMass() returns the inverted mass
cameraBody->setLinearVelocity(btVector3(0,0,0));
cameraBody->applyCentralForce(-dir*str*10) ; //scale the force by camera mass
counter=0;
float speed = cameraBody->getLinearVelocity().length();
if(speed>20.0f)
{
position = cameraBody->getLinearVelocity();
position.normalize();
cameraBody->setLinearVelocity(position*20);
}
world->stepSimulation(timeDiff);
}
else
{
if(sinking)
{
btVector3 currentPos = playerBall->getCenterOfMassPosition();
currentPos -= btVector3(0,0.003f,0);
playerBall->setCenterOfMassTransform(btTransform(playerBall->getOrientation(),currentPos));
if(playerBall->getCenterOfMassPosition().y() < resetHight - 3)
{
playerBall->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(startPosition.x(),startPosition.y() - 3,startPosition.z())));
playerBall->setLinearVelocity(btVector3(0,0,0));
playerBall->setAngularVelocity(btVector3(0,0,0));
forceMoveCamera(startPosition + btVector3(currentDirection.x()*cameraDistance,currentDirection.y()*cameraDistance,currentDirection.z()*cameraDistance));
sinking = false;
}
}
else
{
btVector3 currentPos = playerBall->getCenterOfMassPosition();
currentPos += btVector3(0,0.009f,0);
playerBall->setCenterOfMassTransform(btTransform(playerBall->getOrientation(),currentPos));
if(playerBall->getCenterOfMassPosition().y() >= startPosition.y() + 1)
{
sinking = true;
simulationActive = true;
}
}
}
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
btVector3 dir = cameraBody->getCenterOfMassPosition() - position;
float str = 50 * dir.length() / cameraBody->getInvMass(); //getInvMass() returns the inverted mass
cameraBody->setLinearVelocity(btVector3(0,0,0));
cameraBody->applyCentralForce(-dir*str*10) ; //scale the force by camera mass
counter=0;
float speed = cameraBody->getLinearVelocity().length();
if(speed>20.0f)
{
position = cameraBody->getLinearVelocity();
position.normalize();
cameraBody->setLinearVelocity(position*20);
}
world->stepSimulation(timeDiff);
}
void Physics::removePositionConstraint(int bodyIndice) //remover function for deleting all pos constraints on one body
@ -105,6 +137,8 @@ void Physics::addPlayer(float friction, float rad, Entity entity, float mass, fl
btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(glmQuat.x,glmQuat.y,glmQuat.z,glmQuat.w),btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z))); //next we define the motionstate, wich describes the innital position and rotation
startPosition =btVector3(entity.getPosition().x,entity.getPosition().y,entity.getPosition().z);
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
@ -578,18 +612,22 @@ void Physics::forceMove(glm::vec3 newPosition, unsigned indice)//ugly, but neede
{
bodies[indice]->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z)));
bodies[indice]->setLinearVelocity(btVector3(0,0,0));
bodies[indice]->setAngularVelocity(btVector3(0,0,0));
}
void Physics::forcePlayer(glm::vec3 newPosition)//ugly, but needed for reset
{
playerBall->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z)));
playerBall->setLinearVelocity(btVector3(0,0,0));
forceMoveCamera(newPosition + glm::vec3(currentDirection.x()*cameraDistance,currentDirection.y()*cameraDistance,currentDirection.z()*cameraDistance));
if(!simulationActive)
return;
simulationActive = false;
resetHight = playerBall->getCenterOfMassPosition().y();
startPosition = btVector3(newPosition.x,newPosition.y,newPosition.z);
}
void Physics::forceMoveCamera(glm::vec3 newPosition)
void Physics::forceMoveCamera(btVector3 newPosition)
{
cameraBody->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),btVector3(newPosition.x,newPosition.y,newPosition.z)));
cameraBody->setCenterOfMassTransform(btTransform(btQuaternion(0,0,0,1),newPosition));
}
void Physics::kill() //delete dynamically allocated memory

View File

@ -72,7 +72,7 @@ class Physics {
void kill();
void addButtonFrame(Entity entity);
void forceMove(glm::vec3 newPosition, unsigned indice);
void forceMoveCamera(glm::vec3 newPosition);
void forceMoveCamera(btVector3 newPosition);
void addConvexBody(Entity entity, std::string path, float mass, float dampningL, float dampningA, unsigned indice, float scaling, bool rotate);
void prepareCollisionDetection();
bool playerWithGround();
@ -82,6 +82,10 @@ class Physics {
struct positionConstraint{btRigidBody* body; float strength; btVector3 position;};
private:
btVector3 startPosition = btVector3(0,0,0);
float resetHight = 0;
bool simulationActive = true;
bool sinking = true;
btVector3 currentDirection = btVector3(1,1,1);
btRigidBody* playerBall; //allows for easier access to the ball
btRigidBody* terrainBody; //duh