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
weird collision
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
weird collision
- Attachments
-
- Archive.love
- (2.27 KiB) Downloaded 118 times
-
- Prole
- Posts: 6
- Joined: Mon May 19, 2014 5:21 pm
Re: weird collision
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)
https://developer.mozilla.org/en-US/doc ... _detection
(Sorry for my bad English)
Re: weird collision
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...
hope u can look at it and let me know what I did wrong...
Re: weird collision
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.
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.
Re: weird collision
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!
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
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.
Like this:
same for the y axis.
(Fixed version)
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
(Fixed version)
Re: weird collision
thank you so much now I am going to study it let see if I can make 2 rectangles do collision ...
Re: weird collision
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 :
do:
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.
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
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
Who is online
Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 7 guests