Weird collide behavior --how collide("type",x,y) works?


(icemedia) #1

Hi,

I got confused how collide(“type”,x,y) works, as in pic below,

when box move to left, I use collide(“wall”,x,y) and it works fine.[x,y is upper left corner of hitbox].

when box move to right, I thought it should be collide(“wall”, x+width,y) but not, as checked I should use collide(“wall”,x,y) too.

so I got confused, does (x,y) used in collide ( ) is the point used to check ‘hit’ the target? why collide(“wall”,x,y) work when moving right?

thanks.


(Mike Evmm) #2

The x and y are the offsets for the hitbox, so if your hitbox is the box’s width, placing it at x+width would actually make the box collide with the right wall at x+2*width.
They can also be seen as the “virtual position” of the entity for collision. If you wanted to check if the box would hit a wall if it were one pixel to the left, then you’d check for collide(x+1, y). If you want to check for collisions where the box currently is (which is the case); then you’ll want to check collide(x,y).