Combining two objects using three.js is not possible due to concatenation

I am attempting to combine two objects (bar and circle) to create a ping pong racket in three js,

Even after using

var parent = new THREE.Object3D();
, the objects remain separate

This is the code for my function :

 function addPaddle() {
    var paddleGeometry = new THREE.CylinderGeometry(20, 90, 20, 64, 64),
      paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 }),
      paddle = new THREE.Mesh(paddleGeometry, paddleMaterial)
    paddle.rotation.x = 30

    var barre = new THREE.CubeGeometry(20, 20, 170)

    var materialBarre1 = new THREE.MeshBasicMaterial({
      color: 0xff0000,
      side: THREE.DoubleSide,
    })
    var barre = new THREE.Mesh(barre, materialBarre1)

    var parent = new THREE.Object3D()
    parent.add(paddle)
    parent.add(barre)
    scene.add(parent)

    barre.rotation.y = 180
    barre.position.x = 100
    barre.position.z = 2000
    barre.position.y = 300
    return paddle
  }

View Output Screenshot

Answer №1

To improve your code and easily identify errors, it's important to be more cautious and organize it efficiently. For instance, the declaration of var barre appears twice, once for a geometry and again for the mesh. It's advisable to use more descriptive and distinct variable names to prevent confusion. Additionally, avoid chaining var declarations to simplify future modifications.

Moreover, when grouping barre and paddle within a parent element, ensure that you return the entire parent object instead of just the paddle component.

Below is the same code but enhanced with better organization and unique variable names:

function addPaddle() {
    // Paddle
    var paddleGeometry = new THREE.CylinderGeometry(20, 90, 20, 64, 64)
    var paddleMaterial = new THREE.MeshLambertMaterial({ color: 0xff0000 })
    var customizedPaddle = new THREE.Mesh(paddleGeometry, paddleMaterial)
    customizedPaddle.rotation.x = 30

    // Barre
    var barreShape = new THREE.CubeGeometry(20, 20, 170)
    var barreSurface = new THREE.MeshBasicMaterial({ color: 0xff0000 })
    var newBarreMesh = new THREE.Mesh(barreShape, barreSurface)

    // Grouping Components
    var mainParent = new THREE.Object3D()
    mainParent.add(customizedPaddle)
    mainParent.add(newBarreMesh)
    scene.add(mainParent)

    // Transformations
    newBarreMesh.rotation.y = 180
    newBarreMesh.position.x = 100
    newBarreMesh.position.z = 2000
    newBarreMesh.position.y = 300

    return mainParent
}

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

Is it possible to utilize JavaScript for transmitting and storing data on a server?

Consider this scenario: When you submit a query on stackoverflow, the data you provide is entered into a text field. This information is then transmitted to the server for storage and eventual display to the user. Is it possible to code the functionality ...

Showing the number of guilds as an action displayed on the screen

I'm trying to make my bot say "Watching (number of servers it's in) servers!" with this code: const activities_list = [ "with the &help command.", "with the developers console", "with some code", "with JavaScript", client.guilds. ...

Exploring AngularJS capabilities: Retrieving data and headers from an HttpResponse

Currently, I have a REST API developed using Spring Boot. In the frontend, AngularJS 1.5 is being used. The login service not only sends data but also includes a header. I am wondering how I can effectively read both the data and header on the AngularJS ...

headerFilterParameters sans editor

