Choosing items while using the Orthographic camera in Three Js

Currently, I am working on an isometric level and facing issues while trying to select objects within the level.

I have tried various solutions from Stackoverflow, such as this one Orthographic camera and selecting objects with raycast. However, none of them seem to be working for me. There might be a problem with my camera setup. Here is the relevant code snippet that I am using:

// setting up the camera
scope.camera = new THREE.OrthographicCamera( - scope.cameraDistance * aspect, scope.cameraDistance * aspect, scope.cameraDistance, - scope.cameraDistance, - height, 1000 );
scope.camera.position.set(0 , 0 , 0);
scope.camera.rotation.order = scope.rotationOrder; // = "YXZ"
scope.camera.rotation.y = - Math.PI / 4;
scope.camera.rotation.x = Math.atan(- 1 / Math.sqrt(2));

While looping through my matrix and adding tiles, I add them all to a new THREE.Object3D() and then add that object to the scene. My onMouseMove event function looks like this:

onMouseMove:function(event) {
    event.preventDefault();

    var scope = Game.GSThree,
        $container = $(scope.container.element),
        width = $container.width(),
        height = $container.height(),
        vector,
        ray,
        intersects;

    scope.mouse.x = (event.clientX / width) * 2 - 1;
    scope.mouse.y = - (event.clientY / height) * 2 + 1;

    vector = new THREE.Vector3(scope.mouse.x , scope.mouse.y , 0.5);
    ray = scope.projector.pickingRay(vector , scope.camera);
    intersects = ray.intersectObjects(scope.tiles.children);

    if(intersects.length) {
        console.log(intersects[0]);
    }
}

The issue I am facing is quite perplexing. The ray seems to intersect with objects even when it is not close to them and also intersects with multiple children of tiles simultaneously. Upon logging intersects.length, it sometimes returns 3, 2, or 1 object(s). Just in case it is pertinent information; the material for each object mesh consists of a new THREE.MeshFaceMaterial() with an array containing 6 new THREE.MeshBasicMaterial().

Any insights into what might be causing this?

Answer №1

It never fails to amaze me; the culprit was a simple padding-left:250px; on my container element. Once that was removed, everything fell into place. Always double-check your offsets!

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 incorporating user access control logic into a lazy-loaded Angular Monorepo application without embedding the logic in the main application

In our current project, we are developing an Angular application utilizing the Angular monorepo structure. This setup includes a parent application and several children applications, with the parent application located in the `app` folder and the children ...

The Angular custom modal service is malfunctioning as the component is not getting the necessary updates

As I develop a service in Angular to display components inside a modal, I have encountered an issue. After injecting the component content into the modal and adding it to the page's HTML, the functionality within the component seems to be affected. F ...

How to dynamically load a file based on the chosen option value in React

Two select textboxes on my page are named Choose City and Choose District. I also have some files related to cities and districts: // cities.js const cities = { "01": { "name": "City 1", "code": "01" }, ...

Utilize Javascript to convert centimeters into inches

Can anyone help me with a JavaScript function that accurately converts CM to IN? I've been using the code below: function toFeet(n) { var realFeet = ((n*0.393700) / 12); var feet = Math.floor(realFeet); var inches = Math.round(10*((realFeet ...

jQuery load function may encounter issues on Internet Explorer 9, although it functions properly on Firefox, Chrome, and Safari

Welcome to my website, built with Django. When I try to load jQuery on the top page, it doesn't seem to work on IE9 but works perfectly fine on other browsers. Do you have any suggestions to resolve this issue? Here are the related Javascript file a ...

How can I show or hide a survey pop-up depending on the URL in the address bar?

There are 2 different applications in play here. My application is built using Angular for the front end and can be accessed at http://test.com/search. It includes a JavaScript function that triggers a survey popup whenever a user visits our website. Anot ...

Adjusting the text and background hues using JavaScript results in an immediate reversal

Attempting to dynamically change the text color and background color based on user input in the textbox. While it initially works, the color changes only flash for a brief moment before reverting back. <!DOCTYPE html> <html> <head> ...

Effortlessly submit multiple forms at once with just a single click using the WordPress form

One issue I'm experiencing is that the forms on my site are generated using shortcodes. I have a suspicion that the buttons I created, which lead to submission form1 and then form2, may not be working properly because of this. This is the current set ...

Using JQuery to find the nearest element and narrowing down the search to its child elements

In my program, I have 3 forms identified by form1, form2, and form3. Each form contains its own set of dropdown lists named drop1 and drop2, along with a submit button. When the submit button is clicked in any form, it checks which option was selected from ...

Parsing dynamic parameters or slugs from the URL within the app directory using Next.js

I'm facing an issue with Dynamic Routing where I can't seem to access the parameter. Every time I try to read it, I encounter errors such as: TypeError: Cannot destructure property 'themenID' of 'router.query' as it is undef ...

The getInitialProps function in Next.js React components does not automatically bind props

When building applications with Next.js, you have the opportunity to leverage a server-side rendering lifecycle method within your React components upon initial loading. I recently attempted to implement this feature following instructions from ZEIT' ...

Numerous volume sliders catering to individual audio players

I'm currently working on implementing volume sliders for multiple audio players, similar to what you can find on . With the help of Deepak, I've managed to create code that allows me to control play/pause and adjust the volume of various audio pl ...

Contrast between employing element selector for concealment and activation tasks

I can't figure out why $('#mdiv input')[1].hide(); isn't working while $('#mdiv input')[1].click(); works perfectly fine. Firstly, I'm curious to understand why. Secondly, how can I get it to work without knowing the id ...

When executing `npm run start`, a blank page appears exclusively on the server

I recently set up a Vue landing page on my Mac. In the terminal, I navigated to the folder and executed "npm install" and "npm run dev", which ran without any issues. However, when trying to do the same on a managed server, I encountered challenges with t ...

Tips for deleting a row from a JavaScript list

Plunker I have a list from which I want to remove a row. The initial code is as follows: var newArray = _.filter($scope.componentList, function(arrayItem) { return rowId !== arrayItem.rowId; }); $scope.componentList = newArray; This filter function will ...

Difficulty in using window.scrollTo within useEffect in reactJS

I'm facing an issue where I am trying to use window.scrollTo on the initial render using the useEffect function. I have stored the vertical offset as an integer in localStorage, but the scrolling is not working and remains at position 0. Problem with ...

The Collada model in Three.js appeared completely dark until a mouse movement brought in some light

Recently, I've run into a peculiar issue while trying to display a collada model in three.js. It appears that there may be an error in the script logic, but I'm having trouble pinpointing it. The problem is that when the page first loads, the Col ...

What is the process to set up a WebSocket on Tornado for a production server rather than on a local machine?

Recently, I've delved into the world of WebSockets but have only experimented with them in a localhost environment while developing locally. Following this informative tutorial on real-time data visualization: https://medium.com/@benjaminmbrown/real-t ...

ASP.NET DIV is experiencing issues with Jquery functionality, causing it to flicker and remain fixed in place

As a beginner in JQuery, I am facing an issue with my code in asp.net. Whenever I click on linkbuttons, the behavior is not as expected. The div flickers and does not change its direction. My goal is to move the DIV left or right by 200px, but no matter wh ...

Trigger the useEffect Hook once the condition has been satisfied

Within my component, I am utilizing the useEffect hook to monitor updates of a specific value. I have implemented a condition to prevent the useEffect function from triggering after the initial rendering. However, I noticed that each time the button is c ...