Balancing one cube atop another in a dynamic manner

Currently, I am developing a project using three.js, where users have the ability to dynamically adjust the dimensions of objects within the scene. Specifically, there are two boxes with varying sizes and one box that must always remain positioned on top of the other.

The challenge arises when trying to ensure that if the height of the cube changes, the chimney.position.y should also update accordingly. Despite attempting multiple approaches, I have not been successful in achieving this functionality.

Below is a snippet of the code I am working with:

var cubeHeight = 0.1;
var boxGeometry = new THREE.BoxGeometry(0.1, cubeHeight, 0.1);
var boxMaterial = new THREE.MeshLambertMaterial({color: 0x000088});
cube = new THREE.Mesh(boxGeometry, boxMaterial);
cube.position.set(0, cubeHeight/2, 0);
scene.add(cube);

var chimneyGeometry = new THREE.BoxGeometry(5, 0.1, 5);
var chimneyMaterial = new THREE.MeshLambertMaterial({color: 0x000088});  
chimney = new THREE.Mesh(chimneyGeometry, chimneyMaterial);
chimney.position.set(0,cubeHeight,-12.5);
scene.add(chimney);

// GUI panel for user interaction
gui = new dat.GUI();
parameters = { 
    length: 1, height: 1, width: 1,
    tall: 1 
}

// Folder for defining house dimensions (in cm)
var folder1 = gui.addFolder("House dimensions (cm)");
var cubeX = folder1.add(parameters,"length").min(1).max(2000).step(1).listen();
var cubeY = folder1.add(parameters, "height").min(1).max(2000).step(1).listen();
var cubeZ = folder1.add(parameters, "width").min(1).max(2000).step(1).listen();
var chimneyHeight = folder1.add(parameters, "tall").min(1).max(700).step(1).listen(); 
folder1.open();

 // Function to handle cube transformations
 // Adjustments made to keep the structure stable
 cubeX.onChange(function(value){cube.scale.x = value; roof.scale.x = value;});
 cubeY.onChange(function(value){cube.scale.y = value; cube.position.y = (cubeHeight * value) / 2;});
 cubeZ.onChange(function(value){cube.scale.z = value;});
 chimneyHeight.onChange(function(value){chimney.scale.y = value; chimney.position.y = ((cubeHeight * value) / 2) + cubeY;});

If you have any insights or solutions to resolve this issue, your input would be greatly appreciated! Thank you in advance for your assistance!

Answer №1

Below is a potential solution:

    Adjusting cubeY onChange:
        cube.scale.y = value;
        cube.position.y = (cubeHeight * value) / 2;
        chimney.position.y = (chimneyHeight * chimney.scale.y) / 2 + cube.position.y * 2;

    Modifying chimneyY onChange:
        chimney.scale.y = value;
        chimney.position.y = (chimneyHeight * value) / 2 + cube.position.y * 2;
    });

To see it in action, check out this fiddle: http://jsfiddle.net/z1yd342b/

Utilizing three.js r71 library

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

Ways to set the initial value of an input[range] in Angular2 when the value exceeds 100

After encountering a similar issue with AngularJS, I posted a question on StackOverflow titled How to initialize the value of an input[range] using AngularJS when value is over 100. As I dive into learning Angular2, I am curious if it handles initializatio ...

executing a function within a Vue.js 3 component's <template> tag

As I delved into the world of fetching APIs and displaying their data on a Vue.js3 webpage, I encountered an interesting issue. My goal was to showcase different dog breeds and have a random dog image display when a breed is clicked. However, when I tried ...

How to properly structure a Vue file in vscode to meet eslint guidelines

Essentially, what I am seeking is uniformity in: the way vscode formats my .vue file how the eslint checker scans my .vue file when I execute npm run .... to launch the server or build the target Currently, after formatting my document in vscode and then ...

Is it possible to utilize the react state dispatch within an effect before it has been defined?

