Jump to content

fps player optimization


Slastraf
 Share

Recommended Posts

if you put the player movement into the updateworld instead of the updatephysics function, you experience a better performance, exspecially if you walk over a bluff landscape

then it will look something like this

--Player Movement
local movex=0
local movez=0
self.input[0]=0
self.input[1]=0
if window:KeyDown(Key.W) then self.input[1]=self.input[1]+1 end
if window:KeyDown(Key.S) then self.input[1]=self.input[1]-1 end
if window:KeyDown(Key.D) then self.input[0]=self.input[0]+1 end
if window:KeyDown(Key.A) then self.input[0]=self.input[0]-1 end
local playerMovement = Vec3(0)
playerMovement.x = self.input[0] * self.moveSpeed
playerMovement.z = self.input[1] * self.moveSpeed

--This prevents "speed hack" strafing due to lazy programming
if self.input[0]~=0 and self.input[1]~=0 then
playerMovement = playerMovement * 0.70710678
end

--if self.entity:GetAirborne() then
-- playerMovement = playerMovement * 0.2
--end

--Check for running with shift and when not carrying anything
if self.carryingEntity == nil and window:KeyDown(Key.Shift) then
playerMovement.z = playerMovement.z * self.speedMultiplier
end

-- Check for jumping
local jump = 0
if window:KeyHit(Key.Space) and self:IsAirborne() == 0 then
jump = self.jumpForce

self.sound.footsteps.concrete.jump:Play()

if self.weapons[self.currentweaponindex]~=nil then
self.weapons[self.currentweaponindex]:BeginJump()
end

--Give the player an extra boost when jumping
playerMovement = playerMovement * 1.6
end

-- Check for crouching
--if App.window:KeyHit(Key.ControlKey) then
-- crouched = not crouched
--end

--With smoothing
--Position camera at correct height and playerPosition
self.entity:SetInput(self.camRotation.y, playerMovement.z, playerMovement.x, jump , false, 1.0, 0.5, true)
local playerPos = self.entity:GetPosition()
local newCameraPos = self.camera:GetPosition()
--local playerTempHeight = ((self:IsCrouched() == 1) and crouchHeight or playerHeight)
newCameraPos = Vec3(playerPos.x, newCameraPos.y ,playerPos.z)

if newCameraPos.y newCameraPos.y = Math:Curve(playerPos.y + self.eyeheight, newCameraPos.y, self.camSmoothing)
else
newCameraPos.y = playerPos.y + self.eyeheight
end

self.camera:SetPosition(newCameraPos)


if self.isairborne==true then
if self.entity:GetAirborne()==false then
if self.weapons[self.currentweaponindex]~=nil then
self.weapons[self.currentweaponindex]:BeginLand()
end
end
end
self.isairborne = self.entity:GetAirborne()

--Mouse look
self.currentMousePos = window:GetMousePosition()
window:SetMousePosition(Math:Round(context:GetWidth()/2), Math:Round(context:GetHeight()/2))
self.currentMousePos.x = Math:Round(self.currentMousePos.x)
self.currentMousePos.y = Math:Round(self.currentMousePos.y)

if self.mousezpos==nil then self.mousezpos=0 end
if self.currentMousePos.z~=self.mousezpos then
if self.weapons[self.currentweaponindex] then
if self.weapons[self.currentweaponindex].currentaction==nil then
for n=1,math.abs(self.currentMousePos.z-self.mousezpos) do
if self.currentMousePos.z>self.mousezpos then
self:CycleWeapon(-1)
else
self:CycleWeapon(1)
end
end
self.mousezpos=self.currentMousePos.z
end
else
self.mousezpos=self.currentMousePos.z
end
end

self.mouseDifference.x = Math:Curve(self.currentMousePos.x - Math:Round(context:GetWidth()/2),self.mouseDifference.x,3)
self.mouseDifference.y = Math:Curve(self.currentMousePos.y - Math:Round(context:GetHeight()/2),self.mouseDifference.y,3)
self.camRotation.x = Math:Clamp(self.camRotation.x + self.mouseDifference.y / self.mouseSensitivity,-50,50)
self.camRotation.y = self.camRotation.y + (self.mouseDifference.x / self.mouseSensitivity)
headrotationx = self.camRotation.x

