A guide to determining the position of 3D objects in threejs and arranging them within a scene to ensure they do not intersect

Currently, I am developing a cargo planning application that involves loading cargo into containers. To achieve this, I am utilizing three.js to create a 3D model. Despite being new to three.js, I have successfully created 3D objects (cargo) and placed them inside a container (another 3D object). However, I am encountering a challenge where the 3D objects overlap when multiple items are added to the container.

In my attempts to address this issue, I have only resorted to basic mathematical calculations to determine the correct positioning of objects within the scene.

For reference, here is the JSFiddle link:

https://jsfiddle.net/nro48e6u/1/

The logic responsible for calculating the object positions resides in the 'addCargo' function, which primarily relies on fundamental math concepts. The code snippet for this function is as follows:

function addCargo(dimension, color, isPallete) {
    // Code implementation for adding cargo objects
}

Despite implementing these calculations, I'm still facing challenges with objects overlapping within the container. The current solution does not effectively prevent this issue, and I remain uncertain about how to resolve it.

Answer №1

If you're looking to create a relaxing method, consider adjusting each box slightly in an infinite loop until all boxes are content.

dx = 0.1;
while(overlap_at_all(boxes)) {
    foreach(var box in boxes) {
        var overlapping_boxes = get_overlapping_boxes(box, boxes);
        foreach(var overlapping_box : overlapping_boxes)
            push_away_boxes(box, overlapping_box, dx);
    }
}

The "get_overlapping_boxes" function handles collision detection and returns colliding boxes with the current one efficiently using BVH or another data structure like an OctTree for good results. The same goes for "overlap_at_all."

In "push_away_boxes," both boxes move opposite directions by dx (e.g., 0.1), gradually making them non-overlapping. Increasing dx speeds up convergence but sacrifices precision. Introducing a container box as a constraint could affect convergence speed, so limiting iterations can prevent endless cycling without guaranteeing a solution.

This method does not ensure finding the optimal arrangement with minimal space wastage.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The background image's fadeInOut transitions create a strange phenomenon where all the text appears in white

So, here's a strange issue I'm facing. On my website, the background image changes with a smooth fadeIn/Out transition. Video: Website in action: Here is the HTML markup: <div class="fondo" onclick="descargar(2,500);descargar(1,500);"> ...

What is the best way to attach an event listener to every child node within a div that is stored in an array of divs?

Currently, I am dedicated to learning JS on my own and have decided to hold off on using jQuery until my JavaScript skills have improved. The objective is to attach an event listener for the click event to all divs of a specific class and have all child n ...

Is it possible to apply draggable functionality in the same way you attach events to an arbitrary node and a selector?

Here is a possible solution: function myFunction(){/*.....*/} $("body").on("click", "li.itemlist", function(){ alert("ping"); }); This code sets up a click event for all li.itemlist elements under the body element. However, is there a way to sim ...

The lower section of the scrollbar is not visible

Whenever the vertical scroll bar appears on my website, the bottom half of it seems to be missing. For a live demonstration, you can visit the site HERE (navigate to the "FURTHER READING" tab). HTML: <!DOCTYPE html> <html lang="en"> <h ...

"Mastering the Art of Patience: Guarding Your Way Through

When I set up three guards on a route, it appears that all guards are assessed right away. {path: '', component: OptionsComponent, canActivate: [ GuardOne, GuardTwo, GuardThree]} The issue is that I need GuardTwo to wait until GuardOne complete ...

The default data value in the Vue component is taking precedence over the value I specified in beforeRouteUpdate

When navigating in my Vue application, I utilize vue-router's beforeRouteEnter and beforeRouteUpdate to retrieve data from a REST API. <template> <div> <h2>{{ league.name }} </h2> <user-list :users="league.leaderb ...

What is the method for setting a default image to be preloaded in filepond?

Currently, I am working on a Laravel view for editing a record which includes an associated image. My goal is to have the image preloaded inside the input file so that when you submit the form, the same image is sent or you can choose to change it. // Con ...

Updating ng-model value in AngularJS controller does does not reflect changes in selection

I'm encountering an issue while trying to upgrade my ng-model within a selection feature. Here is the HTML code I am currently using: <div ng-app> <div ng-controller="Ctrl"> <select ng-model="viewmodel.inputDevice" ...

Exploring Frontend Package Dependencies in Different Versions

As I develop the React frontend for a library package, I want to clearly display to users the specific version of the library being utilized. Apart from manual methods or relying on Node.js, I am unsure of alternative approaches to achieve this. I am curi ...

Pause the event listener temporarily and reattach it after specific actions have been completed

I am currently working on a React app that includes a scrollable div. Here is an example: <main id="main" onScroll={this.handleScroll}> My question is, how can I temporarily remove the onScroll event listener from the main element and then reattach ...

Centering the scrollIntoView feature on mobile devices is presenting challenges with NextJS applications

Description While navigating on mobile browsers, I'm facing a challenge with keeping an element centered as I scroll due to the browser window minimizing. I've experimented with different solutions such as utilizing the react-scroll library and ...

Having trouble with the JavaScript code for a basic GPA calculator?

I am having issues with my code for a basic grade calculator. Even though I can input numbers, the final grade is not displayed on the screen. Can someone please help me identify what mistake I might be making? Please ignore the Japanese text, as it's ...

Incorporating a MUI5 theme into a custom emotion-themed application

I'm currently working on an application that incorporates multiple themes. One of these is a Material UI theme specifically designed for MUI v4, while the other is an emotion theme used by non-MUI components. In my attempt to transition to MUI v5, I ...

Missing HTML elements when refreshing AngularJS page

I encountered a major issue with my simple Angular app. Whenever I refresh the page, all of the HTML content disappears! My problem is not related to Angular data or session/local storage, but only with the HTML itself. For instance, when I access the ...

Error message: Unable to locate module using import instead of require

I am currently in the process of transitioning from using require to using import for all modules within my project. However, I have encountered some challenges with older npm modules that only provide instructions for require. For example, when it comes ...

The process of incorporating content from a different page into a Bootstrap 5 Modal can be achieved by using Laravel 10

Here is a snippet of my code from index.blade.php: <a data-bs-toggle="modal" data-bs-target="#modal_frame" href="{{route('caborCreate')}}">link</a> This is how the modal is coded in the create page - cre ...

Finding the absolute root path of a npm package in Node.js

Objective I am on a quest to find a reliable method to ascertain the absolute root path of an npm package that has been installed in Node.js. Challenge Although I am aware of require.resolve, it only provides the entry point (path to the main module) an ...

Retrieve the direction of panning when the panning stops with hammer.js and limit the possible

I have integrated the hammer.js library along with its jQuery plugin. I followed the documentation and used the following code to initialize it on .game-card-mobile divs: /* Creating a Hammer object for swipeable game cards */ var $gameCard = $('.gam ...

Is it necessary to use JS/JQ to trigger PHP form data?

Can PHP files/functions be executed without reloading the page? It can be quite disruptive when developing a chat app and every time you send a message, the entire page refreshes. I attempted to use AJAX but it didn't work. Is it not possible to send ...

What is the best way to retrieve data from within a for loop in javascript?

Seeking assistance in Typescript (javascript) to ensure that the code inside the for loop completes execution before returning I have a text box where users input strings, and I'm searching for numbers following '#'. I've created a fun ...