"Creating a 3D rotation effect on individual objects nested within other objects using Javascript

In my software, I've created four Physijs.BoxMesh(...) shapes, all nested within an independent object. My goal is to rotate these four physics boxes as a whole, but once they are grouped under the 5th object, they no longer respond to rotation commands. Is there a way to rotate objects contained within another object?

EDIT : This is the entirety of my code:

<body></body>
<script src="http://gamingJS.com/Three.js"></script>
<script src="http://gamingJS.com/physi.js"></script>
<script src="http://gamingJS.com/ChromeFixes.js"></script>

<script>
  // Physics settings
  Physijs.scripts.ammo = 'http://gamingJS.com/ammo.js';
  Physijs.scripts.worker = 'http://gamingJS.com/physijs_worker.js';

  // Setting up the game environment:
  var scene = new Physijs.Scene({ fixedTimeStep: 2 / 60 });
  scene.setGravity(new THREE.Vector3(0, -100, 0));

  // Creating the camera:
  var width = window.innerWidth,
      height = window.innerHeight,
      aspect_ratio = width / height;
  var camera = new THREE.PerspectiveCamera(75, aspect_ratio, 1, 10000);
  camera.position.z = 500;
  scene.add(camera);

  // Displaying the rendered content:
  var renderer = new THREE.CanvasRenderer();
  renderer.setSize(window.innerWidth, window.innerHeight);
  document.body.appendChild(renderer.domElement);
  document.body.style.backgroundColor = '#ffffff';

  // ******** CODING BEGINS ON THE NEXT LINE ********

  var m1 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000}));
  var m2 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000}));
  var m3 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000}));
  var m4 = new Physijs.BoxMesh(new THREE.CubeGeometry(20, 150, 20), new THREE.MeshBasicMaterial({color:0x000000}));
  var ground = new Physijs.BoxMesh(new THREE.CubeGeometry(5e2, 10, 5e2), new THREE.MeshBasicMaterial({color:0x0000ff}), 0);  

  var tars = new Physijs.BoxMesh(new THREE.CubeGeometry(1, 1, 1), new THREE.MeshBasicMaterial());

  m1.__dirtyPosition = true;
  m1.position.x = -30;
  m2.__dirtyPosition = true;
  m2.position.x = -10;
  m3.__dirtyPosition = true;
  m3.position.x = 10;
  m4.__dirtyPosition = true;
  m4.position.x = 30;
  ground.__dirtyPosition = true;
  ground.position.y = -300;

  tars.add(m1);
  tars.add(m2);
  tars.add(m3);
  tars.add(m4);

  scene.add(tars);

  scene.add(ground);

  document.addEventListener('keydown', function(event) {
    switch(event.keyCode) {
      case 13:
        m1.setAngularVelocity(new THREE.Vector3(1, 0, 0));
        break;

      case 100:
        m2.setAngularVelocity(new THREE.Vector3(1, 0, 0));
        break;

      case 101:
        m3.setAngularVelocity(new THREE.Vector3(1, 0, 0));
        break;

      case 102:
        m4.setAngularVelocity(new THREE.Vector3(1, 0, 0));
        break;
    }
  });

  // Animation loop for the game
  function animate() {
    requestAnimationFrame(animate);
    renderer.render(scene, camera);
  }
  animate();

  // Running the physics engine
  function gameStep() {
    scene.simulate();
    setTimeout(gameStep, 1000/60);
  }
  gameStep();
</script>

Answer №1

Attempting to inherit properties from a parent Object3D onto a Physijs mesh will not succeed, as certain characteristics like angular momentum cannot be transferred in this way.

To achieve the desired functionality, it is recommended to utilize Physijs constraints.

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

Shut down a pop-up overlay

I've been facing a challenge in implementing greasemonkey to automatically close a modal when the "x" button is clicked. Allow me to share with you the code snippet for the webpage: <div class="modal-header"> <button type="button" class="clo ...

Transmitting data from the backend to AngularJS during page load

After diving into Angular JS for the first time, I've hit a roadblock at an essential stage. My setup consists of an AngularJS front end and Grails backend. Check out the code snippets below along with my query. URL Mapping entry: as "/graph"(cont ...

Tips on invoking a class method within lodash debounce function

Help needed: Issue with TypeError - Expected a function at debounce when calling a class method in the lodash debounce class Foo { bar() { console.log('bar'); } } const foo = new Foo(); _.debounce = debounce(foo.bar(), 300); ...

Create a list that starts with a header determined by an object's attribute within an array

