please help me. My jumps from here feel stiff and when the jump button is released the player falls straight down instead of falling downnin an arc.
The jump code for a quick glance is something like:
Code: Select all
function Player:__init__(...)
...
self.jumpAcceleration = 300
self.runAccel = 500
self.brakeAccel = 1000
self.maxVx = 200
self.maxVy = 200
self.weight = 750
self.gravity = 0
self.initGravity = 400
self._initGravity = self.initGravity
...
end
function Player:updateGravity(dt)
if not self.gravity or self.gravity==0 then
self.gravity = self.initGravity
end
self.gravity = self.gravity+self.weight*dt
local gravity = self.gravity
self.vy = self.vy + gravity*dt
if self.onGround then
self.gravity = self.initGravity
end
end
function Player:jump()
if 1 then
self.stats.jumpsPeformed = self.stats.jumpsPeformed+1
self.sprite:switch("jumping")
media.sfx.player_jumped:play()
self.jumping = true
self.vy = self.vy-self.jumpAcceleration
--create Puff when jumping off wall
if self.wallOnSide then
self:createPuff(5,7)
end
--stops player from flying prematurely
if not self.canFly then
self.canJump = false
end
end
end
funtion Player:update(dt)
...
self:updateGravity(dt)
...
if (self.Ljoystick:getYDir()*-1 > 0 or IM:isDown("Jump")) and self.canJump then
self:jump()
end
--including this though not really used for gravity but who knows...
self:changeVelocityByKeys(dt)
...
self:checkLimits()
local x,y = 0,0
local vx=self.vx
local vx, vy = self.x+vx*dt,self.y+self.vy*dt
if not self.passive then
--collision check
local mx, my = self:move(vx,vy,dt)
self.x, self.y = mx,my
end
end
function Player:checkLimits()
local maxVx,maxVy = self.maxVx, self.maxVy
if true then
if self.vy>0 then
self.oldOnGround = false
end
if math.abs(self.vx)>maxVx then
self.vx = maxVx*self:getDir(self.vx)
end
if self.vy>0 and self.onGround then
self.vy = 0
end
if math.abs(self.vy)>maxVy then
self.vy = maxVy*self:getDir(self.vy)
end
end
end
function Player:getDir(n)
if n > 0 then return 1 elseif n < 0 then return -1
else return 0 end
end
function Player:changeVelocityByKeys(dt, dir, dirY)
self.isJumpingOrFlying = false
if not self.active then return end
local vx, vy = self.vx, self.vy
if dir then
dir, dirY = dir, dirY
else
dir, dirY = self.Ljoystick:getPos()
end
if not self.isBot and self.real then
if (IM:isDown("Left")) then
dir = -1
elseif (IM:isDown("Right")) then
dir = 1
elseif love.keyboard.isDown("right") then error("Something bad happened in controls") end
end
if self.shot then
vx = vx/2
end
if dir<0 then
vx = (vx - 1 * (vx > 0 and self.brakeAccel or self.runAccel)) * (dir*-1)
elseif dir>0 then
vx = (vx + 1 * (vx < 0 and self.brakeAccel or self.runAccel)) * dir
else
local brake = dt * (vx < 0 and self.brakeAccel or -self.brakeAccel)
if math.abs(brake) > math.abs(vx) then--or self.shot then
vx = 0
else
if (150)>math.abs(brake) then
vx = vx + brake
--friction??? Buggy sometimes
if math.floor(math.abs(vx))>0 then
vx = vx - self:getDir(vx)*(frr or 150)*dt
end
end
end
end
if self.vx>self.maxVx then
self.vx = self.maxVx
end
--flying is fine for me
if dirY>0 and self.fly then
self.gravity = self.gravity-500*dir
end
if dirY<0 and self.fly then
self.gravity = self.gravity+500*dirY*-1
end
self.vx, self.vy = vx, vy
end