Skip to content

Fix isPedOnGround #4317

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Client/game_sa/CPedSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,7 @@ class CPedSA : public virtual CPed, public virtual CPhysicalSA
void SetFightingStyle(eFightingStyle style, std::uint8_t styleExtra = 6) override;

CEntity* GetContactEntity() const override;
bool IsStandingOnEntity() const override { return GetPedInterface()->pContactEntity != nullptr; };

int GetRunState() const override { return GetPedInterface()->moveState; }

Expand Down
12 changes: 10 additions & 2 deletions Client/mods/deathmatch/logic/CClientPed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4649,12 +4649,20 @@ float CClientPed::GetDistanceFromGround()
return (vecPosition.fZ - fGroundLevel);
}

bool CClientPed::IsOnGround()
bool CClientPed::IsOnGround(bool checkVehicles)
{
CVector vecPosition;
GetPosition(vecPosition);
float fGroundLevel = static_cast<float>(g_pGame->GetWorld()->FindGroundZFor3DPosition(&vecPosition));
return (vecPosition.fZ > fGroundLevel && (vecPosition.fZ - fGroundLevel) <= 1.0f);

if (definitelyLessThan(vecPosition.fZ, fGroundLevel))
return false;

bool isOnGround = definitelyLessThan((vecPosition.fZ - fGroundLevel), 1.0f) || essentiallyEqual((vecPosition.fZ - fGroundLevel), 1.0f);
if (!isOnGround && checkVehicles && m_pPlayerPed)
return m_pPlayerPed->IsStandingOnEntity();

return isOnGround;
}

bool CClientPed::IsClimbing()
Expand Down
2 changes: 1 addition & 1 deletion Client/mods/deathmatch/logic/CClientPed.h
Original file line number Diff line number Diff line change
Expand Up @@ -374,7 +374,7 @@ class CClientPed : public CClientStreamElement, public CAntiCheatModule

void SetInWater(bool bIsInWater) { m_bIsInWater = bIsInWater; };
bool IsInWater();
bool IsOnGround();
bool IsOnGround(bool checkVehicles = false);

bool IsClimbing();
bool IsRadioOn() const noexcept { return m_bRadioOn; };
Expand Down
4 changes: 3 additions & 1 deletion Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -914,13 +914,15 @@ int CLuaPedDefs::IsPedOnGround(lua_State* luaVM)
{
// Verify the argument
CClientPed* pPed = NULL;
bool checkVehicles = false;
CScriptArgReader argStream(luaVM);
argStream.ReadUserData(pPed);
argStream.ReadBool(checkVehicles, false);

if (!argStream.HasErrors())
{
// Find out whether he's on the ground or not and return it
bool bOnGround = pPed->IsOnGround();
bool bOnGround = pPed->IsOnGround(checkVehicles);
lua_pushboolean(luaVM, bOnGround);
return 1;
}
Expand Down
1 change: 1 addition & 0 deletions Client/sdk/game/CPed.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class CPed : public virtual CPhysical
virtual void SetFightingStyle(eFightingStyle style, std::uint8_t styleExtra) = 0;

virtual CEntity* GetContactEntity() const = 0;
virtual bool IsStandingOnEntity() const = 0;

virtual int GetRunState() const = 0;

Expand Down
20 changes: 20 additions & 0 deletions Shared/mods/deathmatch/logic/Utils.cpp
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we already have functions that do these operations?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't find any

Original file line number Diff line number Diff line change
Expand Up @@ -1107,3 +1107,23 @@ CVector ConvertEulerRotationOrder(const CVector& a_vRotation, eEulerRotationOrde
return a_vRotation;
}
}

bool approximatelyEqual(float a, float b, float epsilon)
{
return std::fabs(a - b) <= std::max(std::fabs(a), std::fabs(b)) * epsilon;
}

bool essentiallyEqual(float a, float b, float epsilon)
{
return std::fabs(a - b) <= std::min(std::fabs(a), std::fabs(b)) * epsilon;
}

bool definitelyGreaterThan(float a, float b, float epsilon)
{
return (a - b) > std::max(std::fabs(a), std::fabs(b)) * epsilon;
}

bool definitelyLessThan(float a, float b, float epsilon)
{
return (b - a) > std::max(std::fabs(a), std::fabs(b)) * epsilon;
}
6 changes: 6 additions & 0 deletions Shared/mods/deathmatch/logic/Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,3 +336,9 @@ void DeletePointersAndClearList(T& elementList)
delete *iter;
}
}

// Comparing floating point numbers
bool approximatelyEqual(float a, float b, float epsilon = FLOAT_EPSILON);
bool essentiallyEqual(float a, float b, float epsilon = FLOAT_EPSILON);
bool definitelyGreaterThan(float a, float b, float epsilon = FLOAT_EPSILON);
bool definitelyLessThan(float a, float b, float epsilon = FLOAT_EPSILON);
Loading