Skip to content

Commit

Permalink
add init velocity for ragdoll corpse
Browse files Browse the repository at this point in the history
use GoldsrcPhysics v2.0
  • Loading branch information
anchurcn committed Feb 19, 2021
1 parent d2b91e8 commit 2ce8b25
Show file tree
Hide file tree
Showing 4 changed files with 172 additions and 130 deletions.
15 changes: 11 additions & 4 deletions cl_dll/GameStudioModelRenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void CGameStudioModelRenderer::Init(void)
{
CStudioModelRenderer::Init();
InitPhysicsInterface(NULL);
gPhysics.InitSystem(&m_clTime, m_plighttransform, &IEngineStudio);
gPhysics.InitSystem("valve", &IEngineStudio);
}
int CGameStudioModelRenderer::StudioDrawRagdoll(int flags)
{
Expand Down Expand Up @@ -92,7 +92,6 @@ int CGameStudioModelRenderer::StudioDrawRagdoll(int flags)
}
else
{
StudioSetupBones();
gPhysics.SetupBonesPhysically(m_pCurrentEntity->index);
m_pCurrentEntity->origin.x = (*m_pbonetransform)[1][0][3];
m_pCurrentEntity->origin.y = (*m_pbonetransform)[1][1][3];
Expand Down Expand Up @@ -136,6 +135,7 @@ int CGameStudioModelRenderer::StudioDrawRagdoll(int flags)
return 1;
}
cvar_t* _drawOriginalDead = NULL;
bool gIsShoot = false;
int CGameStudioModelRenderer::StudioDrawModel(int flags)
{
// we need a better place to call the physics update function
Expand Down Expand Up @@ -197,7 +197,9 @@ int CGameStudioModelRenderer::StudioDrawModel(int flags)
// init pose
CStudioModelRenderer::StudioDrawModel(0);

pgCorpseMgr->CreateRagdollCorpse(m_pCurrentEntity);
TEMPENTITY* tempent = pgCorpseMgr->CreateRagdollCorpse(m_pCurrentEntity);
// Set init ragdoll pose
gPhysics.SetPose(tempent->entity.index, (float*)m_pbonetransform);
pgCorpseMgr->EntityDie(m_pCurrentEntity);
}
else
Expand Down Expand Up @@ -368,9 +370,14 @@ TEMPENTITY* CorpseManager::CreateRagdollCorpse(cl_entity_t* ent)
tempent->die = 3000;
tempent->fadeSpeed = 1;
tempent->entity.index = _corpseIndex++;
gPhysics.CreateRagdollControllerHeader(tempent->entity.index, IEngineStudio.Mod_Extradata(ent->model));
gPhysics.CreateRagdollControllerModel(tempent->entity.index, (ent->model));
gPhysics.StartRagdoll(tempent->entity.index);
gPhysics.SetVelocity(tempent->entity.index, (Vector3*)&ent->curstate.velocity);

cl_entity_t* local = gEngfuncs.GetLocalPlayer();
Vector v = (ent->origin - local->origin).Normalize();
v = v * 5;
gPhysics.SetVelocity(tempent->entity.index, (Vector3*)&v);

gEngfuncs.Con_DPrintf("corpse [%d]'s velocity is %f\n", tempent->entity.index, ent->curstate.velocity.Length());
gEngfuncs.Con_DPrintf("create corpse [%d] for entity [%d]\n", tempent->entity.index, ent->index);
Expand Down
64 changes: 35 additions & 29 deletions cl_dll/physics.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

#include"physics.h"
#include<physics.h>
#include <metahost.h>
#include<stdio.h>
#include<Windows.h>
Expand All @@ -9,11 +9,10 @@

PhsicsAPI gPhysics;


#ifdef _DEBUG
const wchar_t PhyDllPath[] = L".\\gsphysics\\bin\\GoldsrcPhysics.dll";
#else// _DEBUG
const wchar_t PhyDllPath[] = L"\\gsphysics\\GoldsrcPhysics.dll";
const wchar_t PhyDllPath[] = L".\\gsphysics\\bin\\GoldsrcPhysics.dll";
#endif

//globle CLR handle
Expand Down Expand Up @@ -66,16 +65,17 @@ int ExitCLR()
return 0;
}

