Skip to content

Commit 10098b0

Browse files
gownosatanatederisborsuczyna
authored
Add bone quaternion functions (#3679)
* add bone quaternion functions * remove test prints * add lua bone id checks * new error method * different error approach * use unsigned int * fix header files build fail * rename functions * fix the brackets --------- Co-authored-by: = <=> Co-authored-by: TEDERIs <[email protected]> Co-authored-by: borsuczyna <[email protected]>
1 parent 0ca4daa commit 10098b0

File tree

5 files changed

+96
-12
lines changed

5 files changed

+96
-12
lines changed

Client/game_sa/CEntitySA.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,28 @@ bool CEntitySA::GetBoneRotation(eBone boneId, float& yaw, float& pitch, float& r
603603
return false;
604604
}
605605

606+
bool CEntitySA::GetBoneRotationQuat(eBone boneId, float& x, float& y, float& z, float& w)
607+
{
608+
RpClump* clump = GetRpClump();
609+
if (clump)
610+
{
611+
// updating the bone frame orientation will also update its children
612+
// This rotation is only applied when UpdateElementRpHAnim is called
613+
CAnimBlendClumpDataSAInterface* clumpDataInterface = *pGame->GetClumpData(clump);
614+
AnimBlendFrameData* frameData = clumpDataInterface->GetFrameDataByNodeId(boneId);
615+
if (frameData)
616+
{
617+
RtQuat* boneOrientation = &frameData->m_pIFrame->orientation;
618+
x = boneOrientation->imag.x;
619+
y = boneOrientation->imag.y;
620+
z = boneOrientation->imag.z;
621+
w = boneOrientation->real;
622+
return true;
623+
}
624+
}
625+
return false;
626+
}
627+
606628
bool CEntitySA::SetBoneRotation(eBone boneId, float yaw, float pitch, float roll)
607629
{
608630
RpClump* clump = GetRpClump();
@@ -628,6 +650,33 @@ bool CEntitySA::SetBoneRotation(eBone boneId, float yaw, float pitch, float roll
628650
return false;
629651
}
630652

653+
bool CEntitySA::SetBoneRotationQuat(eBone boneId, float x, float y, float z, float w)
654+
{
655+
RpClump* clump = GetRpClump();
656+
if (clump)
657+
{
658+
// updating the bone frame orientation will also update its children
659+
// This rotation is only applied when UpdateElementRpHAnim is called
660+
CAnimBlendClumpDataSAInterface* clumpDataInterface = *pGame->GetClumpData(clump);
661+
AnimBlendFrameData* frameData = clumpDataInterface->GetFrameDataByNodeId(boneId);
662+
if (frameData)
663+
{
664+
RtQuat* boneOrientation = &frameData->m_pIFrame->orientation;
665+
boneOrientation->imag.x = x;
666+
boneOrientation->imag.y = y;
667+
boneOrientation->imag.z = z;
668+
boneOrientation->real = w;
669+
CEntitySAInterface* theInterface = GetInterface();
670+
if (theInterface)
671+
{
672+
theInterface->bDontUpdateHierarchy = false;
673+
}
674+
return true;
675+
}
676+
}
677+
return false;
678+
}
679+
631680
bool CEntitySA::GetBonePosition(eBone boneId, CVector& position)
632681
{
633682
RwMatrix* rwBoneMatrix = GetBoneRwMatrix(boneId);

Client/game_sa/CEntitySA.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,9 @@ class CEntitySA : public virtual CEntity
324324
bool SetBoneMatrix(eBone boneId, const CMatrix& matrix);
325325

326326
bool GetBoneRotation(eBone boneId, float& yaw, float& pitch, float& roll);
327+
bool GetBoneRotationQuat(eBone boneId, float& x, float& y, float& z, float& w);
327328
bool SetBoneRotation(eBone boneId, float yaw, float pitch, float roll);
329+
bool SetBoneRotationQuat(eBone boneId, float x, float y, float z, float w);
328330
bool GetBonePosition(eBone boneId, CVector& position);
329331
bool SetBonePosition(eBone boneId, const CVector& position);
330332

Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,10 @@ void CLuaPedDefs::LoadFunctions()
5454
{"getPedBonePosition", GetPedBonePosition},
5555
{"setElementBonePosition", ArgumentParser<SetElementBonePosition>},
5656
{"setElementBoneRotation", ArgumentParser<SetElementBoneRotation>},
57+
{"setElementBoneQuaternion", ArgumentParser<SetElementBoneQuaternion>},
5758
{"getElementBonePosition", ArgumentParser<GetElementBonePosition>},
5859
{"getElementBoneRotation", ArgumentParser<GetElementBoneRotation>},
60+
{"getElementBoneQuaternion", ArgumentParser<GetElementBoneQuaternion>},
5961
{"setElementBoneMatrix", ArgumentParser<SetElementBoneMatrix>},
6062
{"getElementBoneMatrix", ArgumentParser<GetElementBoneMatrix>},
6163
{"updateElementRpHAnim", ArgumentParser<UpdateElementRpHAnim>},
@@ -998,19 +1000,31 @@ int CLuaPedDefs::CanPedBeKnockedOffBike(lua_State* luaVM)
9981000
return 1;
9991001
}
10001002

1001-
bool CLuaPedDefs::SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, CVector position)
1003+
bool CLuaPedDefs::SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CVector position)
10021004
{
10031005
CEntity* theEntity = entity->GetGameEntity();
10041006
return theEntity ? theEntity->SetBonePosition(static_cast<eBone>(boneId), position) : false;
10051007
}
10061008

