From 25816c5658113dff26c7ab6d0eedfaeef19ebd7c Mon Sep 17 00:00:00 2001 From: Sergey Lapin Date: Thu, 4 Sep 2025 01:14:10 +0300 Subject: [PATCH] Tweaked water physics --- src/gamedata/CharacterModule.cpp | 31 +++++++++++++++++++++++-------- src/gamedata/CharacterModule.h | 1 + src/gamedata/WaterModule.cpp | 10 ++++++++-- water/water.vert | 8 +++++--- 4 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/gamedata/CharacterModule.cpp b/src/gamedata/CharacterModule.cpp index 980e21c..e3877b7 100644 --- a/src/gamedata/CharacterModule.cpp +++ b/src/gamedata/CharacterModule.cpp @@ -15,11 +15,16 @@ CharacterModule::CharacterModule(flecs::world &ecs) AnimationControl::ANIM_NONE, false, false }); player.set( - { "normal-male.glb", nullptr, nullptr, nullptr }); + { "normal-male.glb", 0.0f, nullptr, nullptr, nullptr }); player.set( { nullptr, nullptr, nullptr, { 0, 0, 0 }, false, false }); player.add(); player.add(); + ecs.system("UpdateTimer") + .kind(flecs::OnUpdate) + .each([this](EngineData &eng, CharacterBase &ch) { + ch.mTimer += eng.delta; + }); ecs.system("HandleInput") .kind(flecs::OnUpdate) .each([this](Input &input, Camera &camera) { @@ -191,20 +196,30 @@ CharacterModule::CharacterModule(flecs::world &ecs) Ogre::Vector3 colNormal; bool is_on_floor = false; bool penetration = false; - Ogre::Vector3 gravity(0, -9.8, 0); + Ogre::Vector3 gravity(0, -9.8f, 0); if (e.has()) { + float volume = 2.0f * 0.5f * 0.5f; + float density = 900.0f; float full_subm = 2.0f; + float mass = 80.0f; + float multiplier = 0.25f; float current_subm = -Ogre::Math::Clamp( - pos.y, -full_subm, 0.0f); - float b = 9.0 * current_subm / full_subm; - body.gvelocity += - (gravity + Ogre::Vector3(0, b, 0)) * - delta; + pos.y + Ogre::Math::Sin(ch.mTimer * + 0.13f + + 130.0f) * + 0.07f, + -full_subm, 0.0f); + + Ogre::Vector3 b = -gravity * density * volume * + multiplier * current_subm / + full_subm / mass; + body.gvelocity += (gravity + b) * delta; body.gvelocity.y = Ogre::Math::Clamp( - body.gvelocity.y, -1.0f, 1.0f); + body.gvelocity.y, -2.5f, 2.5f); std::cout << "InWater!!!!!!!\n"; } else body.gvelocity += gravity * delta; + body.gvelocity *= 0.99; velocity += body.gvelocity; Ogre::Vector3 rotMotion = velocity * delta; btVector3 currentPosition = diff --git a/src/gamedata/CharacterModule.h b/src/gamedata/CharacterModule.h index 484f773..18607c3 100644 --- a/src/gamedata/CharacterModule.h +++ b/src/gamedata/CharacterModule.h @@ -9,6 +9,7 @@ struct Character {}; /* tag */ struct Player {}; /* tag */ struct CharacterBase { Ogre::String type; + float mTimer; Ogre::SceneNode *mBodyNode; Ogre::Entity *mBodyEnt; Ogre::Skeleton *mSkeleton; diff --git a/src/gamedata/WaterModule.cpp b/src/gamedata/WaterModule.cpp index 28be7e4..c719a8c 100644 --- a/src/gamedata/WaterModule.cpp +++ b/src/gamedata/WaterModule.cpp @@ -300,21 +300,27 @@ WaterModule::WaterModule(flecs::world &ecs) int i; if (!body.mWaterBody) { btCompoundShape *shape = new btCompoundShape; + btBoxShape *boxShape0 = new btBoxShape( + btVector3(1000, 10, 1000)); btBoxShape *boxShape1 = new btBoxShape( btVector3(1000, 20, 1000)); btBoxShape *boxShape2 = new btBoxShape( btVector3(1000, 100, 1000)); btBoxShape *boxShape3 = new btBoxShape( btVector3(1000, 1000, 1000)); + btTransform boxShapeXform0(btQuaternion(), + btVector3(0, -10.2f, + 0)); btTransform boxShapeXform1(btQuaternion(), - btVector3(0, -20.2f, + btVector3(0, -30.2f, 0)); btTransform boxShapeXform2(btQuaternion(), - btVector3(0, -120.2f, + btVector3(0, -130.2f, 0)); btTransform boxShapeXform3( btQuaternion(), btVector3(0, -1120.2f, 0)); + shape->addChildShape(boxShapeXform0, boxShape0); shape->addChildShape(boxShapeXform1, boxShape1); shape->addChildShape(boxShapeXform2, boxShape2); shape->addChildShape(boxShapeXform3, boxShape3); diff --git a/water/water.vert b/water/water.vert index a9526bf..3448d8a 100644 --- a/water/water.vert +++ b/water/water.vert @@ -42,9 +42,10 @@ uniform float noise; // the noise perturb as a factor of the time ) #line 41 //vec4 wave_a = vec4(1.0, 1.0, 0.35, 3.0); // xy = Direction, z = Steepness, w = Length -f32vec4 wave_a = vec4(1.0, 1.0, 0.55, 24.3); // xy = Direction, z = Steepness, w = Length -f32vec4 wave_b = vec4(1.0, 0.6, 0.29, 12.55); // xy = Direction, z = Steepness, w = Length -f32vec4 wave_c = vec4(1.0, 1.41, 0.11, 10.8); // xy = Direction, z = Steepness, w = Length +f32vec4 wave_a = vec4(1.0, 1.0, 0.08, 35.3); // xy = Direction, z = Steepness, w = Length +f32vec4 wave_b = vec4(1.0, 0.6, 0.02, 21.55); // xy = Direction, z = Steepness, w = Length +f32vec4 wave_c = vec4(1.0, 1.41, 0.01, 0.8); // xy = Direction, z = Steepness, w = Length +f32vec4 wave_d = vec4(-1.2, 1.0, 0.01, 0.1); // xy = Direction, z = Steepness, w = Length f32vec4 wave(f32vec4 parameter, f32vec2 position, float32_t t, inout f32vec3 tangent, inout f32vec3 binormal) { @@ -102,6 +103,7 @@ MAIN_DECLARATION position += wave(wave_a, vertex_position.xz, time, tang, bin); position += wave(wave_b, vertex_position.xz, time, tang, bin); position += wave(wave_c, vertex_position.xz, time, tang, bin); + position += wave(wave_d, vertex_position.xz, time, tang, bin); vtangent = tang; vbinormal = bin; vertex_position = position.xyz;