computational method for organizing balls in a circular formation

I need help arranging 10 spheres in a ring using code. So far, this is what I have, but it's not working as expected.


            const sphereGeometry = new THREE.SphereGeometry(300, 20, 20);
            const sphereMaterial = new THREE.MeshLambertMaterial({ color: 0x888888 });
            const numSpheres = 10;
            let sphere;
            const spheres = [];
            let startX = 0;
            let startY = 500;
            let radius = 80 * Math.sin(angle);

            for (let i = 0; i < numSpheres; i++) {
                sphere = new THREE.Mesh(sphereGeometry, sphereMaterial);
                console.log("sphere" + i);
                let angle = (360 / numSpheres) * i * Math.PI / 180;
                sphere.position.set((startX + Math.cos(angle) * radius), 
                                    (startY + Math.sin(angle) * radius), 
                                    100);
                scene.add(sphere);
                spheres.push(sphere);
            }

I'm not very good at math, so if anyone can spot where I've gone wrong, I would really appreciate the help. Thank you!

Answer №1

            Creating a series of bubbles in a 3D scene involves setting various parameters such as radius, geometry, material, and positioning. In this example, five bubbles are generated with a specific start position and angle.
            var bubbleR = 400;
            var bubbleGeo = new THREE.SphereGeometry(bubbleR, 10, 10);
            var bubbleMat = new THREE.MeshLambertMaterial( { color: 0x888888 } );
            var numBubbles = 5;
            var bubbleMesh;
            bubbles = [];
            var startX = 1000;
            var startZ = 1000;
            for (var i=0; i<numBubbles; i++) {
                bubbleMesh = new THREE.Mesh(bubbleGeo, bubbleMat);
                var angle = 360/numBubbles*i*Math.PI/180;
                bubbleMesh.position.set((startX + Math.sin(angle)*bubbleR) * 2, 
                                         1000, 
                                         startZ + (Math.cos(angle)*bubbleR) * 2
                );
                scene.add(bubbleMesh);
                bubbles.push(bubbleMesh);       
            }

It seems that adding the array of bubbles to the scene after the loop is causing some issues. Additionally, attempting to group the bubbles using THREE.group is not working as intended at the moment, preventing the ability to position the entire group.

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

Using Node.js for a game loop provides a more accurate alternative to setInterval

In my current setup, I have a multiplayer game that utilizes sockets for asynchronous data transfer. The game features a game loop that should tick every 500ms to handle player updates such as position and appearance. var self = this; this.gameLoop = se ...

Advantages of using individual CSS files for components in React.js

As someone diving into the world of Web Development, I am currently honing my skills in React.js. I have grasped the concept of creating Components in React and applying styles to them. I'm curious, would separating CSS files for each React Component ...

Split up the author page description with a new line break for better readability on WordPress

I have a unique plugin that transforms the Biographical Info editor into a standard editor interface. However, I am facing an issue where line breaks/new rows created in the backend are not visible on the front end, and I'm unsure of where the problem ...

Ensuring the model accurately reflects the input's value attribute in AngularJS

I am in the process of creating a set of image "radio buttons" where only one can be selected per group. However, as a newcomer to Angular, I'm facing difficulties in maintaining the value and using it as my ng-model. Additionally, I am looking for a ...

Ways to verify the element prior to the completion of the request?

Utilizing Angular and Playwright Within my application, I have incorporated 2 buttons - one for delete mode and another for refreshing. Whenever the user triggers a refresh action, the delete mode button is disabled. Once the request returns, the delete m ...

Discovering the presence of a component in Angular 1.5

Link to embedded content $injector.has('myMessageDirective') is returning true, while $injector.has('myMessageComponent') is not. Has anyone else encountered this issue or found a solution? I am concerned that my components may not be ...

"Is it possible to move the text on the canvas by dragging it to where you want it to be

Seeking help again after an unsuccessful attempt. How can I allow the user to add text to the canvas by dragging it to their desired location? For example, if they input text somewhere, it should appear on the canvas and then be draggable to any position ...

Turn off a feature on older buttons

For my project, I am looking to dynamically add specific elements like buttons. However, every time a new button is added, the function call for the old button gets duplicated. var btnElem ='<button type="button"class="doSomething">Button< ...

Animating CSS Pixel Fragments

After applying a simple CSS animation that moves size and box shadows from one side of the screen to the other, I am noticing residual pixel fragments left behind. To see this issue in action on Chrome 66, check out the Code Pen: https://i.sstatic.net/Gl ...

The RemoveEventListener function seems to be malfunctioning within Angular2 when implemented with TypeScript

I am currently incorporating three.js into Angular2. The code I am using is quite straightforward, as shown below. this.webGLRenderer.domElement.addEventListener('mousedown', ()=>this.onMouseDown(<MouseEvent>event), false); this.webGLR ...

What is the best way to convert a graphql query into a JSON object?

I'm facing an issue where I need to convert a GraphQL query into a JSON object. Essentially, I have a query structured like the example below, and I'm seeking a method to obtain a JSON representation of this query. Despite my efforts in searching ...

Error message: "Property undefined when Angular attempts to call a function from jQuery/JavaScript."

I'm currently attempting to invoke an angular controller from my JavaScript code. This is my first encounter with Angular and I must admit, I'm feeling a bit overwhelmed! I've been following this example: Unfortunately, when testing it out ...

Having trouble initiating NPM in a terminal

I keep encountering an issue whenever I try to start NPM using a command in the terminal. Here is the error message that appears: npm ERR! npm ERR! Did you mean one of these? npm ERR! npm star # Mark your favorite packages npm ERR! npm stars # Vi ...

auto-scrolling webpage as elements come into view

Here is some jQuery code I have: $(".col-md-12").hide(); $(".button-div").hide(); $(".featurette-divider").hide(); $(".footer").hide(); $(".first").fadeIn(1000); $(".second").delay(900).fadeIn(1000); $(".third").delay(1800).fadeIn(1000); $(".fourth").dela ...

There seems to be a syntax error lurking within npm.js, and for some reason npm insists on utilizing version 10.19.0 of Node.js despite my attempts to update it. The reason behind this behavior

Apologies if this question seems silly, but just a couple of days ago my code was running perfectly fine. Then today when I tried to load it, I encountered all sorts of errors. I am fairly new to node and npm, so I suspect it could be related to version ma ...

Unable to navigate beyond the boundaries of an app

I am currently facing an issue with my authentication service where it redirects to the identity server in case of certain errors. I attempted to achieve this using window.location.href = environment.authCodeFlowIssuer;, however, an error occurred: Ref ...

jQuery wrapAll issue

I have a repeating group of three divs in my code that I need to wrap together. Here's an example from my HTML: <div class="one" /> <div class="two" /> <div class="three" /> <div class="one" /> <div class="two" /> <d ...

How can I deactivate a Material UI button after it has been clicked once?

Looking to make a button disabled after one click in my React project that utilizes the MUI CSS framework. How can I achieve this functionality? <Button variant="contained" onClick={()=>handleAdd(course)} disabled={isDisabled} > ...

Using a timer to make Ajax calls twice on a single webpage

Can multiple ajax calls be made simultaneously on the same page to different receiving div tags? I am struggling to find a solution to this issue. I have a total of 3 pages: a home page, and two PHP pages named status and alert which echo the results. Wi ...

Tips for passing the id using jQuery in an AJAX request to execute a delete action

How can I remove an element from a list by clicking the delete button? https://i.sstatic.net/dQuOu.png I have created an ajax call for this purpose: .ajax({ type : "GET", url : 'getVenueList&apo ...