What is preventing my Three.js object from responding to my commands to move?

I'm currently working on a three.js project where I want to create a block that moves when different keys are pressed. I've managed to set up a functional event listener, used console.log() for checking purposes, and successfully moved the block individually. However, when I try to combine everything together, the block doesn't seem to move. I even attempted to continuously loop the code responsible for moving the block to confirm if it responds to commands, but the block still remained stationary with the __dirtyPosition set to true.

As someone with decent programming experience, I've developed multiple operational programs similar to this one in the past. Despite comparing them side by side, I can't pinpoint what exactly is causing this issue in my current project.

var block1 = new Physijs.ConvexMesh(
  new THREE.CubeGeometry(5, 5, 5),
  new THREE.MeshBasicMaterial({color: 0x444444})
);
block1.position.set(0, 0, 0);
block1.__dirtyPosition = true;
scene.add(block1);

The eventListener:

document.addEventListener("keydown", function(event) {
  var code = event.keyCode;
  //a, s, w, d respectively
  if (code === 65) block1.translateX(-3);
  if (code === 83) block1.translateY(-3);
  if (code === 87) block1.translateY(3);
  if (code === 68) block1.translateX(3);
});

And the renderer:

Top of the code:

var renderer = new THREE.CanvasRenderer();
renderer.setSize(window.innerWidth, window.innerHeight);
document.body.appendChild(renderer.domElement);

Bottom of the code:

renderer.render(scene, camera);

Is there something crucial I may have overlooked?

Answer №1

Big shoutout to @Radio for pointing out my oversight in not looping the renderer. I made a simple adjustment:

renderer.render(scene, camera);

and transformed it into:

function animate() {
  renderer.render(scene, camera);
  setTimeout(animate, 20);
}
animate();

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

Distinguishing between resolving a promise in a service/factory as opposed to in a controller within AngularJS

After experimenting with resolving a promise in both a service and a controller, I have found that I prefer to resolve it in the service so that I can reuse the variable without having to resolve it multiple times. However, I am encountering an issue where ...

What is the best way to successfully send an object through AJAX once all its updates are completed?

I am experiencing an issue with my JavaScript code within an event: var userData = tableWidget.grid('userData'); console.log(tableWidget.grid('userData')); $.ajax({ "url": "../../server/query.aspx?tableEvent=reordercolumns&tabl ...

Guide to accessing URL or parameters in the directory of a NextJs 13 application

Transitioning my getserversideprops() to next13, I am faced with the task of incorporating URL and fetching parameters from the directory structure. In my page path /posts/{postId}, how can I retrieve params or the URL? The code snippet I am currently work ...

Receiving Request URL from XMLHttpRequest in PHP

I'm currently facing a dilemma as I work on a JavaScript script that is responsible for sending data from one of my forums to the server where a PHP script runs. The goal is to have the PHP script determine which JS output should be generated based on ...

What is the process for transferring the "event" object to the functional form of "this.setState()"?

Utilizing the functional form of this.setState() where a function returning an object is passed. When attempting to bind onChange on an input and using this... onInputChange = event => { this.setState(() => ({ ...this.state, [ev ...

What is causing the sorting table to fail in React when using useState?

import React, { useState } from "react"; import "./App.css"; const App = () => { const [data, setData] = useState([ { rank: 1, name: "John", age: 29, job: "Web developer", }, { rank: 2, name: "Micha ...

Experiencing a bug in the production build of my application involving Webpack, React, postCSS, and potentially other JavaScript code not injecting correctly

I've encountered an issue with my webpack.prod.config when building my assets, which may also be related to the JS Babel configuration. While I can successfully get it to work in the development build by inline CSS, the problem arises when attempting ...

Tailwind Component Grid in Action

I'm trying to arrange a list of JavaScript components in a grid with one row and twelve columns. I want to use only six columns for the actual elements, while having three columns of padding on each side of the grid. My goal is to place three elements ...

Leveraging JavaScript variables conditionally within a JSON object

Within the code snippet below, there is a condition written as (if (epsflag==0)<?php $a=",hide:'true'";?> ). I am looking to achieve the same condition using JavaScript. Essentially, I want to conditionally utilize a JavaScript variable in ...

The value in the textarea will stay unchanged even after the form has been submitted

I recently resolved my previous issue, but now I am seeking advice on how to prevent a textarea from clearing its input after submitting a form. You can view the jsFiddle here: http://jsfiddle.net/rz4pnumy/ Should I modify the HTML form? <form id="for ...

Create your masterpiece on a rotated canvas

My goal is to draw on a canvas using the mouse, even after rotating and scaling the canvas container. The issue I am facing is that the mouse coordinates get affected by the rotation and scaling, making it difficult to draw correctly. I have tried switch ...

Issue with Angular 8: click event is not triggering when using ngFor directive to iterate through arrays of objects

Update: The original post has been modified to omit implementation details and complexity. I am facing an issue with an ngFor loop that invokes a method on a service. The method returns an array which is then iterated over by the for loop. The click even ...

Tips for enhancing the functionality of components within Vue

My expertise lies primarily in React, and I'm now exploring the Vue-centric approach to achieve the following: I want to enhance this component: , so that the label-position is set to top on mobile devices and left on desktop. However, I'm not s ...

Rendering external HTML content within a React component can be achieved by utilizing parsing techniques

In my React application, I have a component that receives HTML content as a string field in the props from an API call. My objectives are: To render this HTML content with styles applied. To parse the content and detect "accept-comments" tags within sec ...

Issue with Colorbox plugin preventing ASP.NET button from triggering postback

Currently utilizing the colorbox modal plugin (http://colorpowered.com/colorbox/) I'm facing an issue with a simple form placed in a master page - the submit button is unresponsive and doesn't trigger any action. It seems like several users are ...

An error was encountered: An identifier that was not expected was found within the AJAX call back function

I am experiencing an issue while attempting to query an API. An Uncaught SyntaxError: Unexpected identifier is being thrown on the success part of my JQuery Ajax function. $(document).ready(function(){ $('#submitYear').click(function(){ let year ...

Troubleshooting issue with jQuery datepicker not triggering onselect event

Query Function: $(function() { $("#iDate").datepicker({ dateFormat: 'dd MM yy', beforeShowDay: unavailable onSelect: function (dateText, inst) { $('#frmDate').submit(); } }); }); HTML ...

Hover over the sprites using the "spritely" plugin to see the magic unfold

I'm looking to initiate an animation when the mouse hovers over a sprite and have it stop when the mouse moves away. After exploring various solutions, I found one that seemed promising: Why does jQuery spritely animation play an extra frame on secon ...

Tips for adding and removing active class with navbar links using JavaScriptonclick

I need help with toggling the "li-active" class on my navigation bar links. Currently, when I click on a link, the "li-active" class is transferred from the previous link to the newly clicked link instead of removing it first. Can someone please assist me ...

Using an onclick function to increment and decrement values

Can anyone help me figure out how to reverse a function onclick? Here is the function: var theTotal = 0; $('button').click(function(){ theTotal = Number(theTotal) + Number($(this).val()); $('.total').text(theTotal); }); ...