The code below successfully displays the number 1: "use client"; import { useEffect, useState } from "react"; export function StateTest() { useEffect(() => { setX(1); }); const [x, setX] = useState(0); return <div ...

How can I use JavaScript to retrieve the current time from the time.nist.gov NTP server?

Looking for guidance! I'm new to coding and would greatly appreciate detailed instructions and examples. I've been struggling for hours trying to solve this issue - the online resources I found have not been helpful at all. Unfortunately, I don&a ...

Customize the pagination template in ui-bootstrap to better suit your needs

I have customized the pagination template in AngularJS to display pagination. Here is the modified HTML code: <ul uib-pagination ng-model="source_pagination.current" template-url="pagination.html" total-items="source_pagination.total_pages" items-per-p ...

Is it possible to relocate the file export button to the row of pagination buttons within Datatables implemented with Bootstrap 5?

Utilizing Datatables within a Bootstrap 5 theme has been seamless, with pagination and file export features working effectively. However, the file export button does not align with the theme, prompting me to seek a way to discreetly place it in the same ro ...

Having trouble loading static assets in next.js framework

I am currently using Next.JS to develop a small landing page that features videos and images. To house these static assets, I decided to create a static folder and call the videos/images from there. However, I have encountered an issue where Next is unabl ...

jQuery toggle buttons to show or hide on radio button selection

I have a pair of buttons and a pair of radio buttons Buttons 1) btnErp 2) btngoogle Radio Buttons 1) rdiogoogle 2) rdioErp When I select 'rdiogoogle', 'btngoogle' should be visible while 'btnErp' should be hidden. Conve ...

What methods can be used to troubleshoot an issue of an AngularJS function not being called

I am facing an issue with a dynamic table I have created. The rows and action buttons are generated dynamically when the user clicks on the Devices accordion. However, there seems to be a problem with the function expandCollapseCurrent(). Whenever the use ...

Executing various functions within an event handler

My goal is to accomplish this task @click="update_value && selected = true" Unfortunately, neither of these options work: @click="update_value" @click="selected = true"> Is there a way to execute multiple methods with a single event handler ...

Using Jquery to attach an event listener to an iframe

**I am currently utilizing a jQuery textselect plugin to trigger alerts based on the selected text anywhere on the page, and it is functioning well with code like this: $(document).ready(function() { $(document).bind('textselect', function( ...

Is it possible to transfer an HTML table to a PowerPoint presentation directly from the client

Is there a specific jquery or javascript solution available for converting an HTML table directly into a PowerPoint presentation? So far, the only solution I have come across is html table export, which provides export options for various file formats. H ...

The Ajax function effortlessly receives the returned value and smoothly transitions to the error handling stage

When trying to retrieve data from an ajax request, my function needs to receive the returned data as an array of strings. During debugging, I am able to see the response, but at the same time, the error function is triggered. This is how my code looks: ...

ReactJS: Oops! Looks like there's an issue with the element type - it's invalid. We were expecting a string

I am in the process of setting up a basic server-side rendered React application. Listed below are the steps I have taken: Step 1: Creating a new React app using create-react-app: npx create-react-app my-ssr-app Step 2: Installing necessary dependencies: ...

How can you extract multiple values from a react select component?

return ( <Fragment> <div className="form-group"> <select name="sub" onChange={(e) => { const selectedPackage = e.target.value; setPackage(selectedPackage) }> ...

Is there a way I could customize this design to include buttons that smoothly navigate to the following section?

I'm attempting to create a Web Page that scrolls horizontally. Currently, the template relies on J Query and CSS to manage width but users must still manually drag the scroll bar at the bottom of the page. Is there a way to incorporate arrows or click ...

What method can we use to temporarily route traffic from one URL to another URL for a specific amount of time?

Is there a way to use javascript/jquery code that will redirect my website to a different domain, but only during specific hours? I'm looking to have the redirection occur between 2 a.m. and 4 a.m., so that it only works for two hours out of the day. ...

Identifying Changes in ReactJS Pages

As I delve into learning ReactJS, I have been experimenting with various websites that utilize this technology. Presently, my focus is on making an AJAX call to an API based on the URL of a page. The issue arises when clicking a link breaks my code due t ...

Is there a way to use a button to delete items stored in localStorage?

I am trying to create a button that will clear the local storage for a user using localStorage.clear();, but it is not working as expected. Unfortunately, if you check out the demo, you'll see that it doesn't work properly. Here's the demo ...