From a077f8f493266aac00ce119e5f20ae81c1dc80ed Mon Sep 17 00:00:00 2001 From: RaphaelIT7 <64648134+RaphaelIT7@users.noreply.github.com> Date: Sun, 16 Nov 2025 23:22:47 +0100 Subject: [PATCH] vphysics: expose two new virtual functions to support multiple physics environments --- game/shared/physics_shared.cpp | 5 +---- public/vphysics_interface.h | 11 +++++++++++ vphysics/physics_object.cpp | 6 ++++++ vphysics/physics_object.h | 5 ++++- 4 files changed, 22 insertions(+), 5 deletions(-) diff --git a/game/shared/physics_shared.cpp b/game/shared/physics_shared.cpp index 36c599827..67dd5515f 100644 --- a/game/shared/physics_shared.cpp +++ b/game/shared/physics_shared.cpp @@ -500,10 +500,7 @@ void PhysDestroyObject( IPhysicsObject *pObject, CBaseEntity *pEntity ) g_EntityCollisionHash->RemoveAllPairsForObject( pEntity ); } - if ( physenv ) - { - physenv->DestroyObject( pObject ); - } + DestroyPhysicsObject( pObject ); } static void AddSurfacepropFile( const char *pFileName, IPhysicsSurfaceProps *pProps, IFileSystem *pFileSystem ) diff --git a/public/vphysics_interface.h b/public/vphysics_interface.h index 8de9780fe..805dfb2d3 100644 --- a/public/vphysics_interface.h +++ b/public/vphysics_interface.h @@ -863,8 +863,19 @@ abstract_class IPhysicsObject // dumps info about the object to Msg() virtual void OutputDebugInfo() const = 0; + // returns the buoyancy of the object. + // newly added by Valve in new builds since it was requested by Rubat to solve https://github.com/Facepunch/garrysmod-issues/issues/5696 + virtual float GetBuoyancyRatio( void ) const = 0; + + // returns the environment this object belongs to + virtual IPhysicsEnvironment *GetEnvironment( void ) const = 0; }; +// Helper function - destroys the given object using the right environment +static inline void DestroyPhysicsObject( IPhysicsObject* pObject ) +{ + pObject->GetEnvironment()->DestroyObject( pObject ); +} abstract_class IPhysicsSpring { diff --git a/vphysics/physics_object.cpp b/vphysics/physics_object.cpp index e9f4a5ff7..b41947f0c 100644 --- a/vphysics/physics_object.cpp +++ b/vphysics/physics_object.cpp @@ -1454,6 +1454,12 @@ void CPhysicsObject::OutputDebugInfo() const } } +IPhysicsEnvironment *CPhysicsObject::GetEnvironment() const +{ + // Same as GetVPhysicsEnvironment but this function is exposed for the game dlls + return (CPhysicsEnvironment *) (m_pObject->get_environment()->client_data); +} + bool CPhysicsObject::IsAttachedToConstraint( bool bExternalOnly ) const { if ( m_pObject ) diff --git a/vphysics/physics_object.h b/vphysics/physics_object.h index 6f91235ff..f7921445a 100644 --- a/vphysics/physics_object.h +++ b/vphysics/physics_object.h @@ -187,6 +187,10 @@ class CPhysicsObject : public IPhysicsObject void OutputDebugInfo() const override; + [[nodiscard]] float GetBuoyancyRatio( void ) const override { return m_buoyancyRatio; } + + [[nodiscard]] IPhysicsEnvironment *GetEnvironment() const override; + // local functions [[nodiscard]] inline IVP_Real_Object *GetObject( void ) const { return m_pObject; } // dimhotepus: int -> unsigned short. @@ -202,7 +206,6 @@ class CPhysicsObject : public IPhysicsObject [[nodiscard]] inline intp GetActiveIndex( void ) const { return m_activeIndex; } inline void SetActiveIndex( intp index ) { m_activeIndex = index; } - [[nodiscard]] inline float GetBuoyancyRatio( void ) const { return m_buoyancyRatio; } // returns true if the mass center is set to the default for the collision model [[nodiscard]] bool IsMassCenterAtDefault() const;