Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:36:36+00:00 2026-06-05T16:36:36+00:00

I’m trying to catch a JSON object with a mouse click event. I use

  • 0

I’m trying to catch a JSON object with a mouse click event. I use ray to identify the object, but for some reason, the objects are not always identified. I suspect that it is related to the fact that I move the camera, because when I click nearby the object, i is identified.

Can you help me figure out how to set the ray correctly, in accordance with the camera move?

Here is the code :

this is the part of the mouse down event *

    document.addEventListener("mousemove", onDocumentMouseMove, false);
    document.addEventListener("mouseup", onDocumentMouseUp, false);
    document.addEventListener("mouseout", onDocumentMouseOut, false);
    mouseXOnMouseDown = event.clientX - windowHalfX;
    targetRotationOnMouseDown = targetRotation;
    var ray, intersections;
    _vector.set((event.clientX / window.innerWidth) * 2 - 1, -(event.clientY / window.innerHeight) * 2 + 1, 0);
    projector.unprojectVector(_vector, camera);
    ray = new THREE.Ray(camera.position, _vector.subSelf(camera.position).normalize());
    intersections = ray.intersectObjects(furniture);

    if (intersections.length > 0) {
        selected_block = intersections[0].object;
        _vector.set(0, 0, 0);
        selected_block.setAngularFactor(_vector);
        selected_block.setAngularVelocity(_vector);
        selected_block.setLinearFactor(_vector);
        selected_block.setLinearVelocity(_vector);
        mouse_position.copy(intersections[0].point);
        block_offset.sub(selected_block.position, mouse_position);
        intersect_plane.position.y = mouse_position.y;
    }

}

this is the part of the camera move *

camera.position.x = (Math.cos(timer) * 10);
camera.position.z = (Math.sin(timer) * 10);
camera.lookAt(scene.position); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:36:38+00:00Added an answer on June 5, 2026 at 4:36 pm

    Hmmm, It is hard to say what your problem might be without seeing some kind of demonstration of how your program is actually acting. I would suggest looking at my demo that I have been working on today. I handle my camera, controls, and rays. I am using a JSON as well.

    First you can view my demo: here to get an idea of what it is doing, what your describing sounds similar. You should be able to adapt my code if you can understand it.

    –If you would like a direct link to the source code: main.js

    I also have another you might find useful where I use rays and mouse collisions to spin a cube.
    –Source code: main.js

    Finally I’ll post the guts of my mouse events and how I handle it with the trackball camera in the first demo, hopefully some of this will lead you to a solution:

    /** Event fired when the mouse button is pressed down */
    function onDocumentMouseDown(event) {
        event.preventDefault();
    
        /** Calculate mouse position and project vector through camera and mouse3D */
        mouse3D.x = mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse3D.y = mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
        mouse3D.z = 0.5;
        projector.unprojectVector(mouse3D, camera);
    
        var ray = new THREE.Ray(camera.position, mouse3D.subSelf(camera.position).normalize());
    
        var intersects = ray.intersectObject(maskMesh);
    
        if (intersects.length > 0) {
            SELECTED = intersects[0].object;
            var intersects = ray.intersectObject(plane);
            offset.copy(intersects[0].point).subSelf(plane.position);
            killControls = true;
        }
        else if (controls.enabled == false)
            controls.enabled = true;
    }
    
    /** This event handler is only fired after the mouse down event and
        before the mouse up event and only when the mouse moves */
    function onDocumentMouseMove(event) {
        event.preventDefault();
    
        /** Calculate mouse position and project through camera and mouse3D */
        mouse3D.x = mouse2D.x = (event.clientX / window.innerWidth) * 2 - 1;
        mouse3D.y = mouse2D.y = -(event.clientY / window.innerHeight) * 2 + 1;
        mouse3D.z = 0.5;
        projector.unprojectVector(mouse3D, camera);
    
        var ray = new THREE.Ray(camera.position, mouse3D.subSelf(camera.position).normalize());
    
        if (SELECTED) {
            var intersects = ray.intersectObject(plane);
            SELECTED.position.copy(intersects[0].point.subSelf(offset));
            killControls = true;
            return;
        }
    
        var intersects = ray.intersectObject(maskMesh);
    
        if (intersects.length > 0) {
            if (INTERSECTED != intersects[0].object) {
                INTERSECTED = intersects[0].object;
                INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
                plane.position.copy(INTERSECTED.position);
            }
        }
        else {
            INTERSECTED = null;
        }
    }
    
    /** Removes event listeners when the mouse button is let go */
    function onDocumentMouseUp(event) {
        event.preventDefault();
        if (INTERSECTED) {
            plane.position.copy(INTERSECTED.position);
            SELECTED = null;
            killControls = false;
        }
    
    }
    
    /** Removes event listeners if the mouse runs off the renderer */
    function onDocumentMouseOut(event) {
        event.preventDefault();
        if (INTERSECTED) {
            plane.position.copy(INTERSECTED.position);
            SELECTED = null;
        }
    }
    

    And in order to get the desired effect shown in my first demo that I wanted, I had to add this to my animation loop in order to use the killControls flag to selectively turn on and off the trackball camera controls based on the mouse collisions:

    if (!killControls) controls.update(delta);
    else controls.enabled = false;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.