GetPhysicsObject on client

Is there anyway to use http://wiki.garrysmod.com/page/Entity/GetPhysicsObject on an entity clientside?

Well you could use PhysicsInit on the client object, usually they dont have that by default because the server is managing all that well except clientside ragdolls.

Also if you initialize the physics on the client u’ll be noticing that they arent synchronized with the server physics just like that, you would need to manually set its position every frame.

I used to mess with this stuff a while ago.

[lua]
ClientsidePhysics = ClientsidePhysics or {}
ClientsidePhysics.Active = ClientsidePhysics.Active or {}
ClientsidePhysics.Disabled = ClientsidePhysics.Disabled or {}
ClientsidePhysics.Whitelist = {}
ClientsidePhysics.Whitelist[“prop_physics”] = 1

function ClientsidePhysics:Disable(ent)

if ent.ClientsidePhysicsTracking then

	local phys = ent:GetPhysicsObject()
	if IsValid(phys) then
	
		--print("Disabled physics: ", ent)
		ent:PhysicsDestroy()
		
		self.Disabled[ent] = true
		
	end
	
end

end

function ClientsidePhysics:Enable(ent)

if self.Disabled[ent] ~= nil then

	--print("Enabled Physics: ", ent)
	ent:PhysicsInit(SOLID_VPHYSICS)
	
	self.Disabled[ent] = nil
	
end

end

local ENT = FindMetaTable(“Entity”)

if ENT.PhysicsInitOverride == nil then
ENT.PhysicsInitOverride = ENT.PhysicsInit
end

if ENT.PhysicsDestroyOverride == nil then
ENT.PhysicsDestroyOverride = ENT.PhysicsDestroy
end

function ENT:PhysicsInit(flags)

ENT.PhysicsInitOverride(self, flags)
	
local phys = self:GetPhysicsObject()
if IsValid(phys) then
	
	phys:EnableMotion(false)
	self.ClientsidePhysicsTracking = true
	
	ClientsidePhysics.Active[self] = true
	
end

end

function ENT:PhysicsDestroy()

ENT.PhysicsDestroyOverride(self)

self.ClientsidePhysicsTracking = false

ClientsidePhysics.Active[self] = nil

end

function ENT:PhysicsTrackingActive()

if self.ClientsidePhysicsTracking ~= nil then
	return self.ClientsidePhysicsTracking
end

return false

end

hook.Add(“Think”, “ClientsidePhysics”, function()

for v,_ in pairs(ClientsidePhysics.Active) do
	
	if v.ClientsidePhysicsTracking then
		
		local phys = v:GetPhysicsObject()
		if IsValid(phys) and v:IsDormant() == false then
		
			phys:EnableMotion(false)
			phys:SetPos(v:GetPos())
			phys:SetAngles(v:GetAngles())
			phys:SetVelocity(v:GetVelocity())
			
		end
		
	end
	
end

end)

hook.Add(“EntityRemoved”, “ClientsidePhysicsRemove”, function(ent)

ClientsidePhysics.Active[ent] = nil
ClientsidePhysics.Disabled[ent] = nil

end)

hook.Add(“NetworkEntityCreated”, “ClientsidePhysics”, function(ent)

if ClientsidePhysics.Whitelist[ent:GetClass()] ~= nil then

	local phys = ent:GetPhysicsObject()
	if not IsValid(phys) then
	
		ent:PhysicsInit(SOLID_VPHYSICS)
		
	end
	
end

end)

hook.Add(“PhysgunPickup”, “ClientsidePhysicsPickup”, function(ply, ent)
ClientsidePhysics:Disable(ent)
end)

hook.Add(“PhysgunDrop”, “ClientsidePhysicsDrop”, function(ply, ent)
ClientsidePhysics:Enable(ent)
end)

– Those are not shared at the moment, should we setup some usermessage/net message?
– The gravity gun takes away the clientside physics, so we have to deal with this.
hook.Add(“GravGunOnPickedUp”, “ClientsidePhysicsPickup”, function(ply, ent)
ClientsidePhysics:Disable(ent)
end)

hook.Add(“GravGunOnDropped”, “ClientsidePhysicsDrop”, function(ply, ent)
ClientsidePhysics:Enable(ent)
end)
[/lua]

There is an issue with the Gravity gun, it will kill the physics on the client haven’t worked that out yet so ignore the useless hooks.

EDIT: One might ask what this is all about, long story short: This would allow clientside ragdolls to actually collide with your shitty contraptions.