further improving physics and interaction
This commit is contained in:
parent
a77ed38625
commit
abe9513aa3
24
level.cc
24
level.cc
@ -29,9 +29,9 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
||||
glm::vec3(0.0f, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
|
||||
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
||||
//add player to phy
|
||||
this->physics.addPlayer(0.75f,0.0f,5.0f,0.0f,1.0f,0);
|
||||
this->physics.addPlayer(0.75f,0.0f,15.0f,0.0f,1.0f,0);
|
||||
|
||||
physics.addStaticGroundPlane();
|
||||
//physics.addStaticGroundPlane();
|
||||
|
||||
|
||||
|
||||
@ -56,6 +56,9 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
||||
objects.push_back(object);
|
||||
objects.push_back(terrainObject);
|
||||
cameraCenter = &objects[0];
|
||||
|
||||
//addTerrainPhysic
|
||||
physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap());
|
||||
}
|
||||
|
||||
void Level::render() {
|
||||
@ -64,7 +67,7 @@ void Level::render() {
|
||||
}
|
||||
}
|
||||
|
||||
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool dPressed, bool sPressed) {
|
||||
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool sPressed, bool dPressed) {
|
||||
// rotate bunny
|
||||
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
||||
// Ignore first two mouse updates, because they are incorrect
|
||||
@ -76,14 +79,23 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre
|
||||
camera.updateRotation(mouseDelta/100.0f);
|
||||
}
|
||||
|
||||
if(wPressed)
|
||||
{
|
||||
//physics.rollForward(camera.getRotation);
|
||||
if(wPressed){
|
||||
physics.rollForward(camera.getVector(),1.0f);
|
||||
}
|
||||
if(aPressed) {
|
||||
physics.rollLeft(camera.getVector(),1.0f);
|
||||
}
|
||||
if(sPressed) {
|
||||
physics.rollBack(camera.getVector(),1.0f);
|
||||
}
|
||||
if(dPressed){
|
||||
physics.rollRight(camera.getVector(),1.0f);
|
||||
}
|
||||
|
||||
physics.takeUpdateStep(runTime);
|
||||
|
||||
objects[0].setPosition(physics.getPos(0));
|
||||
objects[0].setRotation(physics.getRotation(0));
|
||||
|
||||
|
||||
}
|
||||
|
10
main.cc
10
main.cc
@ -122,15 +122,16 @@ int main( int argc, char *argv[] )
|
||||
|
||||
double lastUpdate=0.0f;
|
||||
|
||||
int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W);
|
||||
int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A);
|
||||
int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S);
|
||||
int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D);
|
||||
|
||||
do {
|
||||
|
||||
double now = glfwGetTime()- startTimeInSeconds;
|
||||
|
||||
int stateW = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_W);
|
||||
int stateA = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_A);
|
||||
int stateS = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_S);
|
||||
int stateD = glfwGetKey(app.getGraphics()->getWindow(), GLFW_KEY_D);
|
||||
|
||||
if (showNextFPS <= now) {
|
||||
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
||||
sstream << std::setprecision(1) << std::fixed
|
||||
@ -158,6 +159,7 @@ int main( int argc, char *argv[] )
|
||||
glfwSwapBuffers(app.getGraphics()->getWindow());
|
||||
glfwPollEvents();
|
||||
frameCount++;
|
||||
|
||||
} // Check if the window was closed
|
||||
while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) );
|
||||
|
||||
|
57
physics.cc
57
physics.cc
@ -32,30 +32,41 @@ void Physics::init()
|
||||
|
||||
void Physics::takeUpdateStep(float timeDiff)
|
||||
{
|
||||
|
||||
world->stepSimulation(timeDiff);
|
||||
}
|
||||
|
||||
void Physics::addTerrain(int width, int length, float** heightData)
|
||||
{
|
||||
|
||||
float* heightfield = new float[width * length];
|
||||
int highest = -999999, j = 0, i = 0;
|
||||
for (i = 0; i < width; i++)
|
||||
{
|
||||
for (j = 0; j < length; j++) {
|
||||
heightfield[j*length+i] = heightData[i][j];
|
||||
heightfield[i*length+j] = heightData[j][i];
|
||||
|
||||
if (heightData[j][i] > highest)
|
||||
highest = heightData[j][i];
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
heightfield[j*length+i] = heightData[i][j];
|
||||
if (heightData[i][j] > highest)
|
||||
highest = heightData[i][j];
|
||||
}
|
||||
*/
|
||||
|
||||
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(width,length,heightData,highest,1,true,false);
|
||||
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(length,width,heightfield,highest,1,true,false);
|
||||
|
||||
btRigidBody* tBody = new btRigidBody(0,new btDefaultMotionState(),terrianShape);
|
||||
|
||||
tBody->getWorldTransform().setOrigin(btVector3(0,highest/2,0));
|
||||
tBody->getWorldTransform().setOrigin(btVector3(0,5*highest/10,0));
|
||||
|
||||
//tBody->getWoorldTransform().setRotation(btQuaternion(0,0,0,1));
|
||||
|
||||
terrainBody = tBody;
|
||||
|
||||
|
||||
world->addRigidBody(terrainBody);
|
||||
|
||||
/*
|
||||
@ -101,11 +112,14 @@ void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsign
|
||||
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
|
||||
|
||||
playerBall = new btRigidBody(info);
|
||||
|
||||
|
||||
playerBall->setDamping(0.1f,0.3f);
|
||||
|
||||
world->addRigidBody(playerBall);
|
||||
|
||||
bodies.push_back(playerBall);
|
||||
|
||||
|
||||
playerBall->setSleepingThresholds(0,0);
|
||||
if(bodies.size() == indice)
|
||||
throw std::invalid_argument( "Bodies out of Sync" );
|
||||
|
||||
@ -127,13 +141,15 @@ void Physics::addSphere(float rad, float x, float y, float z, float mass, unsign
|
||||
btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(x,y,z)));
|
||||
|
||||
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
|
||||
//info.
|
||||
|
||||
btRigidBody* body = new btRigidBody(info);
|
||||
|
||||
world->addRigidBody(body);
|
||||
|
||||
bodies.push_back(body);
|
||||
|
||||
|
||||
body->setSleepingThresholds(0,0);
|
||||
|
||||
if(bodies.size() == indice)
|
||||
throw std::invalid_argument( "Bodies out of Sync" );
|
||||
@ -158,15 +174,30 @@ glm::mat4 Physics::getRotation(int i)
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void Physics::rollForward(glm::vec3 camPos)
|
||||
void Physics::rollForward(glm::vec3 camPos,float strength)
|
||||
{
|
||||
btVector3 pos(camPos.x,0,camPos.z);
|
||||
pos.cross(btVector3(0,1,0));
|
||||
pos = btCross(pos,btVector3(0,1,0));
|
||||
playerBall->applyTorque(pos);
|
||||
}
|
||||
|
||||
/* glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera;
|
||||
saveVector = glm::cross(glm::vec3(0,1,0),saveVector);
|
||||
playerBall->applyTorque(btVector3(saveVector[0],saveVector[1],saveVector[2]));*/
|
||||
void Physics::rollBack(glm::vec3 camPos,float strength)
|
||||
{
|
||||
btVector3 pos(camPos.x,0,camPos.z);
|
||||
pos = btCross(btVector3(0,1,0),pos);
|
||||
playerBall->applyTorque(pos);
|
||||
}
|
||||
|
||||
void Physics::rollLeft(glm::vec3 camPos,float strength)
|
||||
{
|
||||
btVector3 pos(camPos.x,0,camPos.z);
|
||||
playerBall->applyTorque(pos);
|
||||
}
|
||||
|
||||
void Physics::rollRight(glm::vec3 camPos,float strength)
|
||||
{
|
||||
btVector3 pos(camPos.x,0,camPos.z);
|
||||
playerBall->applyTorque(-pos);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -37,6 +37,9 @@ class Physics {
|
||||
void init();
|
||||
void takeUpdateStep(float timeDiff);
|
||||
void rollForward(glm::vec3 camPos, float strength);
|
||||
void rollLeft(glm::vec3 camPos, float strength);
|
||||
void rollRight(glm::vec3 camPos, float strength);
|
||||
void rollBack(glm::vec3 camPos, float strength);
|
||||
glm::vec3 getPos(int i);
|
||||
glm::mat4 getRotation(int i);
|
||||
void rollForward(glm::vec3 camPos);
|
||||
|
Loading…
Reference in New Issue
Block a user