The error message from the Three.js program states: "Attempting to access property '0' of an undefined value is not possible."

To create a captivating animation with quaternion data, the following code snippet is utilized:

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth/window.innerHeight, 0.1, 1000 );

var renderer = new THREE.WebGLRenderer({antialias: true, alpha: true});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

var geometry = new THREE.BoxGeometry( 1, 0.3, 2 );
var material = new THREE.MeshBasicMaterial( { color: 0xf4b942 } );
var obj = new THREE.Mesh( geometry, material );
scene.add( obj );

var geometry = new THREE.BoxBufferGeometry(1, 0.3, 2 );
var edges = new THREE.EdgesGeometry(geometry);
var lines = new THREE.LineSegments(edges, new THREE.LineBasicMaterial({ color: 0x000000 }));
scene.add( lines );

camera.position.z = 5;
renderer.render(scene,camera);
var xpos_o = obj.rotation.x;
var ypos_o = obj.rotation.y;
var zpos_o = obj.rotation.z;
var xpos_lo = lines.rotation.x;
var ypos_lo = lines.rotation.y;
var zpos_lo = lines.rotation.z;

let i = 0; let m = 0;
var animate = function (quat_data) {
    requestAnimationFrame( animate );

    if (m % 5 == 0 || m == 0) {
        if (m !== 0) {i++;}

        if (i >= quat_data.length){
            obj.rotation.x = xpos_o;
            obj.rotation.y = ypos_o;
            obj.rotation.z = zpos_o;
            lines.rotation.x = xpos_lo;
            lines.rotation.y = ypos_lo;
            lines.rotation.z = zpos_lo;
            i=0;
        }

        let quat = quat_data[i]; 

        var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]);
        obj.applyQuaternion(quaternion);
        lines.applyQuaternion(quaternion);

        renderer.render( scene, camera );    
    }

    m++;    
}

