"Troubleshooting Three.Js: Dealing with positioning issues and coneGeometry

I'm attempting to construct a model of a rocket ship by adding various shapes to a large group and positioning them along specific axes.

However, when I try to use

rocketConeMesh.position.y = 15;

The shape doesn't move at all. My goal is to place the rocketCone (the nose of the rocket ship) on top of the rocketBody and have them both belong to the same group.

Upon running this code, I encounter the following error message:

"THREE.Object3D.add: object not an instance of THREE.Object3D."

This error pertains to the coneGeometry object in my code.

My current code setup is displayed below:

<script type="text/javascript">
            // Setting up the scene to contain all elements such as objects, cameras, and lights.
            var scene = new THREE.Scene();

            // Defining the camera's perspective parameters.
            var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

            // Creating a renderer and setting its size.
            var renderer = new THREE.WebGLRenderer();
            renderer.setSize(window.innerWidth, window.innerHeight);
            document.body.appendChild(renderer.domElement);

            window.addEventListener('resize', function()
            {
                var width = window.innerWidth;
                var height = window.innerHeight;
                renderer.setSize(width, height);
                camera.aspect = width / height;
                camera.updateProjectionMatrix();

            } );

          controls = new THREE.OrbitControls(camera, renderer.domElement);

            // Generating an object group to hold sub-shapes.
            var rocketGroup = new THREE.Object3D();
            scene.add(rocketGroup);

            // Representing the top cone of the rocket.
            var rocketCone = new THREE.ConeGeometry(6, 10, 6);
            var material = new THREE.MeshBasicMaterial({color: 0xcccccc, wireframe: true});
            var cone = new THREE.Mesh(rocketCone, material);
            rocketConeMesh = new THREE.Mesh(rocketCone,new THREE.MeshPhongMaterial())

            scene.add(cone);
            // Specifying the position of the rocket cone.
            rocketConeMesh.position.y = 15;

            // Adding the rocketCone to the lowpolyRocket group.
            rocketGroup.add(rocketCone);

            /******************************************************************************************************************/

            //var rocketBody = new THREE.CylinderGeometry(5, 5, 20, 32);
            //var material = new THREE.MeshBasicMaterial({color: 0xcccccc, wireframe:false});
            //var cylinder = new THREE.Mesh(rocketBody, material);
            //scene.add(cylinder);

            // Positioning and orienting the camera towards the center of the scene.
            camera.position.x = -30;
            camera.position.y = 20;
            camera.position.z = 30;
            camera.lookAt(scene.position);

            // Game logic    
            var update = function ()
            {
                //cone.rotation.x += 0.01;
                //cone.rotation.y += 0.005;
            };
            // Rendering the scene    
            var render = function ()
            {
                renderer.render(scene, camera);
            };



            // Running the game loop (update, render, repeat)
            var GameLoop = function ()
            {
                requestAnimationFrame(GameLoop);
                update();
                render();
            };
            GameLoop();

        </script>

Answer №1

Adding rocketCone to the rocketGroup using rocketGroup.add(rocketCone).

The code provided is incorrect because it attempts to add an instance of ConeGeometry to an Object3D, which is not possible. To fix this issue, change your code as follows:

rocketGroup.add(rocketConeMesh);

By adding the rocketConeMesh (derived from THREE.Object3D) to rocketGroup, it becomes a part of the scene graph and can have its transformation modified successfully.

Also, there seems to be redundancy in creating two meshes with the rocketCone geometry. It's recommended to remove the cone variable for better optimization.

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

"Integrating auto-expandable rows and subrows into a React table with the use

Presented here is my code for a React table using hooks. I encountered a challenge when trying to set all rows and subrows to be expanded by default upon opening the page. Despite attempting various solutions, none of them proved successful. Check out the ...

fill collections within backbone

