1

So I made a game, made a map, and everything is working fine. The problem is I made a very dumb collision system that worked first, but I am running into problems.

I am using player's X and Y positions to draw character, and using players tileX and tileY (x/32 and y/32) to detect collision. Heres a picture which explains my problem: the problem

The Red box is players tileX and tileY cordinate. Player still moves beyound the wall where the collision should happen.

The TileX doesnt let increases/decreases happen if they collide with a solid tile, BUT player's X and Y (sprite) still moves beyond that box for 31 more pixels. I have no idea how to fix this. My player image is not centered, its drawn on the top-right corner.

This is the current code im using:

    for i=1, #lsx_map1 do 
        if math.floor(player.fx/32) == lsx_map1[i] and math.floor(player.fy/32) == lsy_map1[i] then
            player.speedx = 0
            player.speedy = 0   
            print("COLISSION DETECTED ON "..player.x.." "..player.y)
        else
            print(colVar)
            colVar = colVar+1
        end
    end
    if colVar == #lsx_map1 then     
        player.x = player.fx
        player.y = player.fy
    end

lsx_map1 is the number of solid tiles, and colVar should equal to that number if collision doesnt happen. In case collision happens, that number doesnt increase by one, and then nothing happens. Ask for any more details you need if you want to help me but you need more information.

Any help or tips would be appreciated. Thank you.

Egor Skriptunoff
  • 23,359
  • 2
  • 34
  • 64
  • You are not creating a collision box with tileX and tileY, this defines a single point where you are checking for collision. give this answer a read: https://stackoverflow.com/questions/18295825/determine-if-point-is-within-bounding-box. the language is not lua but the concept should help you. – Nifim Dec 02 '19 at 21:05

0 Answers0