var animate1 = function() {

    var quat_data1 = [  [0.681492531,-0.014533572,0.711492547,-0.170668778  ], 
            [-0.81103207,0.125979118,0.555748976,-0.132257921  ],
            [-0.459080697,-0.267181198,-0.846533742,0.035103342  ],
            [0.874097952,-0.166578614,-0.244525976,0.385231457  ],
            ...

    animate(quat_data1);
}

var animate2 = function() {

    var quat_data2 = [[-0.395306087,-0.567475272,-0.168275478,-0.702416024  ], 
            [0.591221013,-0.226393713,-0.208636898,0.745435603  ],
            [-0.279990579,0.754847831,0.56444738,-0.182233852  ],
            [-0.419803039,-0.623439075,-0.538698243,-0.380648559  ],
            [0.878036311,0.088829796,-0.024437397,0.469642749  ],
            ...

    animate(quat_data2);
}

var animate3 = function() {

    var quat_data3 = [ [-0.17958507,0.232682609,-0.217296778,-0.930800793  ], 
            [-0.178598829,0.228014038,-0.2182292,-0.931927075  ],
            [-0.177601473,0.223351625,-0.219152185,-0.933029522  ],
            [-0.176574343,0.218685883,-0.220066047,-0.934113976  ],
            [-0.175533915,0.214015872,-0.22097227,-0.935177153  ],
            [-0.174483815,0.209350901,-0.221866773,-0.936217247  ],
            [-0.173407996,0.204681945,-0.222753878,-0.937238325  ],
            ...

    animate(quat_data3);
}

To save space, some quaternion data values have been replaced with ellipses. The initial section sets up the scene geometry while the animate function processes an array of quaternions for animation. With each iteration, m increments by 1, triggering the animation code every 5th frame to achieve a frame rate of 12 fps instead of the default 60 fps of requestAnimationFrame. The animation adjusts the object's rotation based on the quaternion data. If the index i surpasses the array's length, it resets to the beginning for continuous animation.

Additional functions like animate1, animate2, and animate3 each utilize distinct sets of quaternion data for the main animate function. However, an error is encountered when triggering the animate1 function from an HTML button, specifically at the line:

var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]);

Answer №1

The problem arises due to incorrect execution of the animation (requestAnimationFrame) with an improper callback target. The callback function is only passed a single argument, which is the time value.

var animate = function (quat_data) {
    requestAnimationFrame( animate );

    // [...]

    let quat = quat_data[i]; 
    var quaternion = new THREE.Quaternion(quat[0],quat[1],quat[2],quat[3]);

    // [...]
}

What was originally expected was for the input to the function animate to be an array of animation data, but it turned out to be just a single time value.

The correct approach is as follows:

var animate = function (quat_data) {
    requestAnimationFrame( function() { animate(quat_data); } );

    // [...]
}

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

Tips for sending data from Ajax to another function

Can you assist me in understanding how to retrieve values from an ajax function and then use them in a different function? Here is an example: function getlanlon(){ $.ajax({ type: "GET", url: "{{URL:: ...

"JavaScript throws an 'Undefined' error when trying to access a basic

I am struggling with a basic set of HTML and JS files that are not functioning properly, and I can't seem to pinpoint the issue: <html> <head> <script type="text/javascript" src="data.json"></script> < ...

JavaScript: Remove duplicates and count the number of unique properties in an array

Arrays have always been a challenging aspect for me, so I would really appreciate any assistance. ...

Attempting to change the background color of a table row when hovering in React, but experiencing no success

Describing the appearance of my table row: <tr onMouseEnter={() => {this.buttonIsHovered = true} } onMouseLeave={() => {this.buttonIsHovered = false}} className={this.buttonIsHovered ? 'hover' : null}> The initial value ...

Techniques for transferring PHP print_r arrays to Javascript

Array ( [0] => Array ( [sno] => 1 [name] => Sivamani [contact] => 750241378 [$city] => Madurai ) ) Array ( [1] => Array ( [sno] => 2 ...

Exploring the versatility of JSON for data manipulation in JavaScript as akin to working with a relational SQL

Looking at this JSON variable: var peopleList = { "1": {"Name": "Lisa", "item1": "Name of Item 1"} , "2": {"Name": "Marty"} , "3": {"Name": "Jordan", "item1":"Name of Item 1", "item2":"Name of Item 2"} } This structure seems similar to ...

Tips for avoiding problems with quoting and using apostrophes in a JavaScript function inside a tag in a JSP file

Within my JSP, I have a string value stored in ${state.status.code} that I need to pass to a JavaScript function when a table element is clicked using onClick to trigger the showStatus function. Here is how I have attempted to achieve this: <c:set var= ...

What is the method for subtracting pixels from a percentage value?

Is there a way to accomplish a similar effect to this: height: calc(100% - 50px); height: -webkit-calc(100% - 50px); height: -moz-calc(100% - 50px); In browsers that do not support these properties? I have a responsive div with two children. The height ...

Combining two HTML tables using jQuery/JavaScript

My table is displayed below: <table id="first" class="merge"> <tr> <td>Mick Jagger</td> <td>30</td> <td>50</td> <td>10</td> </t ...

Node is currently posing a challenge for the installation of packages

I am currently facing an issue while setting up my raspberry pi 3. I am attempting to install and execute a node code, but I encountered a problem during the installation of packages using npm. After trying multiple times with different versions of node ( ...

Ensure that an input field on the PHP form is required

Currently working on a form with multiple input fields. I'm wondering if there's a way to prevent the form from being submitted to the server if a specific field is left empty. I'd like to use a JavaScript pop-up box to notify the user inst ...

Guide to embedding bootstrap.min.js into ember-bootstrap extension

I'm currently experiencing an issue with the ember-bootstrap plugin. Following the instructions on their setup page at , I downloaded and installed it. Once I began working on the Navbar, I encountered a problem. The Hamburger menu, which is supposed ...

Showing JSON data in a popup and returning an ActionResult

I have created a custom controller in MVC with the following ActionResult: [HttpPost] public ActionResult Details([DataSourceRequest]DataSourceRequest command, int id) { //var userDetail = _CustomerDetail.GetAllCustomers(); var g ...

Ajax displays JSON data within an array format

Looking for a solution where I have MySQL data displayed in a table, with each row having a button. When the button is clicked, a bootstrap modal should appear containing that data. I have converted the MySQL data to JSON format and assigned the ".view_dat ...

Locating the source and reason behind the [object ErrorEvent] being triggered

I'm facing an issue where one of my tests is failing and the log is not providing any useful information, apart from indicating which test failed... LoginComponent should display username & password error message and not call login when passed no ...

The Angular method for retrieving the child's ID when it is clicked

As a newcomer to Angular 1.0 with a background in jQuery, I am facing the following scenario: Let's imagine we have the following HTML structure : <div id="filters"> <div id="filter1">Filter 1</div> <div id="filter2"> ...

When the web driver fails to function as expected

After installing the selenium-webdriver via npm, I downloaded the IE component from this link and added it to my path on Windows 8. Upon opening IE, I had to set all security zones to high, ensuring they were consistent. However, due to restrictions in th ...

getting information from component in NextJS

Apologies if this question is too basic. I recently started my journey into learning React and NextJS. I am working on a simple application that fetches data and displays it on the Home page. In my component file, I have two functions. This component file ...

Trigger Chrome Extension Background Page Using a Message

Currently, I am in the process of developing my initial chrome extension and focusing on establishing message passing and background.js functionality. However, it seems like my background.js file is not being triggered or receiving any messages. I am seeki ...

Inquiry regarding delays in updating and retrieving data with Mongoose in Node.js

I recently faced an issue related to the CRUD development process. Whenever I update a property and check the response in Postman, it always shows the previous data. For instance, after clicking send on Postman, it shows "weeks" : "11" instead of "10". Che ...