Jump to content

[4.6 Beta] Slow rotation with Kinematic joints.


reepblue
 Share

Recommended Posts

I've noticed that with the pickup system in the template FPS player is now very sloppy. With my code, it's much better with my system, but I've noticed a slow delay between updating the rotation of a clamped object between 4.5 and the 4.6 beta.

Here is my Pickup Controller code if need be. It worked perfectly in 4.5.The SDK system is all rubberbandy and wack.

if PickupController~=nil then return end
PickupController={}

function PickupController:Create(entity)
	if entity== nil then Debug:Error("PickupController: Entity cannot be nil!") end
	local pickupcontroller = {}
	pickupcontroller.entity = entity
	pickupcontroller.allowpickup = true
	pickupcontroller.maxdist = 2.5
	pickupcontroller.carrydist = 1.75
	pickupcontroller.carriedobj=nil
	
	pickupcontroller.effector=nil
	pickupcontroller.carrypos=Vec3(0)
	pickupcontroller.carryquat=Quat(0)
	
	for k,v in pairs(PickupController) do
		pickupcontroller[k] = v
	end
	
	return pickupcontroller
end

function PickupController:Reset()
	if self.effector ~= nil then 
		self.effector:Release() 
		self.effector = nil
	end

	if self.carriedobj ~= nil then
		local n =  self.carriedobj:GetKeyValue("collisiontype")
		n = n tonumber(f)
		if n ~= nil then self.carriedobj:SetCollisionType(n) end
		self.carriedobj:SetKeyValue("collisiontype", "")
		self.carriedobj = nil
	end
end

function PickupController:Delete()
	self:Reset()
	--self.entity = nil
	self.effector=nil
	self.carrypos=nil
	self.carryquat=nil
	self.maxdist=nil
	self = nil
end

function PickupController:CanPickupObject(entity, masslimit)
	if not self.allowpickup then return false end
	if entity == nil then return false end
	if entity:GetMass() <= 0 then return false end
	if entity:GetShape() == nil then return false end
	if entity:GetAABB(Entity.LocalAABB).size.x > 1.28 or 
	entity:GetAABB(Entity.LocalAABB).size.z > 1.28 or 
	entity:GetAABB(Entity.LocalAABB).size.z > 1.28 then
	return false end
	if entity:GetAABB(Entity.GlobalAABB).size.x > self.carrydist or 
	entity:GetAABB(Entity.GlobalAABB).size.z > self.carrydist  then return false end
	if masslimit > 0 and entity:GetMass() > masslimit then return false end
	if entity:GetCollisionType() == Collision.Character or 
	entity:GetCollisionType() == Collision.Scene then return false end
	return self:NotOnTopOfObject(entity)
end

-- Check to see if we're not on top of an object
function PickupController:NotOnTopOfObject(object)
	if object ~= nil then 
		local pickInfo = PickInfo()
		local p0 = self.entity:GetPosition()
		local p1 = Transform:Point(0,-1.0,0,self.entity,nil)
		if self.entity.world:Pick(p0, p1, pickInfo, 0.8, true, Collision.Debris ) then 
			if pickInfo.entity == object then return false end
		end	
	end
	return true	
end

function PickupController:IsHoldingObject()
	if self.carriedobj == nil then return false end
	return true
end

function PickupController:PickupObject(entity, transformfrom)
	-- If we are already holding an object, don't call this function
	if self:IsHoldingObject() then
		return
	end
		
	self:Reset()

	self.carriedobj = entity

	-- Store it's original collision mode within the entity itself.
	self.carriedobj:SetKeyValue("collisiontype", tostring(self.carriedobj:GetCollisionType()))

	-- Change the object's collision type to "Debris" This has it's own issues, but it's less cost effective than raytraces.
	self.carriedobj:SetCollisionType(Collision.Debris)

	-- Play a sound on pickup..
	local entscript = GetEntityScript(self.carriedobj)
	if entscript~=nil then
		if type(entscript.PlayImpactSound)=="function" then 
			entscript:PlayImpactSound()
		end
	end

	self.carrypos = Transform:Point(0, 0, self.carrydist, transformfrom, nil)
	self.effector = Joint:Kinematic(self.carrypos.x, self.carrypos.y, self.carrypos.z, self.carriedobj)
	self.effector:SetFriction(1000, 1000)
	self.effector:SetTargetAngle(1)
	self.carriedobj:SetDamping(1, 1)
	self.carryquat = Transform:Rotation(self.carriedobj:GetQuaternion(true), nil, transformfrom)
end

function PickupController:UpdateHeldObject(transformfrom)
	if not self:IsHoldingObject() then
		return
	end
	
	-- Update the pickup position
	self.carrypos = Transform:Point(0, 0, self.carrydist, transformfrom, nil)

	local current_obj_pos = self.carriedobj:GetPosition(true)
	
	local diff = self.carrypos:DistanceToPoint(current_obj_pos)
	if diff > self.maxdist then
		self:DropObject()
		return
	end

	self.effector:SetTargetPosition(self.carrypos, 1)
	self.carriedobj:SetVelocity(self.carriedobj:GetVelocity() / 1.5)
	self.effector:SetTargetRotation(Transform:Rotation(self.carryquat, transformfrom, nil), 1)
end


function PickupController:DropObject()
	if not self:IsHoldingObject() then
		return
	end

	-- After our AABB test, if we can drop the object, do so
	local aabb = self.carriedobj:GetAABB(Entity.GlobalAABB)
	local player = self.entity:GetAABB(Entity.GlobalAABB)
	
	if not aabb:IntersectsAABB(player,0) then
		self.carriedobj:SetVelocity(self.carriedobj:GetVelocity() / 4)
		self.carriedobj:SetDamping(0.1, 0.1)
		self:Reset()
	end
end

function PickupController:Enable(b) 
	self.allowpickup = b
end

 

Cyclone - Ultra Game System - Component PreprocessorTex2TGA - Darkness Awaits Template (Leadwerks)

If you like my work, consider supporting me on Patreon!

Link to comment
Share on other sites

  • 1 month later...
  • Josh locked this topic
Guest
This topic is now closed to further replies.
 Share

×
×
  • Create New...