got the marble to no longer jitter all over the place

This commit is contained in:
Jasper 2015-02-06 16:26:22 +01:00
parent 7e8b0d4c58
commit 017c96bd55

View File

@ -21,11 +21,12 @@ void Physics::init() //prepares bullet by creating all initial classes
void Physics::takeUpdateStep(float timeDiff) void Physics::takeUpdateStep(float timeDiff)
{ {
world->stepSimulation(timeDiff); //allows the world to be simmulated correctly indipendant of the timedifferences between frames
counter++; counter++;
if(counter<10) if(counter<1)
{
world->stepSimulation(timeDiff);//allows the world to be simmulated correctly indipendant of the timedifferences between frames
return; return;
}
for(unsigned i = 0; i < allPositionConstraints.size();i++) //this handles the spring constraints for(unsigned i = 0; i < allPositionConstraints.size();i++) //this handles the spring constraints
{ {
@ -43,14 +44,16 @@ void Physics::takeUpdateStep(float timeDiff)
position *= 5; position *= 5;
position += playerBall->getCenterOfMassPosition(); //is the position 5 units away from the player in the direction of the camera position += playerBall->getCenterOfMassPosition(); //is the position 5 units away from the player in the direction of the camera
position.setY(playerBall->getCenterOfMassPosition().getY() + 1);
btVector3 dir = cameraBody->getCenterOfMassPosition() - position; btVector3 dir = cameraBody->getCenterOfMassPosition() - position;
float str = cameraBody->getInvMass()*30 * dir.length(); float str = cameraBody->getInvMass()*50 * dir.length();
cameraBody->applyCentralForce(-dir*str);//scale the force by camera mass cameraBody->applyCentralForce(-dir*str);//scale the force by camera mass
cameraBody->applyCentralForce(btVector3(0,96,0)); //we leave gravity in because by contrasting gravity to this force we force the camera slightly higher than the ball when still, but it follows more closer to the level of the ball (though still slightly higher) when it is moving.
counter=0; counter=0;
world->stepSimulation(timeDiff);
} }
//
void Physics::removePositionConstraint(int bodyIndice) //remover function for deleting all pos constraints on one body void Physics::removePositionConstraint(int bodyIndice) //remover function for deleting all pos constraints on one body
{ {
for(unsigned i = 0; i < allPositionConstraints.size(); i++) for(unsigned i = 0; i < allPositionConstraints.size(); i++)
@ -91,7 +94,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; info.m_restitution = 0.1f;
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
@ -130,6 +133,7 @@ void Physics::addTerrain(int width, int length, float** heightData) //The terria
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false); btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false);
btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrianShape,btVector3(0,0,0)); //next we process all data for the rigid body into info btRigidBody::btRigidBodyConstructionInfo info(0,new btDefaultMotionState(),terrianShape,btVector3(0,0,0)); //next we process all data for the rigid body into info
info.m_friction = 1;
info.m_restitution = 0; info.m_restitution = 0;
btRigidBody* tBody = new btRigidBody(info); btRigidBody* tBody = new btRigidBody(info);
@ -277,7 +281,7 @@ void Physics::addBox(float width, float height, float length, Entity entity, flo
void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice,bool rotate) void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, float dampningA, unsigned indice,bool rotate)
{ {
if(bodies.size() == indice) if(bodies.size() == indice) //(user's initial) height, not the actual height. More...
throw std::invalid_argument( "Bodies out of Sync" ); throw std::invalid_argument( "Bodies out of Sync" );
btSphereShape* sphere = new btSphereShape(rad); btSphereShape* sphere = new btSphereShape(rad);
@ -311,7 +315,7 @@ void Physics::addSphere(float rad, Entity entity, float mass, float dampningL, f
void Physics::addCamera() //Camera Creator automatically called when player is created void Physics::addCamera() //Camera Creator automatically called when player is created
{ {
btSphereShape* sphere = new btSphereShape(0.5f);//we use this to make a more interesting camera, that does not interpenetrate with the terrain/objects btSphereShape* sphere = new btSphereShape(0.2f);//we use this to make a more interesting camera, that does not interpenetrate with the terrain/objects
btVector3 inertia(0,0,0); //rotation handled elsewhere (as it always has to look at the player) btVector3 inertia(0,0,0); //rotation handled elsewhere (as it always has to look at the player)
@ -328,6 +332,8 @@ void Physics::addCamera() //Camera Creator automatically called when player is c
world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision); world->addRigidBody(cameraBody,COL_OBJECTS, objectsPhysicsCollision);
cameraBody->setGravity(btVector3(0,0,0));
cameraBody->setSleepingThresholds(0,0); //very important, otherwise camera may go to sleep, aka not move until next collision cameraBody->setSleepingThresholds(0,0); //very important, otherwise camera may go to sleep, aka not move until next collision
} }