remove camera latency

This commit is contained in:
MihailRis
2024-07-15 14:06:49 +03:00
parent 8bb736bef0
commit af1b32d91d
5 changed files with 59 additions and 51 deletions
+35 -35
View File
@@ -62,25 +62,11 @@ Hitbox* Player::getHitbox() {
}
void Player::updateInput(PlayerInput& input, float delta) {
auto entity = level->entities->get(eid);
if (!entity.has_value()) {
auto hitbox = getHitbox();
if (hitbox == nullptr) {
return;
}
auto& hitbox = entity->getRigidbody().hitbox;
auto& skeleton = entity->getSkeleton();
skeleton.visible = currentCamera != camera;
size_t bodyIndex = skeleton.config->find("body")->getIndex();
size_t headIndex = skeleton.config->find("head")->getIndex();
skeleton.pose.matrices[bodyIndex] =
glm::rotate(glm::mat4(1.0f), glm::radians(cam.x-90), glm::vec3(0, 1, 0));
skeleton.pose.matrices[headIndex] = glm::rotate(glm::rotate(
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.6f, 0.0f)),
glm::radians(-cam.y), glm::vec3(0, 0, 1)), glm::radians(90.0f), glm::vec3(0, 1, 0));
bool crouch = input.shift && hitbox.grounded && !input.sprint;
bool crouch = input.shift && hitbox->grounded && !input.sprint;
float speed = this->speed;
if (flight){
speed *= FLIGHT_SPEED_MUL;
@@ -89,7 +75,7 @@ void Player::updateInput(PlayerInput& input, float delta) {
speed *= CHEAT_SPEED_MUL;
}
hitbox.crouching = crouch;
hitbox->crouching = crouch;
if (crouch) {
speed *= CROUCH_SPEED_MUL;
} else if (input.sprint) {
@@ -111,37 +97,37 @@ void Player::updateInput(PlayerInput& input, float delta) {
}
if (glm::length(dir) > 0.0f){
dir = glm::normalize(dir);
hitbox.velocity += dir * speed * delta * 9.0f;
hitbox->velocity += dir * speed * delta * 9.0f;
}
hitbox.linearDamping = PLAYER_GROUND_DAMPING;
hitbox.verticalDamping = flight;
hitbox.gravityScale = flight ? 0.0f : 1.0f;
hitbox->linearDamping = PLAYER_GROUND_DAMPING;
hitbox->verticalDamping = flight;
hitbox->gravityScale = flight ? 0.0f : 1.0f;
if (flight){
hitbox.linearDamping = PLAYER_AIR_DAMPING;
hitbox->linearDamping = PLAYER_AIR_DAMPING;
if (input.jump){
hitbox.velocity.y += speed * delta * 9;
hitbox->velocity.y += speed * delta * 9;
}
if (input.shift){
hitbox.velocity.y -= speed * delta * 9;
hitbox->velocity.y -= speed * delta * 9;
}
}
if (!hitbox.grounded) {
hitbox.linearDamping = PLAYER_AIR_DAMPING;
if (!hitbox->grounded) {
hitbox->linearDamping = PLAYER_AIR_DAMPING;
}
if (input.jump && hitbox.grounded){
hitbox.velocity.y = JUMP_FORCE;
if (input.jump && hitbox->grounded){
hitbox->velocity.y = JUMP_FORCE;
}
if ((input.flight && !noclip) ||
(input.noclip && flight == noclip)){
flight = !flight;
if (flight){
hitbox.velocity.y += 1.0f;
hitbox->velocity.y += 1.0f;
}
}
hitbox.type = noclip ? BodyType::KINEMATIC : BodyType::DYNAMIC;
hitbox->type = noclip ? BodyType::KINEMATIC : BodyType::DYNAMIC;
if (input.noclip) {
noclip = !noclip;
}
@@ -150,18 +136,32 @@ void Player::updateInput(PlayerInput& input, float delta) {
}
void Player::postUpdate() {
auto hitbox = getHitbox();
if (hitbox == nullptr) {
auto entity = level->entities->get(eid);
if (!entity.has_value()) {
return;
}
position = hitbox->position;
auto& hitbox = entity->getRigidbody().hitbox;
position = hitbox.position;
if (flight && hitbox->grounded) {
if (flight && hitbox.grounded) {
flight = false;
}
if (spawnpoint.y <= 0.1) {
attemptToFindSpawnpoint();
}
auto& skeleton = entity->getSkeleton();
skeleton.visible = currentCamera != camera;
size_t bodyIndex = skeleton.config->find("body")->getIndex();
size_t headIndex = skeleton.config->find("head")->getIndex();
skeleton.pose.matrices[bodyIndex] =
glm::rotate(glm::mat4(1.0f), glm::radians(cam.x-90), glm::vec3(0, 1, 0));
skeleton.pose.matrices[headIndex] = glm::rotate(glm::rotate(
glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.4f, 0.0f)),
glm::radians(-cam.y), glm::vec3(0, 0, 1)), glm::radians(90.0f), glm::vec3(0, 1, 0));
}
void Player::teleport(glm::vec3 position) {