The position of THREE.js intersects[0].object always seems to default to (0,0,0)

While using THREE.js to display objects on my page, I encountered an issue with clicking objects. Regardless of which object I click, the intersects[0].object.position always shows as x=0, y=0, z=0, even though the actual positions of the objects are different.

Could you please take a look at the code below and provide some insight into what might be causing this problem?

function onDocumentMouseDown(event) {
  event.preventDefault();
  var vector = new THREE.Vector3((event.clientX / window.innerWidth)*2-1, -(event.clientY / window.innerHeight)*2+1, 0.5);
  projector.unprojectVector(vector, camera);
  var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
  var intersects = ray.intersectObjects(teeth, true);
  if (intersects.length > 0) {
    //not working
    camera.position.x=intersects[0].object.position.x;
    //not working
    camera.position.y=intersects[0].object.postion.y;
    //working
    intersects[0].object.material.color.setHex(Math.random()*0xffffff);
  }
}

Answer №1

the element's location: intersects[0].element.location

the point of intersection: intersects[0].point

function handleMouseClick(event) {
  event.preventDefault();
  var vector = new THREE.Vector3((event.clientX / window.innerWidth)*2-1, -(event.clientY / window.innerHeight)*2+1, 0.5);
  projector.unprojectVector(vector, camera);
  var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
  var intersects = ray.intersectObjects(teeth, true);
  if (intersects.length > 0) {
    //not functioning correctly
    camera.position.x=intersects[0].point.x;
    //not functioning correctly
    camera.position.y=intersects[0].point.y;
    //working as expected
    intersects[0].object.material.color.setHex(Math.random()*0xffffff);
  }
}

Answer №2

In my case, I encountered a similar issue that was quickly resolved by passing a reference to the parent object where the intersection occurred instead of the actual object itself: intersects[0].object.parent

function onDocumentMouseDown(event) {
  event.preventDefault();
  var vector = new THREE.Vector3((event.clientX / window.innerWidth)*2-1, -(event.clientY / window.innerHeight)*2+1, 0.5);
  projector.unprojectVector(vector, camera);
  var ray = new THREE.Ray(camera.position, vector.subSelf(camera.position).normalize());
  var intersects = ray.intersectObjects(teeth, true);
  if (intersects.length > 0) {

    camera.position.x=intersects[0].object.parent.position.x;

    camera.position.y=intersects[0].object.parent.postion.y;

    intersects[0].object.material.color.setHex(Math.random()*0xffffff);
  }
}

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

Display all items on page load using ng-repeat checkboxes in AngularJS filter

I have encountered a problem with filtering checkboxes in my code. I want all products to load on the page initially, and then when I check multiple checkboxes within technologyArray and/or technologyArray2, the products that match the checkbox name should ...

The functionality of the Firebase Realtime Database has ceased

After successfully developing an app with Firebase's Real-time Database for the past month, I suddenly encountered an issue today. Despite the authentication features working seamlessly, my app is no longer able to retrieve data from Firebase. It&apos ...

Issue with 'for' loop iteration over object array

Can someone help me understand why I am getting the unexpected result "6 You have not watched undefined undefined" in the browser console when running this simple code? var films = [{ title: "The Mummy", hasWatched: true, stars: "5 stars" ...

Steps to trigger a mutation in the store every time the route is modified

Is it possible to trigger a mutation in the store every time the route changes, without relying on a Vuex plugin to detect route/ mutations? This scenario involves using vuex, nuxt, vue-router, and vue-router-sync. Clarification: I am attempting to commi ...

When the click event is triggered, the second function guess() does not execute

I am currently developing a number guessing application. It consists of two functions, the first one called startGame() works correctly (it receives the maximum number and then disappears by adding the hidden class). However, the second function that is ...

Combine an array nested within an object with each key of the object

Alright, let's dive into the structure of these objects: custom_fields:{ 21:{ edit:true required:true show:true } } In my Angular controller, this object is stored under $scope.page.custom_fields. Within this object, there is another ...

Gain entry to Zurb Foundation for Apps modules within your AngularJS application

Currently, I am developing an AngularJS application utilizing Foundation for Apps. One key element in my layout is a Foundation Apps panel that serves as the top menu. <div zf-panel="" id="topMenu" position="top" class="panel-fixed">...</div> ...

attach an event listener for the click event

I have a button set up like this Inside index.html: <body> <section> <button id="open-file">Open File</button> ...(series of other similar buttons)... </section> </body> <script> requir ...

The intricacies of JavaScript recursion explained thoroughly

I'm struggling to grasp how this recursion will function. Specifically, I can't seem to comprehend how the final console ('end'--) is being executed. Any guidance on the execution aspect would be greatly appreciated as I am having troub ...

Error: You are not able to utilize an import statement outside of a module when utilizing the `fast-image-zoom` feature

Currently, I am attempting to integrate fast-image-zoom into my React/Next.js application and unfortunately encountering an error message that reads: SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:353:18) ...

Tips for displaying only one image when clicked and hiding other divs:

Currently tackling a project that involves JavaScript and I've hit a roadblock. Before you dive into the text, take a look at the image. Check out the picture here. I have successfully created a slider, but now I want to implement a feature where cli ...

Instructions for iterating through a response object with a nested array

This is the response I received. {status: 'Success', length: 7, data: {…}} An expanded version of the response: data: tours: (7) [{…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]]: Object length: 7 status: "Success" I a ...

Is it possible to utilize WebRTC (simple-peer) with STUN without the need for additional signaling?

I am currently exploring the utilization of the simple-peer library to create browser-to-browser WebRTC connections through data channels. My understanding, although it may be flawed, is that for two browsers to establish a connection via WebRTC, they need ...

Creating a unique Web Component that spans the full height of the page

I created a Web Component with Vue.js and vue-custom-element. I now want my my-chat and my-whiteboard Web Components to have a height of 100%. Here's how I'm using the component: // App.vue <template> <splitpanes class="default-theme" ...

Is it possible to sort an array by both date and time simultaneously?

As I work on developing a schedule app, it is necessary to sort items by both date and time simultaneously. In the current setup, the filtering only considers hours and minutes which is functional, but there is a need to also include dates in the sorting p ...

javascript implement a process to iteratively submit a form using ajax

I have a dynamic form with an unknown number of input fields that are not fixed. While searching for solutions, I came across jQuery ajax form submission which requires manually constructing the query string. In this scenario, the number of input fields ...

Place the React rendering inside a class element, rather than an ID

For my project, I need to display the same information in multiple locations using react. Instead of targeting elements by id (as shown in the code below), I would like to use class selectors. ReactDOM.render(<App />, document.getElementById('a ...

The `arrangeComplete` event in jQuery Isotope is failing to trigger

Here is my jQuery code utilizing the Isotope plugin: $(document).ready(function() { /** * Set up Isotope */ var $grid = $('.grid').isotope({ "itemSelector": ".grid-item", layoutMode: 'masonry', ...

Can flexbox elements be animated as they scroll?

Wondering if it's feasible to animate flex elements upwards when scrolled? Attempting to replicate this effect: https://codepen.io/Sergiop79/pen/bxjGEe I want to apply this to the elements below (styled in flexbox), either the entire "row" or each i ...

How can the value of a button be displayed in an input box using an onclick function?

When the button '1' is clicked, it displays the value "1" inside a <!p> tag. However, the second button '2' does not display the value "2" in the input box. Even though both functions are identical. //Function with working func ...