void* GetFunctionPointer(const LPCWSTR name)
void* GetFunctionPointer(LPWSTR name)
{
void* pfn = NULL;

wchar_t buffer[64];//marshal args to [0xXXXX|MethodName] format
swprintf(buffer, 64, L"%p|%s", &pfn, name);
const int bufsize = 128;
wchar_t buffer[bufsize];//marshal args to [0xXXXX|MethodName] format
swprintf(buffer, bufsize, L"%p|%s", &pfn, name);

DWORD dwRet = 0;
HRESULT hr = pRuntimeHost->ExecuteInDefaultAppDomain(PhyDllPath,
L"GoldsrcPhysics.ExportAPIs.PhysicsMain",
L"UsrSoft.ManagedExport.ManagedExporter",
L"GetFunctionPointer",
buffer,
&dwRet);
Expand All @@ -92,26 +92,32 @@ void* GetFunctionPointer(const LPCWSTR name)
extern "C" void InitPhysicsInterface(char* msg)
{
InitCLR();
gPhysics.Test = (void(_stdcall*)())GetFunctionPointer(L"Test");
gPhysics.InitSystem = (void(_stdcall*)(void* pStudioRenderer, void* lastFieldAddress, void* engineStudioAPI))GetFunctionPointer(L"InitSystem");
gPhysics.ChangeLevel = (void(_stdcall*)(const char* mapName))GetFunctionPointer(L"ChangeLevel");
gPhysics.LevelReset = (void(_stdcall*)())GetFunctionPointer(L"LevelReset");
gPhysics.Update = (void(_stdcall*)(float delta))GetFunctionPointer(L"Update");
gPhysics.Pause = (void(_stdcall*)())GetFunctionPointer(L"Pause");
gPhysics.Resume = (void(_stdcall*)())GetFunctionPointer(L"Resume");
gPhysics.ShotDown = (void(_stdcall*)())GetFunctionPointer(L"ShotDown");
gPhysics.ShowConfigForm = (void(_stdcall*)())GetFunctionPointer(L"ShowConfigForm");
gPhysics.CreateRagdollController = (void(_stdcall*)(int entityId, const char* modelName))GetFunctionPointer(L"CreateRagdollController");
gPhysics.CreateRagdollControllerIndex = (void(_stdcall*)(int entityId, int index))GetFunctionPointer(L"CreateRagdollControllerIndex");
gPhysics.CreateRagdollControllerHeader = (void(_stdcall*)(int entityId, void * hdr))GetFunctionPointer(L"CreateRagdollControllerHeader");
gPhysics.StartRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"StartRagdoll");
gPhysics.StopRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"StopRagdoll");
gPhysics.SetupBonesPhysically = (void(_stdcall*)(int entityId))GetFunctionPointer(L"SetupBonesPhysically");
gPhysics.ChangeOwner = (void(_stdcall*)(int oldEntity, int newEntity))GetFunctionPointer(L"ChangeOwner");
gPhysics.SetVelocity = (void(_stdcall*)(int entityId, Vector3 * v))GetFunctionPointer(L"SetVelocity");
gPhysics.DisposeRagdollController = (void(_stdcall*)(int entityId))GetFunctionPointer(L"DisposeRagdollController");
gPhysics.Explosion = (void(_stdcall*)(Vector3 * pos, float intensity))GetFunctionPointer(L"Explosion");
gPhysics.Shoot = (void(_stdcall*)(Vector3 * from, Vector3 * force))GetFunctionPointer(L"Shoot");
gPhysics.PickBody = (void(_stdcall*)())GetFunctionPointer(L"PickBody");
gPhysics.ReleaseBody = (void(_stdcall*)())GetFunctionPointer(L"ReleaseBody");
gPhysics.Set = (void(_stdcall*)(const char* key, const char* value))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Set");
gPhysics.Test = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Test");
gPhysics.InitSystem = (void(_stdcall*)(const char* modFolder, void* pEngineStudioAPI))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.InitSystem");
gPhysics.ChangeLevel = (void(_stdcall*)(const char* mapName))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ChangeLevel");
gPhysics.LevelReset = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.LevelReset");
gPhysics.Update = (void(_stdcall*)(float delta))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Update");
gPhysics.Pause = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Pause");
gPhysics.Resume = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Resume");
gPhysics.ShotDown = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ShotDown");
gPhysics.ShowConfigForm = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ShowConfigForm");
gPhysics.CreateRagdollController = (void(_stdcall*)(int entityId, char* modelName))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollController");
gPhysics.CreateRagdollControllerIndex = (void(_stdcall*)(int entityId, int index))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollControllerIndex");
gPhysics.CreateRagdollControllerModel = (void(_stdcall*)(int entityId, void * model))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.CreateRagdollControllerModel");
gPhysics.StartRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.StartRagdoll");
gPhysics.StopRagdoll = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.StopRagdoll");
gPhysics.SetupBonesPhysically = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetupBonesPhysically");
gPhysics.ChangeOwner = (void(_stdcall*)(int oldEntity, int newEntity))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ChangeOwner");
gPhysics.SetVelocity = (void(_stdcall*)(int entityId, Vector3 * v))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetVelocity");
gPhysics.DisposeRagdollController = (void(_stdcall*)(int entityId))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.DisposeRagdollController");
gPhysics.ImpulseBone = (void(_stdcall*)(int entityId, int boneId, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ImpulseBone");
gPhysics.ClearRagdoll = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ClearRagdoll");
gPhysics.HeadShootRagdoll = (void(_stdcall*)(int entityId, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.HeadShootRagdoll");
gPhysics.Explosion = (void(_stdcall*)(Vector3 * pos, float intensity))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Explosion");
gPhysics.Shoot = (void(_stdcall*)(Vector3 * from, Vector3 * force))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.Shoot");
gPhysics.PickBodyLocal = (void(_stdcall*)(Vector3 from, Vector3 to))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.PickBodyLocal");
gPhysics.ReleaseBodyLocal = (void(_stdcall*)())GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.ReleaseBodyLocal");
gPhysics.MoveBodyLocal = (void(_stdcall*)(Vector3 from, Vector3 to))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.MoveBodyLocal");
gPhysics.SetPose = (void(_stdcall*)(int entityId, float* pBoneWorldTransform))GetFunctionPointer(L"GoldsrcPhysics.ExportAPIs.PhysicsMain.SetPose");
}
Loading

0 comments on commit 2ce8b25

Please sign in to comment.