From 4faf07cba345d68869e01a2bf5abf91463f90c67 Mon Sep 17 00:00:00 2001 From: Daniel Evans Date: Wed, 1 Apr 2015 04:36:52 +0100 Subject: [PATCH] Fix character inWater behaviour --- rwengine/include/engine/GameObject.hpp | 6 +++--- rwengine/src/objects/CharacterObject.cpp | 15 ++++++++++++--- rwengine/src/objects/InstanceObject.cpp | 8 ++++---- rwengine/src/objects/VehicleObject.cpp | 12 ++++++------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/rwengine/include/engine/GameObject.hpp b/rwengine/include/engine/GameObject.hpp index 08ca4237..46c418b0 100644 --- a/rwengine/include/engine/GameObject.hpp +++ b/rwengine/include/engine/GameObject.hpp @@ -45,7 +45,7 @@ public: */ float mHealth; - bool _inWater; + bool inWater; /** * @brief stores the height of water at the last tick @@ -60,7 +60,7 @@ public: GameObject(GameWorld* engine, const glm::vec3& pos, const glm::quat& rot, ModelRef model) : _lastPosition(pos), _lastRotation(rot), position(pos), rotation(rot), model(model), engine(engine), animator(nullptr), skeleton(nullptr), mHealth(0.f), - _inWater(false), _lastHeight(std::numeric_limits::max()), visible(true), + inWater(false), _lastHeight(std::numeric_limits::max()), visible(true), lifetime(GameObject::UnknownLifetime) {} @@ -139,7 +139,7 @@ public: virtual bool isAnimationFixed() const { return true; } - virtual bool isInWater() const { return _inWater; } + virtual bool isInWater() const { return inWater; } virtual void tick(float dt) = 0; diff --git a/rwengine/src/objects/CharacterObject.cpp b/rwengine/src/objects/CharacterObject.cpp index 3e9c4ef7..1ec0d05b 100644 --- a/rwengine/src/objects/CharacterObject.cpp +++ b/rwengine/src/objects/CharacterObject.cpp @@ -230,7 +230,15 @@ void CharacterObject::updateCharacter(float dt) float wh = engine->gameData.waterHeights[wi]; auto ws = getPosition(); wh += engine->gameData.getWaveHeightAt(ws); - if( ws.z < wh ) { + + // If Not in water before + // If last position was above water + // Now Underwater + // Else Not Underwater + // Else + // Underwater + + if( ! inWater && ws.z < wh && _lastHeight > wh ) { ws.z = wh; btVector3 bpos(ws.x, ws.y, ws.z); @@ -240,13 +248,14 @@ void CharacterObject::updateCharacter(float dt) physObject->setWorldTransform(wt); physCharacter->setGravity(0.f); - _inWater = true; + inWater = true; } else { physCharacter->setGravity(9.81f); - _inWater = false; + inWater = false; } } + _lastHeight = getPosition().z; } } diff --git a/rwengine/src/objects/InstanceObject.cpp b/rwengine/src/objects/InstanceObject.cpp index 13e2ba4f..794455fe 100644 --- a/rwengine/src/objects/InstanceObject.cpp +++ b/rwengine/src/objects/InstanceObject.cpp @@ -65,19 +65,19 @@ void InstanceObject::tick(float dt) wH = engine->gameData.waterHeights[hI]; wH += engine->gameData.getWaveHeightAt(ws); if( vH <= wH ) { - _inWater = true; + inWater = true; } else { - _inWater = false; + inWater = false; } } else { - _inWater = false; + inWater = false; } } _lastHeight = ws.z; - if( _inWater ) { + if( inWater ) { float oZ = -(body->collisionHeight * (dynamics->bouancy/100.f)); body->body->activate(true); // Damper motion diff --git a/rwengine/src/objects/VehicleObject.cpp b/rwengine/src/objects/VehicleObject.cpp index d49ce251..b8d1e0dd 100644 --- a/rwengine/src/objects/VehicleObject.cpp +++ b/rwengine/src/objects/VehicleObject.cpp @@ -245,24 +245,24 @@ void VehicleObject::tickPhysics(float dt) // and was not underwater here in the last tick if( _lastHeight >= wH ) { // we are for real, underwater - _inWater = true; + inWater = true; } - else if( _inWater == false ) { + else if( inWater == false ) { // It's just a tunnel or something, we good. - _inWater = false; + inWater = false; } } else { // The water is beneath us - _inWater = false; + inWater = false; } } else { - _inWater = false; + inWater = false; } } - if( _inWater ) { + if( inWater ) { // Ensure that vehicles don't fall asleep at the top of a wave. if(! physBody->isActive() ) {