Merge branch 'master' of github.com:Faerbit/swp
This commit is contained in:
commit
c22ae2c244
20
level.cc
20
level.cc
@ -78,6 +78,9 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
|||||||
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
||||||
objects.push_back(terrainObject);
|
objects.push_back(terrainObject);
|
||||||
cameraCenter = &objects[0];
|
cameraCenter = &objects[0];
|
||||||
|
|
||||||
|
//addTerrainPhysic
|
||||||
|
physics.addTerrain(terrain.getHeightmapWidth(), terrain.getHeightmapHeight(), terrain.getHeightmap());
|
||||||
}
|
}
|
||||||
|
|
||||||
void Level::render() {
|
void Level::render() {
|
||||||
@ -86,7 +89,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
|
// rotate bunny
|
||||||
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
||||||
// Ignore first two mouse updates, because they are incorrect
|
// Ignore first two mouse updates, because they are incorrect
|
||||||
@ -98,14 +101,23 @@ void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPre
|
|||||||
camera.updateRotation(mouseDelta/100.0f);
|
camera.updateRotation(mouseDelta/100.0f);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(wPressed)
|
if(wPressed){
|
||||||
{
|
physics.rollForward(camera.getVector(),1.0f);
|
||||||
//physics.rollForward(camera.getRotation);
|
}
|
||||||
|
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);
|
physics.takeUpdateStep(runTime);
|
||||||
|
|
||||||
objects[0].setPosition(physics.getPos(0));
|
objects[0].setPosition(physics.getPos(0));
|
||||||
|
objects[0].setRotation(physics.getRotation(0));
|
||||||
lights[2].setPosition(physics.getPos(0));
|
lights[2].setPosition(physics.getPos(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
main.cc
10
main.cc
@ -124,15 +124,16 @@ int main( int argc, char *argv[] )
|
|||||||
|
|
||||||
double lastUpdate=0.0f;
|
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 {
|
do {
|
||||||
|
|
||||||
double now = glfwGetTime()- startTimeInSeconds;
|
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) {
|
if (showNextFPS <= now) {
|
||||||
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
||||||
sstream << std::setprecision(1) << std::fixed
|
sstream << std::setprecision(1) << std::fixed
|
||||||
@ -160,6 +161,7 @@ int main( int argc, char *argv[] )
|
|||||||
glfwSwapBuffers(app.getGraphics()->getWindow());
|
glfwSwapBuffers(app.getGraphics()->getWindow());
|
||||||
glfwPollEvents();
|
glfwPollEvents();
|
||||||
frameCount++;
|
frameCount++;
|
||||||
|
|
||||||
} // Check if the window was closed
|
} // Check if the window was closed
|
||||||
while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) );
|
while( !glfwWindowShouldClose(app.getGraphics()->getWindow()) );
|
||||||
|
|
||||||
|
60
physics.cc
60
physics.cc
@ -32,30 +32,41 @@ void Physics::init()
|
|||||||
|
|
||||||
void Physics::takeUpdateStep(float timeDiff)
|
void Physics::takeUpdateStep(float timeDiff)
|
||||||
{
|
{
|
||||||
|
|
||||||
world->stepSimulation(timeDiff);
|
world->stepSimulation(timeDiff);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Physics::addTerrain(int width, int length, float** heightData)
|
void Physics::addTerrain(int width, int length, float** heightData)
|
||||||
{
|
{
|
||||||
|
|
||||||
float* heightfield = new float[width * length];
|
float* heightfield = new float[width * length];
|
||||||
int highest = -999999, j = 0, i = 0;
|
int highest = -999999, j = 0, i = 0;
|
||||||
for (i = 0; i < width; i++)
|
for (i = 0; i < width; i++)
|
||||||
|
{
|
||||||
for (j = 0; j < length; j++) {
|
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)
|
if (heightData[i][j] > highest)
|
||||||
highest = heightData[i][j];
|
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);
|
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));
|
//tBody->getWoorldTransform().setRotation(btQuaternion(0,0,0,1));
|
||||||
|
|
||||||
terrainBody = tBody;
|
terrainBody = tBody;
|
||||||
|
|
||||||
world->addRigidBody(terrainBody);
|
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);
|
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
|
||||||
|
|
||||||
playerBall = new btRigidBody(info);
|
playerBall = new btRigidBody(info);
|
||||||
|
|
||||||
|
playerBall->setDamping(0.1f,0.3f);
|
||||||
|
|
||||||
world->addRigidBody(playerBall);
|
world->addRigidBody(playerBall);
|
||||||
|
|
||||||
bodies.push_back(playerBall);
|
bodies.push_back(playerBall);
|
||||||
|
|
||||||
|
playerBall->setSleepingThresholds(0,0);
|
||||||
if(bodies.size() == indice)
|
if(bodies.size() == indice)
|
||||||
throw std::invalid_argument( "Bodies out of Sync" );
|
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)));
|
btDefaultMotionState* motion = new btDefaultMotionState(btTransform(btQuaternion(0,0,0,1),btVector3(x,y,z)));
|
||||||
|
|
||||||
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
|
btRigidBody::btRigidBodyConstructionInfo info(mass,motion,sphere,inertia);
|
||||||
|
//info.
|
||||||
|
|
||||||
btRigidBody* body = new btRigidBody(info);
|
btRigidBody* body = new btRigidBody(info);
|
||||||
|
|
||||||
world->addRigidBody(body);
|
world->addRigidBody(body);
|
||||||
|
|
||||||
bodies.push_back(body);
|
bodies.push_back(body);
|
||||||
|
|
||||||
|
body->setSleepingThresholds(0,0);
|
||||||
|
|
||||||
if(bodies.size() == indice)
|
if(bodies.size() == indice)
|
||||||
throw std::invalid_argument( "Bodies out of Sync" );
|
throw std::invalid_argument( "Bodies out of Sync" );
|
||||||
@ -158,16 +174,30 @@ glm::mat4 Physics::getRotation(int i)
|
|||||||
return matrix;
|
return matrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Physics::rollForward(glm::vec3 camPos)
|
void Physics::rollForward(glm::vec3 camPos,float strength)
|
||||||
{
|
{
|
||||||
btVector3 pos(camPos.x,camPos.y,camPos.z);
|
btVector3 pos(camPos.x,0,camPos.z);
|
||||||
pos -= playerBody->getCenterOfMassPosition();
|
pos = btCross(pos,btVector3(0,1,0));
|
||||||
pos.cross(btVector3(0,1,0));
|
|
||||||
playerBall->applyTorque(pos);
|
playerBall->applyTorque(pos);
|
||||||
|
}
|
||||||
|
|
||||||
/* glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera;
|
void Physics::rollBack(glm::vec3 camPos,float strength)
|
||||||
saveVector = glm::cross(glm::vec3(0,1,0),saveVector);
|
{
|
||||||
playerBall->applyTorque(btVector3(saveVector[0],saveVector[1],saveVector[2]));*/
|
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 init();
|
||||||
void takeUpdateStep(float timeDiff);
|
void takeUpdateStep(float timeDiff);
|
||||||
void rollForward(glm::vec3 camPos, float strength);
|
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::vec3 getPos(int i);
|
||||||
glm::mat4 getRotation(int i);
|
glm::mat4 getRotation(int i);
|
||||||
void rollForward(glm::vec3 camPos);
|
void rollForward(glm::vec3 camPos);
|
||||||
|
Loading…
Reference in New Issue
Block a user