-2

I recently added an entity framework to my code (sorta half done) and after I added it I noticed something wrong with the y velocity of the player it seems to fluctuate between 1 and 0. I am completely stumped as to why this has occurred I assume it's something to do with the current physics I've put in but not sure what happening. code below:

Collisions.Lua

-- btw Not my Code just needed something to get AABB to work right
 function isColliding(a,b)
      if ((b.X >= a.X + a.Width) or
         (b.X + b.Width <= a.X) or
         (b.Y >= a.Y + a.Height) or
         (b.Y + b.Height <= a.Y)) then
           return false 
      else return true
           end
   end

-- 'A' Must Be an entity that Can Move 
-- 'B' Can Be a Static Object
-- Both need a Height, width, X and Y value
function IfColliding(a, b)

    if isColliding(a,b) then

        if a.Y + a.Height < b.Y then 
            a.Y = b.Y - a.Height
            a.YVel = 0
        elseif a.Y - a.Height > b.Y - b.Height then
            a.Y = b.Y + b.Height
            a.YVel = 0
        end

    end 

end

entitybase.lua

local entitybase = {}

lg = love.graphics

    -- Set Health
    entitybase.Health = 100

    -- Set Acceleration
    entitybase.Accel = 3000

    -- Set Y velocity
    entitybase.Yvel = 0.0

    -- Set X velocity
    entitybase.Xvel = 0.0

    -- Set Y Position
    entitybase.Y = 0

    -- Set X Position
    entitybase.X = 0

    -- Set Height
    entitybase.Height = 64

    -- Set Width 
    entitybase.Width = 64

    -- Set Friction
    entitybase.Friction = 0

    -- Set Jump
    entitybase.Jump = 350

    -- Set Mass
    entitybase.Mass = 50 

    -- Set Detection Radius
    entitybase.Radius = 100

    -- Sets boolean value for if the player can jump
    entitybase.canJump = true

    -- Sets Image default
    entitybase.img = nil

------------------------------------------------
-- Some Physics To ensure that movement works --
------------------------------------------------

function entitybase.physics(dt)


    -- Sets the X position of the entities
    entitybase.X = entitybase.X + entitybase.Xvel * dt

    -- Sets the Y position of the entities
    entitybase.Y = entitybase.Y + entitybase.Yvel * dt

    -- Sets the Y Velocity  of the entities
    entitybase.Yvel = entitybase.Yvel + (gravity  * entitybase.Mass) * dt

    -- Sets the X Velocity of the entities
    entitybase.Xvel = entitybase.Xvel * (1 - math.min(dt * entitybase.Friction, 1))

end

-- Entity Jump feature
function entitybase:DoJump()

    -- if the entities y velocity = 0 then subtract the Yvel from Jump Value
    if entitybase.Yvel == 0 then
        entitybase.Yvel = entitybase.Yvel - entitybase.Jump
    end

end

-- Updates the Jump
function entitybase:JumpUpdate(dt)

    if entitybase.Yvel ~= 0 then
        entitybase.Y = entitybase.Y + entitybase.Yvel * dt 
        entitybase.Yvel = entitybase.Yvel - gravity * dt
    end

end

function entitybase:update(dt)

    entitybase:JumpUpdate(dt)
    entitybase.physics(dt)
    entitybase.move(dt)

end

return entitybase

Sorry For chucking in all this I didn't know what I needed to show as I don't completely know whats creating the problem, I have had another look and it looks to be something to do with collisions maybe, not sure. anyway thank you to those who want to have a crack at my problem if anything needs explaining or understanding I'll try my best to clear it up.

Codedin97
  • 3
  • 1
  • 3
  • 1
    That's too much code. Narrow your problem down to [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Yu Hao May 14 '15 at 08:36
  • ok will do thanks for editing it though had no idea what i was doing – Codedin97 May 14 '15 at 09:31

1 Answers1

0

Fix your timestep and don't use dt for physics calcualtions.

easy82
  • 1
  • 1