Looking to optimize populating a Backbone collection, I have come across the push method but it requires iterating over all items: define([ ... ], function($, _, Backbone, imagesCollection, imageTemplate, gridView) { var AppView = Backbone.View.ex ...

Introducing the latest feature in Vue.js 3.2: script setup with dynamic $

I am attempting to generate a dynamic summary. <template> <ul> <li v-for="(category, i) in categories" :key="i" class="cursor-pointer text-capitalize q-mb-sm" @click="scrollTo('category&apo ...

Statement after post is not yielding any result

Below is a javascript function that I'm struggling with: function loginsubmit() { var url = "../php/loginsubmit.php"; var data = ""; ajaxRequest(url, "POST",data , true, insertNewBody); } This function is responsible for sending an ajax ...

Enhancing the Calculator Functionality in a React Program

I'm struggling to incorporate a reset button into the input field, similar to CE on a calculator. I'm facing challenges when it comes to integrating it within the existing code structure. import { useRef } from "react"; import './A ...

What could be the reason behind the app.get middleware not functioning properly following the app.use middleware in ExpressJS?

My server.js file includes the following code. However, I've encountered an issue where the code in app.get() function works fine when the app.use() middleware is commented out. But, when both are included, the get request doesn't seem to run. An ...

What steps should I take to address the issue of the document not being defined?

I encountered an error that I initially thought was related to node.js, but now I'm not entirely sure. How can I go about resolving this issue? [Running] node "c:\Users\Lenovo\Desktop\projectjs\index.js" c:\User ...

What is the best way to show an image on the screen once a submit button is clicked?

I have a hidden loader-bar gif that I want to display when the user submits a form. Here is the code: <p class="loadingImg" style="display:none;"><img src="/design/styles/_shared/classic-loader.gif" alt="" /></p> Is there a way to ...

How come my responsive design functions properly when I test it using the mobile device tool, but fails to work when I manually decrease the browser size?

While testing my responsive design, I've noticed that using the toggle device tool in the Inspect tools of my browser produces the expected results. However, when I simply resize the browser window manually, the design does not respond as it should. C ...

Guide to animating an svg icon when hovering and when it comes into view on the screen

Seeking a way to animate the SVG icon upon mouse hover or when it comes into view. Encountering an issue where the icon animates on appearance but not on mouse hover. Any straightforward solutions for this? Below is the code snippet. Using window.addEve ...

Is there a way to incorporate TypeScript type definitions into a JavaScript module without fully transitioning to TypeScript?

Although the title may be a bit confusing, it encapsulates my query in a succinct manner. So, here's what I'm aiming to achieve: I currently have an npm module written in JavaScript, not TypeScript. Some of the users of this module prefer using ...

Is there a way to improve the efficiency of this jQuery function that toggles the image source?

I have implemented a functionality that seems to work, but I'm unsure if it's the most efficient solution. I couldn't find a ready-made 'copy-paste' solution online, so I ended up writing this code myself. I am sticking with the &l ...

What is the process for running a JavaScript file within another JavaScript file?

I am facing an issue with creating a JavaScript file that can execute multiple other JS files, but I am unable to get it working. Is there a method to call and run a JavaScript file within another JavaScript file? For instance: function executeJSFile() { ...

My PHP errors and success messages are not being displayed properly after an AJAX success

After making an AJAX call to submit a form, I would like to display either the PHP success message or error message upon completion. This is my current AJAX success function: success: function (data) { resultSuccess = $(data).find("#success") ...

Tips on defining the specific CSS and JavaScript code to include in your Flask application

I am currently working on a web application using Flask and I need to specify which CSS and JS files should be included in the rendered HTML page based on a condition. There are times when I want to include mycss1.css and myjs1.js: <link href="/sta ...

The header.ejs file is unable to render the image

As I work on my website, I had the brilliant idea of using a JavaScript HTTP header (via express) to avoid having to recreate a header and footer on every single file. And guess what? I successfully implemented it! But now I'm facing an issue with ins ...

Obtain the clicked href value and pass it from one page to another

Welcome, everyone, I am currently in the process of creating a webpage that serves the following functions: When the administrator accesses the page, they will see a form populated with information fetched from a database: https://i.sstatic.net/RrE9l.png ...

interactive division element

I'm facing an issue with making my div tag clickable in a five-star rating system. The stars are supposed to represent different values from 1 to 5. Even though the image changes on mouseover, clicking the div tag does not yield any result. This is ...

Achieving two-way data binding in a directive without using an isolated scope

Implementing scope: { ... } in a directive creates an isolated scope that doesn't inherit from its parent. However, my usual practice has been to utilize this for easily declaring HTML attributes with bi-directional data binding: scope: { attr1: ...

Input a new value into the registration field to update it

My current task involves adding a new text field to a React form using a button. I need this new input text field to be able to store data on a register. How can I go about setting this property? Typically, when creating a text field, you would utilize co ...