1007-
bool CLuaPedDefs::SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, float yaw, float pitch, float roll)
1009+
bool CLuaPedDefs::SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float yaw, float pitch, float roll)
10081010
{
1011+
if (boneId > BONE_RIGHTFOOT)
1012+
throw LuaFunctionError("Invalid bone ID");
1013+
10091014
CEntity* theEntity = entity->GetGameEntity();
10101015
return theEntity ? theEntity->SetBoneRotation(static_cast<eBone>(boneId), yaw, pitch, roll) : false;
10111016
}
10121017

1013-
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId)
1018+
bool CLuaPedDefs::SetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float x, float y, float z, float w)
1019+
{
1020+
if (boneId > BONE_RIGHTFOOT)
1021+
throw LuaFunctionError("Invalid bone ID");
1022+
1023+
CEntity* theEntity = entity->GetGameEntity();
1024+
return theEntity ? theEntity->SetBoneRotationQuat(static_cast<eBone>(boneId), x, y, z, w) : false;
1025+
}
1026+
1027+
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
10141028
{
10151029
CEntity* theEntity = entity->GetGameEntity();
10161030
CVector position;
@@ -1019,22 +1033,37 @@ std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElement
10191033
return false;
10201034
}
10211035

1022-
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId)
1036+
std::variant<bool, CLuaMultiReturn<float, float, float>> CLuaPedDefs::GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
10231037
{
1038+
if (boneId > BONE_RIGHTFOOT)
1039+
throw LuaFunctionError("Invalid bone ID");
1040+
10241041
float yaw = 0.0f, pitch = 0.0f, roll = 0.0f;
10251042
CEntity* theEntity = entity->GetGameEntity();
10261043
if (theEntity && theEntity->GetBoneRotation(static_cast<eBone>(boneId), yaw, pitch, roll))
10271044
return std::make_tuple(yaw, pitch, roll);
10281045
return false;
10291046
}
10301047

1031-
bool CLuaPedDefs::SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, CMatrix boneMatrix)
1048+
std::variant<bool, CLuaMultiReturn<float, float, float, float>> CLuaPedDefs::GetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
1049+
{
1050+
if (boneId > BONE_RIGHTFOOT)
1051+
throw LuaFunctionError("Invalid bone ID");
1052+
1053+
float x = 0.0f, y = 0.0f, z = 0.0f, w = 0.0f;
1054+
CEntity* theEntity = entity->GetGameEntity();
1055+
if (theEntity && theEntity->GetBoneRotationQuat(static_cast<eBone>(boneId), x, y, z, w))
1056+
return std::make_tuple(x, y, z, w);
1057+
return false;
1058+
}
1059+
1060+
bool CLuaPedDefs::SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CMatrix boneMatrix)
10321061
{
10331062
CEntity* theEntity = entity->GetGameEntity();
10341063
return theEntity ? theEntity->SetBoneMatrix(static_cast<eBone>(boneId), boneMatrix) : false;
10351064
}
10361065

1037-
std::variant<bool, std::array<std::array<float, 4>, 4>> CLuaPedDefs::GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId)
1066+
std::variant<bool, std::array<std::array<float, 4>, 4>> CLuaPedDefs::GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId)
10381067
{
10391068
CEntity* theEntity = entity->GetGameEntity();
10401069
if (theEntity)

Client/mods/deathmatch/logic/luadefs/CLuaPedDefs.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,17 @@ class CLuaPedDefs : public CLuaDefs
5050
LUA_DECLARE(GetPedContactElement);
5151
LUA_DECLARE(GetPedRotation);
5252
LUA_DECLARE(CanPedBeKnockedOffBike);
53-
static bool SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, CVector position);
54-
static bool SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, float yaw, float pitch, float roll);
55-
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId);
53+
static bool SetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CVector position);
54+
static bool SetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float yaw, float pitch, float roll);
55+
static bool SetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, float x, float y, float z, float w);
56+
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBonePosition(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
5657

57-
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId);
58+
static std::variant<bool, CLuaMultiReturn<float, float, float>> GetElementBoneRotation(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
59+
static std::variant<bool, CLuaMultiReturn<float, float, float, float>> GetElementBoneQuaternion(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
5860

59-
static bool SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId, CMatrix boneMatrix);
61+
static bool SetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId, CMatrix boneMatrix);
6062

61-
static std::variant<bool, std::array<std::array<float, 4>, 4>> GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::int32_t boneId);
63+
static std::variant<bool, std::array<std::array<float, 4>, 4>> GetElementBoneMatrix(lua_State* const luaVM, CClientPed* entity, std::uint32_t boneId);
6264

6365
static bool UpdateElementRpHAnim(lua_State* const luaVM, CClientEntity* entity);
6466
LUA_DECLARE_OOP(GetPedBonePosition);

Client/sdk/game/CEntity.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ class CEntity
112112
virtual bool SetBoneMatrix(eBone boneId, const CMatrix& matrix) = 0;
113113

114114
virtual bool GetBoneRotation(eBone boneId, float& yaw, float& pitch, float& roll) = 0;
115+
virtual bool GetBoneRotationQuat(eBone boneId, float& x, float& y, float& z, float& w) = 0;
115116
virtual bool SetBoneRotation(eBone boneId, float yaw, float pitch, float roll) = 0;
117+
virtual bool SetBoneRotationQuat(eBone boneId, float x, float y, float z, float w) = 0;
116118
virtual bool GetBonePosition(eBone boneId, CVector& position) = 0;
117119
virtual bool SetBonePosition(eBone boneId, const CVector& position) = 0;
118120
};

0 commit comments

Comments
 (0)