+ 2

Line rectangle collision?

How do I do line rectangle collision? It seems way more complex than usual, here's the code. I'm trying to make r1 collide with the blue lines on r2. The collision method is vertextIntersect() in the Rect object. I am not asking for rect collision, I am asking for line rect collision. The code draws blue lines over the second rectangle, I want to collide with 1 line. https://code.sololearn.com/W7N67EeZEjkC/?ref=app

2nd Aug 2020, 8:33 AM
Clueless Coder
Clueless Coder - avatar
29 Respuestas
+ 3
https://code.sololearn.com/WGBLLnFkuMpR/?ref=app
2nd Aug 2020, 2:43 PM
Gordon
Gordon - avatar
+ 6
P5js has an additional collision module with examples. https://github.com/bmoren/p5.collide2D
2nd Aug 2020, 8:54 AM
Oma Falk
Oma Falk - avatar
+ 5
ok... a m*n rectangle has m*n points. a k*1 line has k points. if any point of k is in rectangles point... collision.
2nd Aug 2020, 9:11 AM
Oma Falk
Oma Falk - avatar
+ 5
now optimize atm your lines horizontal and vertical lines?
2nd Aug 2020, 9:13 AM
Oma Falk
Oma Falk - avatar
+ 5
width*height and length of objects
2nd Aug 2020, 9:15 AM
Oma Falk
Oma Falk - avatar
+ 5
Rectangle Collision Testing https://code.sololearn.com/WOSkh92TwAYI/?ref=app Circle
2nd Aug 2020, 11:47 AM
Gordon
Gordon - avatar
+ 5
https://bit.ly/2XkTBnt Clueless Coder again the little sketch.
2nd Aug 2020, 1:02 PM
Oma Falk
Oma Falk - avatar
+ 5
👍thanks a million
2nd Aug 2020, 3:36 PM
Chuu Wuttyi Linn
Chuu Wuttyi Linn - avatar
+ 3
Clueless Coder Quite close Center of moving square, correct. For blue line, make it not a vertical line, make it with a random angle. Find the nearest point on the line which has shortest distance from center of square. Calculate the distance between that point and the center of square.
2nd Aug 2020, 1:56 PM
Gordon
Gordon - avatar
+ 2
Oma Falk I know, but I want to learn the theory
2nd Aug 2020, 8:56 AM
Clueless Coder
Clueless Coder - avatar
2nd Aug 2020, 9:00 AM
minirkk
minirkk - avatar
+ 2
Oma Falk Just remembered, collide2d won't work with my rectangle configuration. It only works with rectMode(CORNER)
2nd Aug 2020, 9:01 AM
Clueless Coder
Clueless Coder - avatar
+ 2
Calculate the normal length from line to center of shape , and then compare with the length of shape at that direction.
2nd Aug 2020, 1:33 PM
Gordon
Gordon - avatar
+ 2
Gordon Thanks, that very nearly worked. It's bit off though, I may have misunderstood. This is way to advanced for me I think. I added it to the code
2nd Aug 2020, 1:45 PM
Clueless Coder
Clueless Coder - avatar
+ 2
For solving rect1 being stuck to rect2 currently, here is my demo for another Sololearner a year ago : https://code.sololearn.com/W385n4ccicXX/?ref=app Use a flag to indicate that the shaped are overlapping, so that the direction will not be switching every frame.
2nd Aug 2020, 2:03 PM
Gordon
Gordon - avatar
+ 2
If it is vertical, you just need to check right side of rect1 with rect2.x
2nd Aug 2020, 2:05 PM
Gordon
Gordon - avatar
+ 2
Gordon I tried something else and it's got worse. This is impossible.
2nd Aug 2020, 2:35 PM
Clueless Coder
Clueless Coder - avatar
+ 2
I'll explain in a YouTube video, if you need.
2nd Aug 2020, 2:44 PM
Gordon
Gordon - avatar
+ 2
Thanks so much! I actually get it now!
2nd Aug 2020, 2:45 PM
Clueless Coder
Clueless Coder - avatar
+ 2
👍
2nd Aug 2020, 2:47 PM
Gordon
Gordon - avatar