My goal is to provide users with the ability to filter for specific predefined keywords using headerFilterParams in conjunction with headerFilter. However, the headerFilter example showcased on the Tabulator website triggers a cell editor as well. {title ...

Guide on embedding a map inside a popover using Bootstrap

I am trying to create a popover that displays a map inside it. I attempted this simple example: <!DOCTYPE html> <html> <head> <title>Bootstrap Example</title> <meta charset="utf-8"> <meta name="viewport" c ...

Value replaced by ajax response

Currently experimenting with Google Maps and the Geocoder, my goal is to iterate through a list of addresses, retrieve LatLng coordinates for each one, and then use that data to create markers using the setMarker function provided below. The issue I' ...

Inserting data into a table using variables in Mssql database management system

I'm really struggling to find a way to safely add my Variables into an MSSQL server. I've tried everything. Could someone please help me and provide the solution for adding my Variables into the Database? It is crucial that I prevent any possib ...

Looking to transfer data between files in Nodejs

This is the routes.js file I am working with const express = require('express'), router = express.Router(), loginHandler = require('../handler/loginHandler'), router.get('^/:userId/:userType/:sessId/:lang/:orgId/:merchantI ...

Backend undergoing fluctuations in hourly values

When passing JS dateTime to the backend using ajax(axios), I encountered a discrepancy in the timestamps. Prior to the post request, I have the following timestamp: Sun Nov 04 2018 21:53:38 GMT+0500 However, upon reaching the backend, the timestam ...

By checking the active box and completing the fields, the button will be activated

I'm currently working on a form that requires all fields to be filled out and the acceptance checkbox to be checked before the Submit button becomes active. If any entry or the checkbox is subsequently removed, the button should become inactive again. ...

Creating a table from data in your database using Firebase

Can anyone guide me on how to craft a data table similar to this examplehttps://i.sstatic.net/xiUNn.pngusing information from a Firebase database like shown here https://i.sstatic.net/YBzjz.png The table should have columns for ID, Title, Number of Answer ...

Encountering an Error when Integrating Pusher (real-time data library) with Next.js: PusherRequestError - Unexpected status code 400

I encountered an issue while trying to integrate Pusher into my Next.js application due to Vercel's restriction on websockets in their serverless functions. The error message I keep receiving after running the program with Pusher is: error - unhandled ...

Encountering an issue with extending the MUI color palette, receiving a "reading 'dark'" error due to properties of undefined

Encountering an issue when trying to expand the MUI color palette—getting this error instead: Error: Cannot read properties of undefined (reading 'dark') Take a look at my theme.ts file below: const theme = createTheme({ palette: { pri ...

What is the best way to extract user input from a bootstrap search bar and integrate it into an ajax request to fetch information?

I am currently utilizing HTML, Javascript, and bootstrap to develop a web application. However, I have encountered an obstacle. When using the code document.getElementById("input here"), it only returned an array of 0. My goal is to retrieve data from an A ...

Jquery loop using closures

I've been working on creating a plugin that involves passing handler functions for specific events. Consider the scenario below: I have two buttons, and when I click button 1, its label is supposed to change to 'Button A', while clicking but ...

Attempting to eliminate annoying alternative text from the lightbox photo gallery

My collection of artwork can be found on my website www.unlicensedeyesurgery.com. However, I am facing an issue with the Lightbox code that is being used. The problem lies in the fact that the Lightbox code utilizes the rel attribute of the anchor tag to d ...

The Group Hover feature in Tailwind CSS seems to only affect the initial SVG element and not any subsequent SVG children within the group

I encountered an issue with TailwindCSS (version 3.2.4) regarding group-hover, where hovering over elements with SVG child elements only seems to affect the first element and not the others. For a demonstration, I have set up an example on the TailwindCSS ...

My goal is to implement a confirmation modal prior to any route changes within Next.js framework

Below is the code block that I have: const onRouteChangeStart = React.useCallback(() => { if (formState.isDirty) { if (window.confirm('Confirmation message')) { return true; } NProgress.done(); throw "A ...

Steps to implement jQuery after executing the command "npm install jquery"

Greetings! I recently utilized npm install jquery to add jQuery to my project. However, I noticed that it was downloaded into node_modules\jquery along with some unnecessary files. My goal is to only move node_modules\jquery\dist\jquer ...

Using jQuery DataTable to fetch JSON data upon Document Loaded

I am struggling to implement a PHP script that returns JSON data to populate a DataTable. The PHP script 'api/exceptions_all.php' is as follows: <?php $select = "SELECT '', [FOR_PARTNER], [FOR_NAME] FROM [brokerage].[dbo].[for_ex ...