Exploring collision detection in Three.js

I'm fairly new to 3D and Threejs.

I've created a scene with a ground, where many cubes are positioned on top of it.

http://jsfiddle.net/whurp02s/1/

My goal is to select the cubes that intersect with the yellow rectangle.

To achieve this, I researched online and came across the Raycaster object along with its intersectObject function:

//**************** colision detection
    var caster = new THREE.Raycaster();
    var collisions = [];

    var rays = [
      new THREE.Vector3(0, 0, 1),
      new THREE.Vector3(1, 0, 1),
      new THREE.Vector3(1, 0, 0),
      new THREE.Vector3(1, 0, -1),
      new THREE.Vector3(0, 0, -1),
      new THREE.Vector3(-1, 0, -1),
      new THREE.Vector3(-1, 0, 0),
      new THREE.Vector3(-1, 0, 1)
    ];

    for (var i = 0; i < rays.length; i += 1) {
        caster.set(squareTL.position, rays[i]);

        for(var boxId in boxGroup) {
            var boxObj = boxGroup[boxId];

            collisions = caster.intersectObject(boxObj);

            if(collisions.length) {
                console.log(collisions);
            } else console.log("no collision");
        }

    }

However, despite implementing this code, I am not getting any collisions detected.

It seems like there is something crucial that I'm overlooking...

Answer №1

After reviewing the code on your jsfiddle, I noticed a few key points that could improve your setup:

-Make sure to populate your "boxGroup" array with another array containing the object, not just the object itself

-Consider changing the statement if (collisions.length) to (collisions.length > 0) for cases where the length is greater than one

-For smoother functionality, incorporate your collision logic into an animation or run loop

-I suggest creating a new JSFiddle specifically focused on collision work with fewer objects to simplify debugging and understanding

Best of luck, J3zusla

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

Troubleshooting Next.js Mobile Freeze Issue: Unresponsive Scroll After Page Transition

Encountered a strange bug while testing my Next.js + Bootstrap demo project on mobile view. When using the burger menu to navigate to a new page on a mobile phone, attempting to scroll down causes it to stick/freeze/hang inexplicably. Despite my efforts to ...

Navigating errors during the distribution of numerous messages with SendGrid and Node.js

I have developed a command line application that interacts with a DynamoDB table to extract email addresses for items that have not yet received an email. The process involves creating customized message objects, sending emails using SendGrid's sgMail ...

I'm facing a frustrating issue where using NodeJS with the Passport local strategy is resulting

I am experiencing a severe headache when attempting to log in using Passport. When I make a post request to /login with an email and password, Passport successfully authenticates it without any errors. However, the return res.redirect('/user') fu ...

Tips for implementing self-managed state in Vue.js data object

My approach in organizing my Vue application involves using classes to encapsulate data, manage their own state (edited, deleted, etc), and synchronize with the back-end system. However, this method seems to conflict with Vue in some respects. To illustra ...

Integrate predictive text suggestions in JavaServer Pages for efficient form filling

After some research, I have managed to solve the issue I was facing. On my jsp page, I have three text boxes. When I enter data into the first text box, it triggers a call to get.jsp to fetch data from the database and populate the second text box. However ...

Dynamic manipulation of lighting effects in three.js during runtime

Is it possible to dynamically add and remove various light types in three.js during runtime? I have checkboxes that correspond to different types of lights, and I would like to add a specific light type to the scene when its checkbox is checked, and remov ...

Retrieving information within the iteration

I am facing an issue with connecting to an external server named Pexels in order to retrieve photos from node.js. The problem seems to be related to JavaScript, as Pexels limits the user to download up to 40 pictures per page. https://api.pexels.com/v1/cu ...

Using a JavaScript loop to modify the color of the final character in a word

I am curious to find out how I can dynamically change the color of the last character of each word within a <p> tag using a Javascript loop. For example, I would like to alter the color of the "n" in "John", the "s" in "Jacques", the "r" in "Peter" ...

Identify the opening of the console for the background page of a Chrome

Is it possible to detect when I click on the "Background Page" for my test plugin on the "chrome://extensions/" page? This question has been boggling my mind. Currently, whenever I open the background page, the console remains undocked. After reading a po ...

Problem with CSS3 Target Selector

As I delve into the world of JS/HTML5/CSS3, I have been immersing myself in code examples from three different authors and combining and modifying them to enhance my learning. You can find my work-in-progress Pen, with references to the original authors, r ...

karma: Error: Unable to access the 'prototype' property of an undefined object

Whenever I attempt to inject my service, the following error occurs: (function () { describe('Test', function () { var $scope, $state, service,ctrl; beforeEach(function() { module('moduleApp'); ...

Is it possible to invoke a Java function from a text box on an HTML page?

For a web project using JSP, MySQL, AJAX with Netbeans and MySQL, I have three textboxes. Two textboxes are for user input, and the third should display the product of the two input values. How can I achieve this? Should I make an AJAX call or can I call ...

Utilizing a class instance as a static property - a step-by-step guide

In my code, I am trying to establish a static property for a class called OuterClass. This static property should hold an instance of another class named InnerClass. The InnerClass definition consists of a property and a function as shown below: // InnerC ...

implementing a JavaScript function and declaring a variable from an HTML source

On my webpage, I have a feature that gathers a large amount of data using jQuery. My goal is to limit the number of results displayed by changing the shown results dynamically to create a false-page effect. This functionality is all handled through a singl ...

Running queries in a loop is not functional within node-firebird

When running multiple select queries in a loop, I encountered an issue that puzzled me. For simplicity, let's take a look at the code snippet below: var Firebird = require('node-firebird'); Firebird.attach({database: 'TEST'}, (e ...

Retrieve the value of a field from a Formik form integrated with Material UI

I've been working on a way to disable a checkbox group depending on the selected value of a radio group. I took inspiration from the technique outlined in the final section of the Formik tutorial. Using React context has definitely helped clean up the ...

What steps can be taken to address the error "console is undefined" in PowerShell?

I have a basic .js file saved on one of my drives that contains the following code: var x=3; function numSqaure(x) { return(x*x); } var sentence="The square of" + x + "is" + numSqaure(x); console.log(sentence); When attempting to run this script thro ...

Tips for importing JSON data from a file into a jQuery variable?

I have a JSON document that contains: { "data": [{ "red": "#f00", "green": "#0f0", "blue": "#00f", "cyan": "#0ff", "magenta": "#f0f", "yellow": "#ff0", "black": "#000" }] } My goal is to ret ...

Why is my Bootstrap Carousel Switching Images but Refusing to Slide?

I've encountered a problem with my bootstrap carousel. The slide effect doesn't seem to be working, even though I have added the "slide" CSS tag. Instead of sliding, the images just quickly change without any transition. Here are some key points ...

The Angular boilerplate and Twitter Bootstrap 3 make for a powerful combination in

I've been attempting to integrate Twitter Bootstrap 3 into this project found at https://github.com/ngbp/ngbp, but I haven't been able to find any information on how to do so. Could it be that: It's simply not possible It's not recomm ...