Fix defaultplayercontroller

This commit is contained in:
MrLetsplay 2022-11-14 20:42:13 +01:00
parent 459bf87869
commit f4cdc01ff3
5 changed files with 10 additions and 19 deletions

View File

@ -182,19 +182,12 @@ void init() {
positions[1] = 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}; float radii[2] = {KEK_PLAYER_RADIUS, KEK_PLAYER_RADIUS};
btCollisionShape *shape = new btMultiSphereShape(positions, radii, 2); btCollisionShape *shape = new btMultiSphereShape(positions, radii, 2);
btRigidBody *body = new btRigidBody(KEK_PLAYER_MASS, nullptr, shape); btRigidBody *body = new btRigidBody(0, nullptr, shape);
body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT); body->setCollisionFlags(body->getCollisionFlags() | btCollisionObject::CF_KINEMATIC_OBJECT);
body->setFriction(3); body->setFriction(3);
body->setActivationState(DISABLE_DEACTIVATION); body->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addRigidBody(body); kekData.physics->world->addRigidBody(body);
/*btCollisionShape *jumpShape = new btSphereShape(KEK_PLAYER_RADIUS / 2);
btGhostObject *jumpCollider = new btGhostObject();
jumpCollider->setCollisionShape(jumpShape);
jumpCollider->setCollisionFlags(jumpCollider->getCollisionFlags() | btCollisionObject::CF_NO_CONTACT_RESPONSE);
jumpCollider->setActivationState(DISABLE_DEACTIVATION);
kekData.physics->world->addCollisionObject(jumpCollider);*/
btTransform from; btTransform from;
from.setIdentity(); from.setIdentity();
from.setOrigin(btVector3(0,0,0)); from.setOrigin(btVector3(0,0,0));

View File

@ -62,11 +62,11 @@ glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
normal = glm::normalize(normal); normal = glm::normalize(normal);
float angle = 90.0f - glm::angle(normal, glm::normalize(movement)) / M_PI * 90.0f; totalMovement += movement * frac;
float angle = 90.0f - glm::angle(normal, glm::normalize(movement)) / M_PI * 90.0f;
if(angle < minSlideAngle) break; if(angle < minSlideAngle) break;
totalMovement += movement * frac;
totalMovement += normal * 0.001f; totalMovement += normal * 0.001f;
glm::vec3 remainingProjected = glm::normalize(glm::cross(normal, glm::cross(movement, normal))) * glm::length(movement) * (1 - frac); glm::vec3 remainingProjected = glm::normalize(glm::cross(normal, glm::cross(movement, normal))) * glm::length(movement) * (1 - frac);
@ -79,19 +79,20 @@ glm::vec3 DefaultPlayerController::move(glm::vec3 movement) {
} }
void DefaultPlayerController::jump() { void DefaultPlayerController::jump() {
velocity += glm::vec3(0, 10 * jumpHeight, 0) * kekData.lastFrameTime; velocity += glm::vec3(0, jumpVelocity, 0);
} }
void DefaultPlayerController::update() { void DefaultPlayerController::update() {
bool onGround = checkPlayerGrounded(); bool onGround = checkPlayerGrounded();
kekData.player->onGround = onGround; kekData.player->onGround = onGround;
move(velocity * kekData.lastFrameTime);
if(!onGround) { if(!onGround) {
velocity += gravity * kekData.lastFrameTime; velocity += gravity * kekData.lastFrameTime;
}else{ }else{
velocity = glm::vec3(0); velocity = glm::vec3(0);
} }
move(velocity);
} }
} }

View File

@ -46,6 +46,3 @@
#define KEK_PLAYER_HEIGHT 2 #define KEK_PLAYER_HEIGHT 2
#define KEK_PLAYER_RADIUS 0.5f #define KEK_PLAYER_RADIUS 0.5f
#define KEK_PLAYER_EYE_OFFSET (KEK_PLAYER_HEIGHT / 2 - KEK_PLAYER_RADIUS) #define KEK_PLAYER_EYE_OFFSET (KEK_PLAYER_HEIGHT / 2 - KEK_PLAYER_RADIUS)
#define KEK_PLAYER_MASS 50
#define KEK_PLAYER_WALK_VELOCITY 6.0f
#define KEK_PLAYER_JUMP_VELOCITY 2.4f

View File

@ -13,7 +13,7 @@ public:
glm::vec3 gravity = glm::vec3(0,-9.81,0); glm::vec3 gravity = glm::vec3(0,-9.81,0);
float minSlideAngle = 5; float minSlideAngle = 5;
float maxWalkAngle = 60; float maxWalkAngle = 60;
float jumpHeight = 1; float jumpVelocity = 2.5;
float groundDistance = 0.01; float groundDistance = 0.01;
glm::vec3 velocity = glm::vec3(0); glm::vec3 velocity = glm::vec3(0);

View File

@ -81,13 +81,13 @@ int main(int argc, char **argv) {
test->moveTo(glm::vec3(0,5,0)); test->moveTo(glm::vec3(0,5,0));
scene->addObject(test);*/ scene->addObject(test);*/
/*Mesh *mesh2 = ObjParser::loadMesh("object/cube_colored/Cube.obj"); Mesh *mesh2 = ObjParser::loadMesh("object/cube_colored/Cube.obj");
GameObject *test3 = new GameObject(); GameObject *test3 = new GameObject();
btCollisionShape *shape2 = new btBoxShape(btVector3(1,1,1)); btCollisionShape *shape2 = new btBoxShape(btVector3(1,1,1));
test3->addPhysics(shape2, 0, btCollisionObject::CF_STATIC_OBJECT); test3->addPhysics(shape2, 0, btCollisionObject::CF_STATIC_OBJECT);
test3->addMesh(mesh2); test3->addMesh(mesh2);
test3->moveTo(glm::vec3(2, 1, 2)); test3->moveTo(glm::vec3(2, 1, 2));
scene->addObject(test3);*/ scene->addObject(test3);
/*for(int i = 0; i < 10; i++) { /*for(int i = 0; i < 10; i++) {
GameObject *test2 = new GameObject(); GameObject *test2 = new GameObject();