Page 1 of 1

weird collision

Posted: Sun Jul 20, 2014 2:26 am
by pielago
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 :)

Re: weird collision

Posted: Sun Jul 20, 2014 2:08 pm
by Highjhacker
Hey, i didn't look at your code (I have no time right now), but I think this should be solving your problems ;
https://developer.mozilla.org/en-US/doc ... _detection

(Sorry for my bad English)

Re: weird collision

Posted: Sun Jul 20, 2014 6:54 pm
by pielago
thanks I do understand the collision its just that my collision its not working idk why???
hope u can look at it and let me know what I did wrong...

Re: weird collision

Posted: Sun Jul 20, 2014 9:18 pm
by MadByte
Hi,

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.
RectangleCollision.love

Re: weird collision

Posted: Sun Jul 20, 2014 11:28 pm
by pielago
Thank you so much

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!

Re: weird collision

Posted: Thu Jul 24, 2014 10:09 pm
by MadByte
No it's not Mac related. Sorry for the late anwser but I had to figure it out myself :D

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.

Like this:

Code: Select all

 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
same for the y axis.

(Fixed version)
RectangleCollision2.love

Re: weird collision

Posted: Sat Jul 26, 2014 8:31 am
by pielago
thank you so much now I am going to study it :) let see if I can make 2 rectangles do collision ...

Re: weird collision

Posted: Sat Jul 26, 2014 9:10 am
by MadByte
hmm, I need to correct me again, sorry :)

It's enough to ask for the direction and then set the position to the other rects side. Means instead of :

Code: Select all

 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
do:

Code: Select all

if self.direction == "left" then self.x = rect.x + rect.w end
if self.direction == "right" then self.x = rect.x - self.w end
the "rect.x + rect.w < self.x" check already is part of the collision check I did above so it isn't neccessary at all.