--Adjust the view shake
self.hurtoffset.x = Math:Inc(0,self.hurtoffset.x,2*Time:GetSpeed())
self.hurtoffset.y = Math:Inc(0,self.hurtoffset.y,2*Time:GetSpeed())
self.smoothedhurtoffset.x = Math:Curve(self.hurtoffset.x,self.smoothedhurtoffset.x,3)
self.smoothedhurtoffset.y = Math:Curve(self.hurtoffset.y,self.smoothedhurtoffset.y,3)

--Set the camera angle
self.camera:SetRotation(self.camRotation+self.smoothedhurtoffset)

--Picking and usage
local pickInfo = PickInfo()

--Raycast Pick that is being send from the camera in to the world
self.canUse = false

local fire = false
local currentime = Time:GetCurrent()
if self.carryingEntity==nil then
if self.weapons[self.currentweaponindex]~=nil then
if self.weapons[self.currentweaponindex].automatic then
if window:MouseDown(1) then
fire=true
else
self.suspendfire=false
end
else
if window:MouseHit(1) then
fire=true
end
end
end
end

--Fire weapon
if self.carryingEntity==nil then
if fire then
if self.suspendfire~=true then
if self.weapons[self.currentweaponindex].clipammo==0 and self.weapons[self.currentweaponindex].automatic==true then
self.suspendfire=true
end
self.weapons[self.currentweaponindex]:Fire()
end
end
end

--Throw object if holding one
if self.carryingEntity then
if window:MouseHit(1) then
local dir = Transform:Vector(0,0,self.throwforce,self.camera,nil)
self.carryingEntity:AddForce(dir)
self:DropEntityCarrying()
end
end

if window:KeyHit(Key.R) then
if self.weapons[self.currentweaponindex]~=nil then
if type(self.weapons[self.currentweaponindex].CanReload)=="function" then
if self.weapons[self.currentweaponindex]:CanReload() then
self.suspendfire=false
self.weapons[self.currentweaponindex]:Reload()
end
end
end
end

if window:KeyHit(Key.E) then
if self.carryingEntity then
self:DropEntityCarrying()
else
local p0 = self.camera:GetPosition(true)
local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil)
if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then

--Looks for any entity in the hierarchy that has a "Use" function
local usableentity = self:FindUsableEntity(pickInfo.entity)

if usableentity~=nil then

--Use the object, whatever it may be
usableentity.script:Use(self.entity)

else
if self.carryingEntity == nil then
mass = pickInfo.entity:GetMass()

--Pick up object if it isn't too heavy
if mass>0 and mass<=self.maxcarryweight then
self.carryingEntity = pickInfo.entity
self.carryingobjectcollisiontype = self.carryingEntity:GetCollisionType()
self.carryingEntity:SetCollisionType(Collision.Debris)
self.carryrotation = Transform:Rotation(pickInfo.entity:GetQuaternion(true),nil,self.camera)
self.carryposition = Transform:Point(pickInfo.entity:GetPosition(true),nil,self.camera)
end
end
end
end
end
end

--The icon that shows that an object can be picked up or can be interacted with
--Amnesia fan, I see. 
if self.carryingEntity == nil then
local p0 = self.camera:GetPosition(true)
local p1 = Transform:Point(0,0,self.useDistance,self.camera,nil)
if self.entity.world:Pick(p0,p1, pickInfo, 0, true) then
if self:FindUsableEntity(pickInfo.entity)~=nil then
self.canUse=true
else
local mass = pickInfo.entity:GetMass()
if mass>0 and mass<=self.maxcarryweight then
self.canUse = true
end
end
end
end

Link to comment
Share on other sites

This is a problem i find long time ago, it's a core problem with LE3 and physics sleep.png

I made a TPS and the character had very jurky physics using UpdatePhysics, while it was running smoothly on the level putting all movement code in UpdateWorld. Also you don't need anymore camera smoothing by code as UpdateWorld don't make physics jittering.

Stop toying and make games

Link to comment
Share on other sites

I notice you are not using a time variable. This could be the answer. Using UpdateWorld is ok but you need to be careful because this is more costly. UpdateWorld runs constantly in a loop as fast as it can. The number of times a second will very heavily based on size of UpdateWorld, Computer Specs, and how many other programs are running. Update Physics only runs a set amount of times a second regardless of speed. By adding to UpdateWorld you could be eating resources that could be used for other tasks. Another problem is that the game could run differently on different PCs. A better machine would allow the player to move faster because the faster computer can loop UpdateWorld faster and receive more movement updates. This is fixed by using a time variable to the movement of the player. All computers will move the player x distance a second regardless of how many UpdateWorld passes there are.

  • Upvote 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...