Check if a UiElement touch another UiElement [Answered]

27 Replies
Yawnder
Yawnder3y ago
(I can safely say you're not speaking Québec-French) But for your question: You have the position and size of the items?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
Are they all considered circles, square, or something else?
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
Then you check if any corner of an item is within the boundaries of another. Not the same. Contained within.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
It's not quite that many though.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
for (int i = 0; i < [Whatever]; i++)
{
for (int j = i + 1; j < [Whatever]; j++)
{
}
}
for (int i = 0; i < [Whatever]; i++)
{
for (int j = i + 1; j < [Whatever]; j++)
{
}
}
See how the inner for starts at i + 1, not 0.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
Say it in French and I'll tell you the word you're looking for 😉
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
"I'll keep you posted."
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
I wouldn't do that. If you do, you can't start the inner loop at the current index of the outer one. And it's not only "is not". Go for a simpler example. Let's say you have 3 coins (a, b, c). How would you check if any coins touch.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
You check if A touches B, if A touches C, if B touches C. You never check if B touches A or C touches A since you already did the other way around.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
And to see if they overlap, since they're all the same size, you check if a corner is inscribed within a rectangle.
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
Meaning: corner.X >= shape2.Left && corner.X <= shape2.Right && corner.Y >= shape2.Top && corner.Y <= shape2.Bottom Sure (if you don't understand something I say, I can translate. I just have to keep it generally in English to allow for anyone to step in and help.)
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
Not quite. I think you should break it down first. Extract the values you'll need, it will make it easier to debug:
// Between the two for:
var corner1X = ...
var corner1Y = ...
var corner2X = ...
var corner2Y = ...
var corner3X = ...
var corner3Y = ...
var corner4X = ...
var corner4Y = ...

// In the two for:
var leftBound = ...
var rightBound = ...
var topBound = ...
var bottomBound = ...
// Between the two for:
var corner1X = ...
var corner1Y = ...
var corner2X = ...
var corner2Y = ...
var corner3X = ...
var corner3Y = ...
var corner4X = ...
var corner4Y = ...

// In the two for:
var leftBound = ...
var rightBound = ...
var topBound = ...
var bottomBound = ...
And then do your checks. So for example, corner1X will probably be Canvas.GetLeft(Canvas_Board.Children[i]) while corner2X will probably be Canvas.GetLeft(Canvas_Board.Children[i]) + ElementSize
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Yawnder
Yawnder3y ago
I went with 1 = top left, then clockwise. Or you can do 1 2 3 4 It's not important, as long as you know!
Unknown User
Unknown UserOP3y ago
Message Not Public
Sign In & Join Server To View
Accord
Accord3y ago
✅ This post has been marked as answered!

Did you find this page helpful?