Collision function

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
baconhawka7x
Party member
Posts: 491
Joined: Mon Nov 21, 2011 7:05 am
Location: Oregon, USA
Contact:

Collision function

Post by baconhawka7x »

I have been working on a tilebased platformer.
I made a basic, filler collision function to use with all entities and items, and it works alright, but it could definitely be better.

Here is the function

Code: Select all

function collide_withTile(ia,va)
  for i,v in ipairs(solid) do
    --Limits the function to tiles closer to the entity.
    if va.x + va.width / 2 > v.x + v.width / 2 - renderDistance * 16 and va.x + va.width / 2 < v.x + v.width / 2 + update_Distance * 16 and
    va.y + va.height / 2 > v.y + v.height / 2 - renderDistance * 16 and va.y + va.height / 2 < v.y + v.height / 2 + update_Distance * 16 then 

 --Collision detection + resolution.
    if va.x + va.width > v.x and
    va.x + (va.width / 2) < v.x + (v.width / 2) and
    va.y + va.height > v.y + extra and
    va.y + extra < v.y + v.height then
      va.x = v.x - va.width
      va.xvel = 0
    end
    if va.y + va.height > v.y and
    va.y + (va.height / 2) < v.y + (v.height / 2) and
    va.x + va.width > v.x + extra and
    va.x + extra < v.x + v.width then
      va.y = v.y - va.height
      va.yvel = 0
      va.grounded = true
    end
    if va.x < v.x + v.width and
    va.x + (va.width / 2) > v.x + (v.width / 2) and
    va.y + va.height > v.y + extra and
    va.y + extra < v.y + v.height then
      va.x = v.x + v.width
      va.xvel = 0
    end
    if va.y < v.y + v.height and
    va.y + (va.height / 2) > v.y + (v.height / 2) and
    va.x + va.width > v.x + extra and
    va.x + extra < v.x + v.width then
      va.yvel = 0
      va.y = v.y + v.height 
    end
    end
  end
end
"Solid" is the table for the tiles, and ia,va are things such as the player, enemies, weapons, helmets and such.
The problem may be the variable 'extra', i'm not sure, but before I put that in there the player would slide off or away from the tile on contact, it is set at 2 right now, and most of the sprites colliding are 16x16:P
I am very sleep deprived, and could really use some help. I'd appreciate it a ton if someone could help me touch up this function.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 58 guests