[FIXED] decimal discrepancy on x and y

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
Path
Prole
Posts: 10
Joined: Tue Nov 17, 2015 8:06 pm

[FIXED] decimal discrepancy on x and y

Post by Path »

So i have a tilemap and a player all with collision boxes set up, and i tried doing collision tests between the blocks and the player. i'm using the following collision function to detect it:

Code: Select all

function CheckCollision(rect1, rect2)
   local x1,y1,w1,h1 = rect1.x,rect1.y,rect1.width,rect1.height
   local x2,y2,w2,h2 = rect2.x,rect2.y,rect2.width,rect2.height
   return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end
every collision box's (save player) x, y are divisible by 8, and the widths and heights for all collision, player and tiles are 8.
for some reason, when i decrease the y value of the player by increments of 0.1, some tiles will register collision exactly divisible by 8 (ei 288, 296, 304) etc but some tiles, specifically on the lower half of the map, will register the collision a decimal early (287.99999998) etc. I've ran checks on every block's x and y value and had them print if they were divisible by 8, and they are, which shows that it has nothing to do with the block's collision.

I don't understand where this inconsistency comes from, and i've checked every single value to make sure the position is correct. I think the issue might be a love2d-related error, or maybe the collision function is off. Anyways, here are some pictures of results that i got when changing the y by increments of 0.001

Image

here is the y value i got for a block on the higher part of the map

Image

y value for lower.
Last edited by Path on Tue Jun 13, 2017 9:02 am, edited 1 time in total.
Path
Prole
Posts: 10
Joined: Tue Nov 17, 2015 8:06 pm

Re: decimal discrepancy on x and y

Post by Path »

Okay so i found out the issue. The collision detection function, https://love2d.org/wiki/BoundingBox.lua , actually uses a 'less than' sign when it should be using a 'less than or equal to'. this was causing the discrepancy. sorry for posting so soon without testing that part properly!
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 5 guests