How can I generate spheres in threejs with varying radii while all passing through the shared point located at coordinates (0,0,200)?

Is there a way to generate spheres in threejs where the radius increases while always passing through a single point at 0,0,200? This would allow the origin of each new sphere to move along the z-axis.

Appreciate any help, AriemWebgl

Answer №1

To create a "parent" Object3D and connect the spheres to it, simply use the following code:

var sphereParent = new THREE.Object3D();
var numOfSpheres = 8;
for(var j = 0 ; j < numOfSpheres; j++)
{
   var radiusSize = 10 + j;
   var sphereGeo = new THREE.SphereGeometry( radiusSize, 16, 8 );
   var sphereMat = new THREE.MeshLambertMaterial( { color: 0x0000ff } );
   var nextSphere = new THREE.Mesh( sphereGeo, sphereMat );
   sphereParent.add( nextSphere );
}
sphereParent.position.set(0,0,200);
scene.add(sphereParent);

Answer №2

In my opinion, all it takes is the creation of a few meshes and adjusting their positions:

let numberOfCylinders = 10;
for(let j = 0 ; j < numberOfCylinders; j++){
   let height = 15 + (3*j);//increment the height based on the counter
   let cylinder = new THREE.Mesh( new THREE.CylinderGeometry( 5, 5, height, 32 ), new THREE.MeshPhongMaterial( { color: 0xee3333, shininess: 100, wireframe:false } ) );
   cylinder.position.z = -100;//or cylinder.position.set(0,0,-100);
   scene.add( cylinder );
}

Answer №3

This age-old conundrum may have been resolved long ago, but since it remains unanswered, let's shed some light on it for future readers...

The number of spheres with a radius R sharing a common surface point P is equivalent to the surface area of a sphere with a radius R centered at P, which is 4πR². This is because their centers are points on the same sphere centered at P:

In scenarios like the one presented in this question, involving internally tangent spheres, mathematical calculations reveal that the center of the updated sphere (C₁) will lie along the direction between the shared point (P) and the original sphere's center (C₀), at a distance equal to the difference in their radii (R₁ - R₀) from the latter (C₀):

When dealing with positional and directional vectors, it's important to understand:

  • To move a position by a certain distance in a particular direction, we add the product of the direction vector and the distance to the original position.
  • A direction between two positions can be calculated as the normalized difference between the second and first position vectors.

Hence,

position(C₁) = position(C₀) + direction(P, C₀) * (R₁ - R₀)
. Alternatively, for a more concise solution using Three.js, we can directly update via
position(C₀) += normalize(C₀ - P) * (R₁ - R₀)
and scale by R₁ / R₀, as shown below:

sphere.position.addScaledVector(sphere.position.clone().sub(point).normalize(), newradius - oldradius);
sphere.scale.setScalar(newradius / oldradius);

Note: Circles were used in the illustrations for simplicity, but imagine them as cross sections through the spheres' midpoints if it helps. The method described above applies to any point in any given position—it doesn't have to be restricted to the Z-axis or even confined to the sphere surfaces.

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

Issue: Unable to utilize import statement outside a module as per the guidelines of the vue-test-utils official tutorial

I'm struggling to get Vue testing set up with vue-test-utils and jest. I followed the installation guide at https://vue-test-utils.vuejs.org/installation/#semantic-versioning, but can't seem to figure out what's wrong. Here's what I&apo ...

Will cancelling a fetch request on the frontend also cancel the corresponding function on the backend?

In my application, I have integrated Google Maps which triggers a call to the backend every time there is a zoom change or a change in map boundaries. With a database of 3 million records, querying them with filters and clustering on the NodeJS backend con ...

Reposition div when clicked

I have encountered a challenge where I am unable to perform a small task. My goal is to have the position of "div1" change upon clicking on "div2", taking into account that "div2" is nested inside "div1". Additionally, when clicking on "div2" again, "div1" ...

Comparing two Objects in JavaScript results in automatic updates for the second Object when changes are made to the first

Can someone please assist me with a hash map issue I'm encountering in my for loop? When resetting the second object, it unintentionally alters the Map values of the previous Key in the Hash Map. Any guidance on how to prevent this behavior would be g ...

