What causes the world to lose its definition when included as a parameter in a constructor function?

I'm attempting to utilize threejs and cannonjs in my project, but I'm encountering the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'copy')
   at World.add.World.addBody (cannon.js:12985:23)
   at new BothObject (objects.js:15:16)
   at new Cube (objects.js:33:5)
   at script.js:7:15

The code snippet causing the issue is shown below:

objects.js

class BothObject{
  constructor(shape,geometry,mass,color,scene,world){
    this.scene=scene
    this.world=world
    
    this.physics = new CANNON.Body({
       mass: mass, // kg
    });
    this.physics.shape=shape
    this.geometry = geometry;
    this.material = new THREE.MeshPhongMaterial( { color: color});
    this.mesh = new THREE.Mesh(this.geometry,this.material)
    console.log(world)
    
    this.scene.add(this.mesh)
    this.world.addBody(this.physics)
  }

  
  update(){
    this.mesh.position.copy(this.physics.position)
    this.mesh.quaternion.copy(this.physics.quaternion)
  }
  
}

class Sphere extends BothObject{
  constructor(radius,mass,color,scene,world){
    super(new CANNON.Sphere(radius/2),new THREE.SphereGeometry(radius),mass,color,scene,world)
  }
}

class Cube extends BothObject{
  constructor(width,height,depth,mass,color,scene,world){
    super(new CANNON.Box(new CANNON.Vec3(width/2,height/2,depth/2)),new THREE.BoxGeometry(width,height,depth),mass,color,scene,world)
  }
}

script.js:

console.log("start")
const scene = new THREE.Scene();
scene.background = new THREE.Color( 'skyblue' );
var world = new CANNON.World();
world.gravity.set(0,0,-9.82);
var gground = new Cube(100,100,0.1,"green",scene,world)//it's gground because I have another variable named ground

(script.js is executed after objects.js)

(neither are run as modules)

When I log world in script.js, it appears to be functioning properly. However, when I log it in Cube or BothObject, it returns as undefined.

Answer №1

There appears to be an issue with the number of arguments provided

(width, height, depth, mass, color, scene, world) // Definition of Cube constructor
(100, 100, 0.1, "green", scene, world) // Creating a new instance of Cube

Utilize typescript for error detection in situations like this

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

Exploring the intricacies of nested directories with Nuxt 3 and Vite

I'm looking to implement nested directory functionality in Nuxt 3 beta with Vite. In Nuxt 2, I successfully used the following configuration in (nuxt.config.js): components: [ { path: '~/components', // will get any components nested in l ...

Passing PHP values to JavaScript and then to AJAX requires using the appropriate syntax and techniques for

Currently, I am populating a table with data retrieved from my database. Here is a snippet of the code: <?php //mysqli_num_rows function while($row=mysqli_fetch_array //I know this may be wrong, but that's not the point echo "<tr><t ...

The Bootstrap dropdown menu experiences a flickering issue when the user quickly hovers over it

I currently have a bootstrap menu implemented on my website. When the user hovers over any item in the navbar, a dropdown is displayed, and within that dropdown, there are sub-items as well: https://i.sstatic.net/6XdgR.png As shown in the image. <l ...

What steps can be taken to prevent a Javascript Timeout Exception when attempting to launch a new browser window?

Recently, I have encountered an issue while running my test cases on a Linux server. Specifically, when trying to open a new window using Robot Framework, I consistently receive a Timeout Exception. This problem seems to be isolated to the server environm ...

Creating personalized headers for WebSocket in JavaScript

I am currently in search of a straightforward method to utilize WebSocket with custom headers for a web application, utilizing PHP as the backend and js+vuejs for the frontend. My application needs to establish a connection with a WebSocket server based o ...

Upcoming API and backend developments

When working with the NEXT project, API Routes provide the ability to create an API endpoint within a Next.js application. This can be achieved by creating a function in the pages/api directory following this format: // req = HTTP incoming message, res = H ...

The function's name has been obscured by the name of its parameter

Caution: ECMAScript 5 (ES5) strictly prohibits the use of arguments.callee(). To avoid this, either name function expressions or opt for a function declaration that calls itself. [MDN] How can we refer to the o function within itself in this scenario? fun ...

Ways to refresh the appearance of clothing in Three.js

Currently, I am diving into the world of Three.js. My latest project involves adapting a shader that I originally created in Shader Toy to be displayed on my own webpage. The shader itself is a procedural terrain where users can navigate using the WASD key ...

Using the JavaScript selectionchange event to target a specific element exclusively

I am looking for a way to incorporate a JavaScript selectionchange event on a specific div element. The goal is to display a highlighter box when the user selects text from the DOM. I was able to achieve this functionality for web using an onmouseup event, ...

What is the purpose of using JSON.parse(decodeURIComponent(staticString))?

A specific approach is utilized by some dynamic web frameworks in the following code snippet <script> appSettings = JSON.parse( decodeURIComponent( "%7B%22setting1%22%3A%22foo%22%2C%22setting2%22%3A123%7D")); </script> Is there a part ...

Mongoose stores data in two separate collections

Hey everyone, I could really use some assistance with this issue that has been bothering me for the past few days. model/user.js var UserSchema = mongoose.Schema({ username:{ type: String, unique: true, index:true }, password:{ type:Strin ...

Is it possible to target elements based on a specific CSS3 property they use?

Is there a method to target all elements in the DOM with a border-radius other than 0? If anyone has suggestions, it would be greatly appreciated! ...

How to utilize jQuery to eliminate HTML onchange code

This block of code features an HTML onchange attribute as well as the use of jQuery's change event on an input text. I want only the jQuery change event to take precedence. I have attempted to use both unbind and off without success. Can someone prov ...

Tips for refreshing a TinyMCE instance after modifying its settings

Is there a method in TinyMCE to refresh the instance when the options are updated with { language: "fr" }? How can I apply this new option or reload the TinyMCE instance? I am looking to dynamically change the language using Javascript. ...

Managing various swipe interactions using HTML and JavaScript/jQuery

I'm in the process of creating a mobile multiplayer game using HTML5 and Javascript. The jQuery Plugin "touchwipe" is utilized to manage swipe events in various divs like this: $('#play1').touchwipe({ wipeLeft: function(){ if ...

Vue: need to import a package: unable to set a value to a constant property 'exports' of the object '#<Object>'

After installing the resource-bundle package in my project, I attempted to use it in my main.js file: /*main.js*/ var co=require('co'); var loader = require('resource-bundle'); co(function*(){ ... ... }).catch(onerror); Upon exa ...

Utilizing CSS Media Queries to Activate DOM Alterations

Utilizing CSS media queries, I am able to display different layouts of my website based on the width of the screen. This involves showing and hiding specific elements accordingly. However, in addition to this, I also need to rearrange elements within the ...

The Redux store has been modified, yet the changes are not reflected in the

In my Posts.js component, I am mapping every object in the posts array. Within this function, I attempt to filter all notes that have the same post_id as the id of the current mapped post object and store them in a variable called filteredNotes. I then pas ...

the JavaScript anchor feature is malfunctioning

Steps to Play Back: To start, in the header section, select any of the links with anchors: ##bankaccount #pack #platform #acq ##scorecard ##intrade #form Next, scroll up to the top of the page Then, reload the page Actual Outcome: Upon reloading a page w ...

Implementing jQuery to easily upload and display images

Discover a handy jQuery script that enables you to preview an image before uploading it. <!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <scrip ...