Implimented a basic version of Physics into level with a ball colliding with a static plain.
This commit is contained in:
parent
3ddcb72d5a
commit
cac4672c7c
25
level.cc
25
level.cc
@ -1,5 +1,7 @@
|
||||
#include "level.hh"
|
||||
|
||||
|
||||
|
||||
Level::Level(std::string filePath){
|
||||
this->filePath = filePath;
|
||||
this->terrain = Terrain(filePath + "/terrain");
|
||||
@ -12,6 +14,10 @@ Level::~Level() {
|
||||
}
|
||||
|
||||
void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
||||
|
||||
this->physics = Physics();
|
||||
this->physics.init();
|
||||
|
||||
// currently hard coded should later read this stuff out of a file
|
||||
this->camera = Camera(glm::vec2(-0.8f, 0.0f), 3.0f);
|
||||
// load the geometry of the stanford bunny and build a VAO:
|
||||
@ -19,16 +25,21 @@ void Level::load(ACGL::OpenGL::SharedShaderProgram shader) {
|
||||
// load a texture:
|
||||
Material material = Material("stoneTexture.png", 0.1f, 0.5f, 0.5f, 3.0f);
|
||||
//Create object
|
||||
Object object = Object(model, material, glm::vec3(0.0f, 1.0f, -2.0f),
|
||||
Object object = Object(model, material, glm::vec3(0.0f, 5.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);
|
||||
//add player to phy
|
||||
this->physics.addPlayer(0.75f,0.0f,5.0f,0.0f,1.0f,0);
|
||||
|
||||
physics.addStaticGroundPlane();
|
||||
|
||||
|
||||
|
||||
//set lighting parameters
|
||||
ambientLight = glm::vec3(1.0f, 1.0f, 1.0f);
|
||||
Light light = Light(glm::vec3(-3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f);
|
||||
Light light = Light(glm::vec3(-10.0f, 20.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f);
|
||||
lights.push_back(light);
|
||||
Light light2 = Light(glm::vec3(3.0f, 0.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f);
|
||||
Light light2 = Light(glm::vec3(10.0f, 20.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(1.0f, 1.0f, 1.0f), 2.0f);
|
||||
lights.push_back(light2);
|
||||
|
||||
|
||||
@ -53,7 +64,7 @@ void Level::render() {
|
||||
}
|
||||
}
|
||||
|
||||
void Level::update(float runTime, glm::vec2 mouseDelta) {
|
||||
void Level::update(float runTime, glm::vec2 mouseDelta, bool wPressed, bool aPressed, bool dPressed, bool sPressed) {
|
||||
// rotate bunny
|
||||
//cameraCenter->setRotation(glm::vec3(0.0f, 1.0472f * runTime, 0.0f));
|
||||
// Ignore first two mouse updates, because they are incorrect
|
||||
@ -64,6 +75,12 @@ void Level::update(float runTime, glm::vec2 mouseDelta) {
|
||||
else {
|
||||
camera.updateRotation(mouseDelta/100.0f);
|
||||
}
|
||||
|
||||
physics.takeUpdateStep(runTime);
|
||||
|
||||
objects[0].setPosition(physics.getPos(0));
|
||||
|
||||
|
||||
}
|
||||
|
||||
glm::vec3 Level::getAmbientLight() {
|
||||
|
4
level.hh
4
level.hh
@ -8,6 +8,7 @@
|
||||
#include "terrain.hh"
|
||||
#include "material.hh"
|
||||
#include "camera.hh"
|
||||
#include "physics.hh"
|
||||
|
||||
class Level {
|
||||
public:
|
||||
@ -15,7 +16,7 @@ class Level {
|
||||
Level();
|
||||
~Level();
|
||||
void load(ACGL::OpenGL::SharedShaderProgram shader); // Shader is necessary for correct texture assigning
|
||||
void update(float runTime, glm::vec2 mouseDelta);
|
||||
void update(float runTime, glm::vec2 mouseDelta,bool wPressed, bool aPressed, bool dPressed, bool sPressed);
|
||||
void render();
|
||||
glm::vec3 getAmbientLight();
|
||||
std::vector<Light> getLights();
|
||||
@ -27,6 +28,7 @@ class Level {
|
||||
std::vector<Light> lights;
|
||||
glm::vec3 ambientLight;
|
||||
Object* cameraCenter;
|
||||
Physics physics;
|
||||
Camera camera;
|
||||
Terrain terrain;
|
||||
};
|
||||
|
17
main.cc
17
main.cc
@ -120,8 +120,16 @@ int main( int argc, char *argv[] )
|
||||
double startTimeInSeconds = glfwGetTime();
|
||||
double showNextFPS = startTimeInSeconds + FPSdelay;
|
||||
|
||||
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();
|
||||
|
||||
double now = glfwGetTime()- startTimeInSeconds;
|
||||
|
||||
if (showNextFPS <= now) {
|
||||
std::stringstream sstream (std::stringstream::in | std::stringstream::out);
|
||||
@ -136,8 +144,11 @@ int main( int argc, char *argv[] )
|
||||
glfwGetCursorPos(app.getGraphics()->getWindow(), &xpos, &ypos);
|
||||
glfwSetCursorPos(app.getGraphics()->getWindow(), app.getGraphics()->getWindowSize().x/2, app.getGraphics()->getWindowSize().y/2);
|
||||
|
||||
app.getLevel()->update(now - startTimeInSeconds, glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
|
||||
(float)xpos-app.getGraphics()->getWindowSize().x/2));
|
||||
app.getLevel()->update(now - lastUpdate,
|
||||
glm::vec2((float)ypos-app.getGraphics()->getWindowSize().y/2,
|
||||
(float)xpos-app.getGraphics()->getWindowSize().x/2),
|
||||
stateW == GLFW_PRESS,stateA == GLFW_PRESS,stateS == GLFW_PRESS,stateD == GLFW_PRESS);
|
||||
lastUpdate = now;
|
||||
app.getGraphics()->render(app.getLevel(), app.getShader());
|
||||
|
||||
openGLCriticalError();
|
||||
|
40
physics.cc
40
physics.cc
@ -1,7 +1,5 @@
|
||||
#include "physics.hh"
|
||||
|
||||
#include <vector>
|
||||
|
||||
|
||||
btDynamicsWorld* world; //contains physical attributes of the world.
|
||||
btDispatcher* dispatcher; //
|
||||
@ -12,8 +10,17 @@ btConstraintSolver* solver; //solver for forces and impulses.
|
||||
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
|
||||
btRigidBody* playerBall;
|
||||
btRigidBody* terrainBody;
|
||||
btRigidBody* staticGroundBody;
|
||||
|
||||
void init()
|
||||
|
||||
Physics::Physics() {
|
||||
//init();
|
||||
}
|
||||
|
||||
Physics::~Physics() {
|
||||
}
|
||||
|
||||
void Physics::init()
|
||||
{
|
||||
colConfig = new btDefaultCollisionConfiguration();
|
||||
dispatcher = new btCollisionDispatcher(colConfig);
|
||||
@ -22,16 +29,14 @@ void init()
|
||||
world = new btDiscreteDynamicsWorld(dispatcher,broadphase,solver,colConfig);
|
||||
|
||||
world->setGravity(btVector3(0,-10,-0));
|
||||
|
||||
|
||||
}
|
||||
|
||||
void takeUpdateStep(float timeDiff)
|
||||
void Physics::takeUpdateStep(float timeDiff)
|
||||
{
|
||||
world->stepSimulation(timeDiff);
|
||||
}
|
||||
|
||||
void addTerrain(int width, int length, float** heightData)
|
||||
void Physics::addTerrain(int width, int length, float** heightData)
|
||||
{
|
||||
float* heightfield = new float[width * length];
|
||||
int highest = -999999, j = 0, i = 0;
|
||||
@ -70,7 +75,17 @@ void addTerrain(int width, int length, float** heightData)
|
||||
|
||||
}
|
||||
|
||||
void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice)
|
||||
void Physics::addStaticGroundPlane()
|
||||
{
|
||||
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(0, 1, 0), 0);
|
||||
btDefaultMotionState* groundMotionState = new btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0, 0, 0)));
|
||||
btRigidBody::btRigidBodyConstructionInfo groundRigidBodyCI(0, groundMotionState, groundShape, btVector3(0, 0, 0));
|
||||
staticGroundBody = new btRigidBody(groundRigidBodyCI);
|
||||
|
||||
world->addRigidBody(staticGroundBody);
|
||||
}
|
||||
|
||||
void Physics::addPlayer(float rad, float x, float y, float z, float mass, unsigned indice)
|
||||
{
|
||||
if(bodies.size() != indice)
|
||||
throw std::invalid_argument( "Bodies out of Sync" );
|
||||
@ -98,7 +113,7 @@ void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice
|
||||
}
|
||||
|
||||
|
||||
void addSphere(float rad, float x, float y, float z, float mass, unsigned indice)
|
||||
void Physics::addSphere(float rad, float x, float y, float z, float mass, unsigned indice)
|
||||
{
|
||||
if(bodies.size() != indice)
|
||||
throw std::invalid_argument( "Bodies out of Sync" );
|
||||
@ -126,26 +141,25 @@ void addSphere(float rad, float x, float y, float z, float mass, unsigned indice
|
||||
|
||||
}
|
||||
|
||||
glm::vec3 getPos(int i)
|
||||
glm::vec3 Physics::getPos(int i)
|
||||
{
|
||||
btVector3 origin = bodies[i]->getCenterOfMassPosition();
|
||||
glm::vec3 save(origin.getX(),origin.getY(),origin.getZ());
|
||||
return save;
|
||||
}
|
||||
|
||||
glm::mat4 getRotation(int i)
|
||||
glm::mat4 Physics::getRotation(int i)
|
||||
{
|
||||
btQuaternion quat = bodies[i]->getOrientation();
|
||||
|
||||
glm::mat4 matrix = glm::rotate(
|
||||
matrix,
|
||||
quat.getAngle(),
|
||||
glm::vec3(quat.getAxis().getX(), quat.getAxis().getY(), quat.getAxis().getZ())
|
||||
);
|
||||
return matrix;
|
||||
}
|
||||
|
||||
void rollForward(glm::mat3 rotCamera)
|
||||
void Physics::rollForward(glm::mat3 rotCamera)
|
||||
{
|
||||
glm::vec3 saveVector= glm::vec3(1,0,0) * rotCamera;
|
||||
saveVector = glm::cross(glm::vec3(0,1,0),saveVector);
|
||||
|
@ -11,6 +11,7 @@
|
||||
|
||||
#include "extern/bullet/src/BulletCollision/CollisionShapes/btSphereShape.h"
|
||||
#include "extern/bullet/src/BulletCollision/CollisionShapes/btHeightfieldTerrainShape.h"
|
||||
#include "extern/bullet/src/BulletCollision/CollisionShapes/btStaticPlaneShape.h"
|
||||
|
||||
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btConstraintSolver.h"
|
||||
#include "extern/bullet/src/BulletDynamics/ConstraintSolver/btSequentialImpulseConstraintSolver.h"//YAY!
|
||||
@ -31,7 +32,7 @@
|
||||
|
||||
class Physics {
|
||||
public:
|
||||
Physics();
|
||||
Physics();
|
||||
~Physics();
|
||||
void init();
|
||||
void takeUpdateStep(float timeDiff);
|
||||
@ -39,6 +40,7 @@ class Physics {
|
||||
glm::vec3 getPos(int i);
|
||||
glm::mat4 getRotation(int i);
|
||||
void rollForward(glm::mat3 rotCamera);
|
||||
void addStaticGroundPlane();
|
||||
void addTerrain(int width, int length, float** heightData);
|
||||
void addPlayer(float rad, float x, float y, float z, float mass, unsigned indice);
|
||||
void addSphere(float rad, float x, float y, float z, float mass, unsigned indice);
|
||||
@ -47,6 +49,7 @@ class Physics {
|
||||
btRigidBody* playerBody;
|
||||
btRigidBody* terrainBody;
|
||||
std::vector<btRigidBody*> bodies; //list of all bodies. bodies are also in world, but save again to ease cleaning up process.
|
||||
btRigidBody* staticGroundBody;
|
||||
|
||||
|
||||
btDynamicsWorld* world; //contains physical attributes of the world.
|
||||
|
Loading…
Reference in New Issue
Block a user