Improve physics
This commit is contained in:
parent
4d78ca9d94
commit
6e45e0bc34
@ -42,8 +42,10 @@ static void defaultInput(GLFWwindow *window, void *data) {
|
||||
direction += glm::normalize(glm::cross(kekData.activeCamera->direction, glm::vec3(0.0f, 1.0f, 0.0f)));
|
||||
}
|
||||
|
||||
bool jump = false;
|
||||
if(Input::getKeyState(keyUp) == GLFW_PRESS) {
|
||||
direction += glm::vec3(0,1,0);
|
||||
jump = true;
|
||||
}
|
||||
|
||||
if(Input::getKeyState(keyDown) == GLFW_PRESS) {
|
||||
@ -55,7 +57,12 @@ static void defaultInput(GLFWwindow *window, void *data) {
|
||||
if(kekData.player->noclip) {
|
||||
kekData.activeCamera->translate(direction * KEK_NOCLIP_SPEED * kekData.lastFrameTime);
|
||||
}else {
|
||||
kekData.player->physics->body->applyCentralImpulse(Physics::fromGLM(glm::normalize(direction) * (kekData.lastFrameTime * 20)));
|
||||
//kekData.player->physics->body->applyCentralImpulse(Physics::fromGLM(glm::normalize(direction) * (kekData.lastFrameTime * 20)));
|
||||
btVector3 vel = kekData.player->physics->body->getLinearVelocity();
|
||||
glm::vec3 newVel = glm::normalize(direction) * 6.0f;
|
||||
newVel.y = vel.y();
|
||||
if(jump) newVel.y = 6.0f;
|
||||
kekData.player->physics->body->setLinearVelocity(Physics::fromGLM(newVel));
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -163,11 +170,17 @@ void init() {
|
||||
//UI::addElement(options);
|
||||
|
||||
kekData.player = new Player();
|
||||
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1));
|
||||
kekData.player->physics = new PhysicsObjectData();
|
||||
btRigidBody *body = new btRigidBody(1, nullptr, shape);
|
||||
btVector3 positions[2];
|
||||
positions[0] = btVector3(0, -KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
|
||||
positions[1] = btVector3(0, -KEK_PLAYER_HEIGHT / 2 + KEK_PLAYER_RADIUS, 0);
|
||||
float radii[2] = {KEK_PLAYER_RADIUS, KEK_PLAYER_RADIUS};
|
||||
btCollisionShape *shape = new btMultiSphereShape(positions, radii, 2);
|
||||
btRigidBody *body = new btRigidBody(KEK_PLAYER_MASS, nullptr, shape);
|
||||
body->setFriction(3);
|
||||
body->setActivationState(DISABLE_DEACTIVATION);
|
||||
kekData.physics->world->addRigidBody(body);
|
||||
|
||||
kekData.player->physics = new PhysicsObjectData();
|
||||
kekData.player->physics->body = body;
|
||||
kekData.player->moveTo(glm::vec3(0,10,0));
|
||||
}
|
||||
|
@ -34,7 +34,10 @@ void GameObject::draw(Shader *shader) {
|
||||
|
||||
void GameObject::addPhysics(btCollisionShape *shape, float mass, int collisionFlags) {
|
||||
this->physics = new PhysicsObjectData();
|
||||
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape);
|
||||
btVector3 inertia = btVector3(0,0,0);
|
||||
shape->calculateLocalInertia(mass, inertia);
|
||||
btRigidBody *body = new btRigidBody(mass, new btDefaultMotionState(), shape, inertia);
|
||||
body->setFriction(1);
|
||||
kekData.physics->world->addRigidBody(body);
|
||||
body->setCollisionFlags(collisionFlags);
|
||||
this->physics->body = body;
|
||||
|
@ -34,7 +34,6 @@ void step(float deltaT) {
|
||||
for(GameObject *obj : kekData.activeScene->objects) {
|
||||
if(obj->physics) {
|
||||
obj->physics->body->getWorldTransform().setOrigin(fromGLM(obj->getPosition()));
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -46,3 +46,4 @@
|
||||
#define KEK_PLAYER_HEIGHT 2
|
||||
#define KEK_PLAYER_RADIUS 0.5f
|
||||
#define KEK_PLAYER_EYE_OFFSET (KEK_PLAYER_HEIGHT / 2 - KEK_PLAYER_RADIUS)
|
||||
#define KEK_PLAYER_MASS 50
|
||||
|
@ -25,7 +25,7 @@ public:
|
||||
|
||||
void draw(Shader *shader);
|
||||
|
||||
void addPhysics(btCollisionShape *shape, float mass = 1, int collisionFlags = 0);
|
||||
void addPhysics(btCollisionShape *shape, float mass, int collisionFlags = 0);
|
||||
|
||||
virtual void rotate(float angle, glm::vec3 axis) { rotateTo(glm::rotate(getRotation(), angle, axis)); };
|
||||
|
||||
|
@ -60,7 +60,7 @@ int main(int argc, char **argv) {
|
||||
Mesh *mesh = ObjParser::loadMesh("object/sphere/Sphere.obj");
|
||||
GameObject *test = new GameObject();
|
||||
btCollisionShape *shape = new btSphereShape(1);
|
||||
test->addPhysics(shape);
|
||||
test->addPhysics(shape, 10);
|
||||
test->addMesh(mesh);
|
||||
test->moveTo(glm::vec3(0,5,0));
|
||||
scene->addObject(test);
|
||||
@ -77,7 +77,7 @@ int main(int argc, char **argv) {
|
||||
for(int i = 0; i < 10; i++) {
|
||||
GameObject *test2 = new GameObject();
|
||||
btCollisionShape *shape = new btBoxShape(btVector3(1,1,1));
|
||||
test2->addPhysics(shape);
|
||||
test2->addPhysics(shape, 10);
|
||||
test2->addMesh(ObjParser::loadMesh("object/cube_colored/Cube.obj"));
|
||||
test2->moveTo(glm::vec3(1.0f, 5.0f, 3 * i));
|
||||
scene->addObject(test2);
|
||||
|
Loading…
Reference in New Issue
Block a user