for some weird reason my collision is not good I am not sure what I did wrong..
can someone help me fix it??? it wont act normal til i try many times its like 1 pixel to soon for collision...
or is there a better way to do what i am trying to do?
If so can you share it
basically you just have to calculate the estimated position after your movement ( but don't change the actual position ) and check if the rectangle would collide with any other rectangle on screen. If there is a collision, skip the movement and leave the rectangle as it is. If there is no collision, set the value to the estimated position.
The space between the rects could be the result of odd numbers (318,32904234679 instead of 318). But I'm not completly sure about that. Maybe there is a problem in the code ( my code also isn't perfect about that ).
Anyway here is an example with comments on how I would try to do what you want.
If you' have any questions, just ask.
Okay I saw your code and it weird it behave the same way????
this is odd... do you think its because I am using mac computer?
or is it my conf.lua????
but it behaves the same way as my code!
No it's not Mac related. Sorry for the late anwser but I had to figure it out myself
You can fix that by checking which direction your rectangle move ( as you already do ) and instead of just stop the movement when an collision has been detected you have to set the position of the moving rect to the exact same position as the colliding side of the other rect.
if self.direction == "left" and rect.x + rect.w < self.x then self.x = rect.x + rect.w end
if self.direction == "right" and rect.x > self.x + self.w then self.x = rect.x - self.w end
if self.direction == "left" and rect.x + rect.w < self.x then self.x = rect.x + rect.w end
if self.direction == "right" and rect.x > self.x + self.w then self.x = rect.x - self.w end