The onClick event for "history.go(0)" is functional in Internet Explorer, but it does not work in Mozilla Firefox. What can be done to address this problem specifically for Mozilla browser?

After experimenting with different browser compatibility, I have discovered a way to clear all fields when clicking the "New" button. By using the code onClick="history.go(0)", the fields are successfully emptied in IE but unfortunately fail in Mozilla. ...

What is the method for displaying map tooltips by default rather than on mouseover?

Currently, I have a script set up to display a map with two markers. Whenever I hover over one of the markers, a popup tooltip appears with location information. My question is, how can I make the information appear by default without needing to hover ov ...

Is there a way to successfully submit multiple locations, each separated by commas, through the multipart form?

Here is my HTML form: <form method="POST" enctype="multipart/form-data" v-on:submit.prevent="handelSubmit($event);"> <div class="clear"> <div class="col-md-3"></div> <div class="col-md-6"> <div class="form ...

What is the best way to run an external JavaScript file at regular intervals?

I enjoy loading an external JavaScript file every 10 seconds, or whenever the page body is clicked (in which case, the script should only run if a few seconds have passed). If you could provide me with some documentation on this topic, that would be grea ...

Update a specific element within Angular framework

Just starting out with angular and facing a seemingly simple issue that I can't seem to solve despite trying various solutions found on SO. I have created a login component where upon submission, the user is redirected to their profile page. While I a ...

How can AngularJS handle multiple routes using unique templates while sharing the same controller?

I am currently researching whether I can achieve the functionality described in the title. Here are my initial thoughts: Let's consider the following routes I have set up: .when('/', { templateUrl : 'partials/homepage.html&ap ...

Exploring jQuery's AJAX capabilities in handling cross-domain requests and implementing Basic Authentication

I attempted the solutions provided in the suggested duplicate question, but unfortunately, they did not yield the desired outcome. I have been struggling to utilize AJAX to POST data to a remote server's API from a local PC client (testing on Chrome ...

Trigger Modal using PHP/JavaScript

After implementing the code below on my page, I expected guests to be redirected to the main page post login and greeted with a small pop-up modal containing a thank you message. However, despite trying multiple variations of this code, it doesn't see ...

employing strings in passing functions as arguments

The script below, taken from a tutorial by Adam Khoury, is designed to create a timer that displays a message once it reaches completion. While I grasp the overall functionality of the code, I'm puzzled by the use of strings in certain parts: 1) Why ...

difficulty arises when attempting to invoke a viewmodel from another viewmodel inside a ko.computed function

Is it possible to have two view model functions in my JavaScript where one references the other? I am encountering an error with this setup. Here are my view models: var userViewModel = function (data) { var _self = this; _self.ID = ko.obs ...

Deactivate DropDownList within Update Panel with JQuery

Below is the Update Panel that I am working on: <asp:ScriptManager ID="ScriptManager1" runat="server" /> <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server"> <Triggers> <asp:AsyncPostBackTrigger ControlID="d ...

When using ReactJS, hovering over a row in a table should trigger a change in the background color of the corresponding row in another table with the same index

I need to create a feature where hovering over a row in the first table will highlight a corresponding row in the second table. The highlighting should be based on the index of the hovered row. Here is an example: <div> <table> < ...

Retrieve information from arrays within objects in a nested structure

I am working with structured data that looks like the example below: const arr = [{ id: 0, name: 'Biomes', icon: 'mdi-image-filter-hdr', isParent: true, children: [{ id: 1, name: 'Redwood forest& ...

Javascript - Execute function after all nested forEach loops have finished running

I'm finding this task to be quite challenging, as I am struggling to comprehend it fully at the moment. The issue involves nested forEach loops, and I require a callback function to execute once all the loops have finished running. I am considering u ...

Duplicating an array retrieved through a jQuery ajax request

Currently, I am encountering an issue while attempting to duplicate a JSON array within this specific JavaScript function: var test = new array(); function showUser(user, pass, remember) { $.getJSON("login.php", { username : user, password : pass, che ...

Strategies for capturing POST data sent to a JavaScript webpage

The request is sent from an external source beyond my control xyz.php <form action='https_:_//xyz.com/javascriptFile' method='POST'> <input type='text' name='data' /> </form> to: (My custom f ...