weird collision

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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

weird collision

Post 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 :)
Attachments
Archive.love
(2.27 KiB) Downloaded 119 times
Highjhacker
Prole
Posts: 6
Joined: Mon May 19, 2014 5:21 pm

Re: weird collision

Post 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)
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: weird collision

Post 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...
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: weird collision

Post 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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: weird collision

Post 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!
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: weird collision

Post 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
pielago
Party member
Posts: 142
Joined: Fri Jun 14, 2013 10:41 am

Re: weird collision

Post by pielago »

thank you so much now I am going to study it :) let see if I can make 2 rectangles do collision ...
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: weird collision

Post 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.
Post Reply

Who is online

Users browsing this forum: darkfrei, Google [Bot] and 3 guests