Currently in Vue, I am attempting to create a list based on a specific property within an object. The array being retrieved from the vuex store is structured as follows: const array = [ { name: "British title string" nationality: "British" }, { ...

Angular: The Ultimate Guide to Reloading a Specific Section of HTML (Form/Div/Table)

On my create operation page, I have a form with two fields. When I reload the page using window.reload in code, I can see updates in the form. However, I want to trigger a refresh on the form by clicking a button. I need help writing a function that can r ...

utilize the useRef hook to display the total number of characters within a text area

Introducing the following component: import React from 'react'; export interface TexareaProps extends React.TextareaHTMLAttributes<HTMLTextAreaElement> { maxLength?: number; id: string; } export const Textarea = React.forwardRef( ( ...

Step-by-step guide to designing a custom eBay-inspired logo page using HTML and CSS

Recently, I stumbled upon a fascinating website design for eBay's new logo page. It got me thinking about how I could recreate something similar for my own website. The concept of having the logo visible through the gaps is quite intriguing. If you&a ...

What is the best way to transmit two variables in a single message with Socket.io?

My goal is to send both the clientid and username through the socket communication. client.userid = userid; client.username = username; client.emit('onconnected', { id: client.userid, name: client.username }); I attempted this approach, how ...

Is it better to throw an exception before doing any filtering?

Checking for the existence of removeId in the items before filtering to avoid exceptions. Is there a more efficient way to handle this without using two loops? This code may not be optimized due to the double looping process. const items = ...

Redux: triggering a dispatch when a component or function is initialized

I'm facing a challenge where I need to update a state only when a specific functional component is initialized. My initial approach was to try something like this: export default function SalesFeedPage(){ const {salesFeed} = useSelector((state) => ...

Using PHP and AJAX, populate a table based on the selection made from a dropdown

Hello, thank you for taking the time to review my issue. Let me outline the objective. I have successfully implemented two drop-down menus that are populated dynamically from a database. The query retrieves names and phone numbers (with plans to fetch mor ...

Unfulfilled Peer Dependency in react

Encountering javascript issues with react. Chrome error message when rendering page: Uncaught TypeError: Super expression must either be null or a function, not undefined at _inherits (application.js:16301) at application.js:16310 at Object.232.prop-types ...

In Electron, methods for detecting and resolving Error TS2304: Unable to locate the name 'unknown on(event: 'ready', listener: (launchInfo: unknown) => void): this are essential for smooth operation

Encountering an issue while running npm build. Electron version: 7.1.2 TypeScript version: ^2.5.1 Target version: ES2017 Here are the details of the error. When I performed the build under the following conditions: Electron version: 6.0.0 TypeScript ver ...

Ways to transmit data from autocorrect to a higher-level class

Previously, I raised a question about passing state for React via props on Stack Overflow: Laggy TextField Updates in React. I have now revamped my code using ChrisG's approach, where I store states in the parent component FormSection and pass them a ...

Execute the function upon clicking

When I click on an icon, I want it to blink. This is the code in my typescript file: onBlink() { this.action = true; setTimeout(() => { this.action = false; }, 1000) return this.action }; Here is how the action is declared in my ...

Is it possible for me to input text into html while the page is loading?

Is there a way to preload external files while a web page is loading? I'm looking to incorporate a JavaScript templating engine into my website and prefer to store my templates in separate files. Instead of loading these templates asynchronously, I&ap ...

Why aren't the correct error messages being shown when requesting from the network?

Struggling to figure out how to display informative error messages on the client side instead of generic ones? For instance, when attempting to register with an email that is already taken, users should see a message saying "<a href="/cdn-cgi/l/email-pr ...

What is the best method for binding variables in Vue.JS using moustache syntax and passing parameters to them?

If I have an HTML structure like this: <div> {{ CUSTOM_MESSAGE }} </div> And in my data: data() { return { CUSTOM_MESSAGE: 'Hey there! This message is for {{name}} from {{location}}' } } Is there a way to dynamically p ...

height detection is not functioning

Here is a straightforward script that I'm using: var element = document.getElementById("container"); if (element.clientHeight == "372") { element.style.height = "328px"; element.style.marginBottom = "44px"; } else { element.style.height = "21 ...

Developing a personalized child node on Firebase's real-time database using React Native

Seeking assistance as a newcomer to Firebase and React, I've exhausted all my options and appreciate any help you can provide. Here is the data structure I am using in Firebase: https://i.sstatic.net/fKtdK.png When adding a new Beverage type, such ...