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, 1.0472f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f),
|
||||||
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
glm::vec3(0.0f, 0.0f, 0.0f), shader);
|
||||||
//add player to phy
|
//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(object);
|
||||||
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() {
|
||||||
@ -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
|
// 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
|
||||||
@ -76,14 +79,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));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
10
main.cc
10
main.cc
@ -122,15 +122,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
|
||||||
@ -158,6 +159,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()) );
|
||||||
|
|
||||||
|
51
physics.cc
51
physics.cc
@ -32,25 +32,36 @@ 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[i][j] > highest)
|
|
||||||
highest = heightData[i][j];
|
if (heightData[j][i] > highest)
|
||||||
|
highest = heightData[j][i];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
btHeightfieldTerrainShape* terrianShape = new btHeightfieldTerrainShape(width,length,heightData,highest,1,true,false);
|
/*
|
||||||
|
heightfield[j*length+i] = heightData[i][j];
|
||||||
|
if (heightData[i][j] > highest)
|
||||||
|
highest = heightData[i][j];
|
||||||
|
*/
|
||||||
|
|
||||||
|
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));
|
||||||
|
|
||||||
@ -102,10 +113,13 @@ void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsign
|
|||||||
|
|
||||||
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,6 +141,7 @@ 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);
|
||||||
|
|
||||||
@ -134,6 +149,7 @@ void Physics::addSphere(float rad, float x, float y, float z, float mass, unsign
|
|||||||
|
|
||||||
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,15 +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,0,camPos.z);
|
btVector3 pos(camPos.x,0,camPos.z);
|
||||||
pos.cross(btVector3(0,1,0));
|
pos = btCross(pos,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