The performance of aframe is being affected by the use of bounding boxes for collision

Hey there! I recently created a component to handle collision detection for primitive and non-primitive shapes. While using the bounding box collision feature provided in three.js, everything was working smoothly. However, when applying it to custom objects, it seems to be causing a slowdown in the overall user experience. Could you take a look at my component and help me pinpoint the issue?


AFRAME.registerComponent('manual-body', {
matchingElements: function(attribute){
var matchingElements = [];
var allElements = document.getElementsByTagName('*');
for (var i = 0, n = allElements.length; i < n; i++){
if (allElements[i].getAttribute(attribute) !== null){
// Element exists with attribute. Add to array.
matchingElements.push(allElements[i]);
}
}
return matchingElements;
},
tick: function(){
var elements=this.matchingElements('manual-body');
for(var i=0;i<elements.length;i++){
if(this.el.id==elements[i].id){
continue;
}
firstBB = new THREE.Box3().setFromObject(elements[i].object3D);
secondBB = new THREE.Box3().setFromObject(this.el.object3D);
var collision = firstBB.intersectsBox(secondBB);
if(collision){
this.el.emit('collision', {elSource: this.el,elTarget:elements[i]});
}
}
}
});

What do you think could be causing the lag? Is it due to the collision logic within the tick function, or is there another factor at play?

Thank you!

Answer №1

Utilizing the THREE.Box3().setFromObject method involves iterating through each vertex within a model to establish its bounding box. However, this process can be too intensive to execute during every frame in your tick function when dealing with intricate models.

To address this issue, it may be beneficial to explore the aframe-extras sphere-collider component, which offers efficient and approximate collision detection capabilities.

Answer №2

It seems like the performance slowdown on your website may be due to three.js adjusting the box boundaries for the custom model. To fix this, consider manually adding a box entity inside your model:

<a-entity collada-model="my model">
      <a-box></a-box>
</a-entity>

You can then perform collision checks with these added boxes. If you're looking to implement physics, check out Don McCurdy's Cannon.js integration, as there's no need to start from scratch!

UPDATE
If you're only encountering issues with .obj files and not with colladas, try converting the obj models to .dae format. If problems persist, give my boundary override method a shot.

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

A guide on crafting a test scenario for an AngularJS controller using the Jasmine framework

I recently created an angular module called userModule.js 'use strict'; angular.module('users', ['ngRoute','angular-growl','textAngular','ngMaterial','ngMessages','ngImgCrop', ...

Error Checking in AngularJS Form Submission

According to my form.json file, I have a form that needs validation and a simulated submission. Firstly, I need to address this issue: fnPtr is not a function Next, I want to submit the form to a mocked API endpoint that will return true or false. Can I ...

Empower the user with the ability to interact through touch on dynamically

I am trying to make a div touchable and draggable. I have dynamically created 1 to 10 divs within another div. Currently, these divs can only be dragged using the mouse or cursor. However, I want to enable dragging through touch as well. Can anyone provi ...

It appears that the flex-grow property is not functioning as expected

I am working with a parent div and two children divs underneath it. I noticed that when I set the flex-grow property of the first child to zero, its width remains constant. However, if I add text to the second child, the width of the first child decreases. ...

Leveraging the power of the map function to cycle through two arrays

Right now in React, I am utilizing array.map(function(text,index){}) to loop through an array. But, how can I iterate through two arrays simultaneously using map? UPDATE var sentenceList = sentences.map(function(text,index){ return <ListGr ...

What is the best way to retrieve the current quality label from JWPlayer using JavaScript?

My goal is to retrieve the Current Quality Label from JWPlayer 7 using JS, but instead of getting the defined labels like 360p, 480p, 720p, I'm only receiving numbers such as 1, 2, 3... This is what I've tried: playerInstance.getCurrentQuality( ...

What is the best way to utilize the Material UI theme provider in a React application that includes a mix of class components and functional components?

I have been working on a React app that utilizes class components, but I have been transitioning to using functional components instead. I have replaced some class components with functional ones in the application. However, I have encountered an issue whe ...

Reset the input field once the message has been successfully sent

My goal is to clear the form message input field after the form is sent. However, it seems to be clearing the data before it has a chance to be sent. Here is the code I'm using: <script> $(function () { $('form#SendForm'). ...

Using Javascript to Swap the Content of an Element

Recently, I've been delving into the world of javascript and have hit a roadblock. I understand how to instruct javascript to set a specific value for an ID, but now I have a new challenge: I want to create javascript code that can retrieve informati ...

Combining multiple arrays in Node.js to create a single JSON file

I'm exploring the world of nodejs and currently working on creating a Json parser that will pull data from a Json API, allow me to access specific data points (some of which will need transforming), and then save it to a Json file. I recently came ac ...

Create a layout with two rows of two buttons in HTML aligned to the right side while maintaining the left side's

I am currently working on designing a menu for my webpage that includes four buttons. My goal is to have two buttons displayed at the top of the page and two buttons displayed at the bottom. To achieve this, I have written the following CSS code: .navButt ...

Using jQuery to select and animate several elements simultaneously without repeating the animation

Issue Summary (Fiddle): I'm trying to make all elements fade out simultaneously when clicked, but the current code causes the target to trigger three times resulting in three alert() calls. I want them to fade together without using a wrapper element ...

Allow frontend JavaScript to retrieve a text file that is restricted to individual users

Trying to articulate my goal is challenging, but I'll do my best to clarify. My objective is to enable JavaScript (or an alternative solution) to retrieve data from a text file on a static site and utilize that data in some way. The challenge here is ...

Modules failing to load in the System JS framework

Encountering a puzzling issue with System JS while experimenting with Angular 2. Initially, everything runs smoothly, but at random times, System JS struggles to locate modules... An error message pops up: GET http://localhost:9000/angular2/platform/bro ...

Is it possible that activating the alert() function within a Node.js script could cause the server to freeze completely?

Given that Node.js operates on a single thread, is there a risk of freezing the entire server by calling functions like alert(), which typically wait for user input? Does Node.js have mechanisms in place to prevent such issues? ...

An effective method for retrieving textarea data in Node.js

I am facing an issue where I cannot successfully send data from a <textarea> to Node.js. It seems that the data I'm trying to send is not being received by Node.js. To retrieve data in Node.js: continueBtn.addEventListener("click", ...

The function .play() cannot be executed on document.getElementById(...) - it is not a

There is an error in the console indicating that document.getElementById(...).play is not a valid function. import React from 'react'; const musicComponent=(props)=>{ const style={background:props.color} return( <div classN ...

Problem with Node JS controller when using async/await

While working on my Node API controller, I encountered an issue with my 'error-handler' middleware related to using an asynchronous function. TypeError: fn is not a function at eval (webpack:///./app/middleware/errorHandler.js?:16:21) T ...

Route in Node.js for event binding

Currently, I am using expressjs in combination with nowjs and binding some events to the now object directly within the route when it is accessed. However, this approach feels messy and I am concerned that every time the route is accessed, the events are e ...

Running a Python script through a Javascript event

I'm looking to enhance my webpage by implementing a feature where users can generate a personalized QR code with the click of a button. My current setup involves a Python script that creates the QR code image and saves it as a .png file. I want to int ...