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

@ -23,6 +23,8 @@ void Physics::init(std::string geometryPath) //prepares bullet by creating all i
}
void Physics::takeUpdateStep(float timeDiff)
{
if(simulationActive)
{
counter++;
if(counter<1)
@ -66,6 +68,36 @@ void Physics::takeUpdateStep(float timeDiff)
}
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;
}
}
}
}
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