diff --git a/src/Components/Editor/Behaviors/RigidBodyBehaviors.ts b/src/Components/Editor/Behaviors/RigidBodyBehaviors.ts index 0bdefd0..b683baa 100644 --- a/src/Components/Editor/Behaviors/RigidBodyBehaviors.ts +++ b/src/Components/Editor/Behaviors/RigidBodyBehaviors.ts @@ -90,7 +90,7 @@ function constraintBodyInsideSpace( if (containerX < x) { containerProperties.x = x; } - if (containerX + containerWidth > width) { + if (containerX + containerWidth > x + width) { containerProperties.x = x + width - containerWidth; } @@ -98,7 +98,7 @@ function constraintBodyInsideSpace( if (containerY < y) { containerProperties.y = y; } - if (containerY + containerHeight > height) { + if (containerY + containerHeight > y + height) { containerProperties.y = y + height - containerHeight; } @@ -122,12 +122,7 @@ export function constraintBodyInsideUnallocatedWidth( // Get the available spaces of the parent const availableWidths = getAvailableWidths(container.parent, container); const containerX = Number(container.properties.x); - - // Sort the available width to find the closest one - availableWidths.sort( - (width1, width2) => - Math.abs(width1.x - containerX) - Math.abs(width2.x - containerX) - ); + const containerWidth = Number(container.properties.width); // Check if there is still some space if (availableWidths.length === 0) { @@ -136,6 +131,24 @@ export function constraintBodyInsideUnallocatedWidth( ); } + const middle = containerX + containerWidth / 2; + // Sort the available width to find the space with the closest position + availableWidths.sort( + (width1, width2) => { + let compared1X = width1.x; + if (width1.x < containerX) { + compared1X = width1.x + width1.width - containerWidth; + } + + let compared2X = width2.x; + if (width2.x < containerX) { + compared2X = width2.x + width2.width - containerWidth; + } + + return Math.abs(compared1X - middle) - Math.abs(compared2X - middle); + } + ); + // Check if the container actually fit inside // It will usually fit if it was alrady fitting const availableWidthFound = availableWidths.find((width) =>