Having difficulty rotating cube as intended using the plane and mousemove technique

I am trying to achieve a rotation effect where the cube stays flat but rotates towards the cursor in a top-down 2D fashion. I suspect that the issue might be related to how the mesh is centered against itself. Can anyone offer some assistance with this?

For reference, you can view the project here:

The code snippet I am working on can be found here: https://github.com/the-coop/coopwebsite/blob/89ca8909ed3fe28afd79b34c5305b63aabba8638/lib/conquest/ground/engine/setupGroundMovement.js#L35

Below is an excerpt from the code where I have been experimenting with different approaches in order to make the cube rotate correctly:

    const plane = new Plane(new Vector3(0, 1, 0), 1);
    const raycaster = new Raycaster();
    const mouse = new Vector2();
    const pointOfIntersection = new Vector3();


    document.addEventListener('mousemove', ev => {
        const { camera, me } = window.GROUND_LEVEL;

        mouse.x = ( ev.clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( ev.clientY / window.innerHeight ) * 2 + 1;

        raycaster.setFromCamera(mouse, camera);
        raycaster.ray.intersectPlane(plane, pointOfIntersection);
        
        if (me.mesh) {
            // Attempting to rotate the mesh to target position.
            me.mesh.geometry.lookAt(pointOfIntersection);
        }
    });

Answer №1

pointOfIntersection is a vector with dimensions aligned to the cube's three axes.

To limit the cube's rotation to one axis, you can set the other axes to 0 in the Object3D#lookAt method.

// Rotate the cube around its Y axis if tracking a point on the X axis
me.mesh.geometry.lookAt(pointOfIntersection.x, 0, 0);

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 on displaying a div element using jQuery

What is required: I simply need to display a div. Code snippet: <a href="javascript:void(0);" id="viewdetail" onclick="$(this).show();" style="color:green">View Detail <div class="speakers dis-non"> </div> </a> After ...

Toggle the display of input fields based on radio button selection in Angular 2

We want to dynamically display additional input fields based on the selected radio button. Radio <div class="form-group"> <label>What would you like to evaluate?</label> <div class="radio"> <label> ...

Is it feasible to alter cookie data within a jQuery ajax call?

I'm currently developing a Chrome extension that enables users to capture all HTTP requests for a specific website, edit components of the request, and then send it again. My plan is to utilize jQuery's ajax function to design and dispatch the a ...

Convert a file into an empty array when sending a post request from Angular to Laravel

I am currently working on a simple Laravel 5 post request that aims to achieve the following tasks: Save some data to a database. Store a file (image) in public storage with a name consisting of 20 random numbers followed by '.jpg'. Save a URL ...

Guide to displaying a task within a table cell after a user submits the form

<?php $servername = "localhost"; $username = "u749668864_PandaHost"; $password = "Effy1234"; $dbname = "u749668864_PandaHost"; $q = $_REQUEST["task"]; $task = $q; echo $task; $conn->close(); ?> Exploring the world of ajax has left me scratching ...

Swap out the content in a text input box with the text chosen from a suggested autocomplete option

I am working on a text input box with auto-complete options displayed below it. I want to enable users to navigate through the options using keyboard arrows and "select" one, causing it to change color. How can I update the text in the input box with the s ...

Setting up a new event handler for an ng-repeat element

I am completely new to Angular! I want to implement a simple event in a form element created by ng-repeat with a specific value. Here is the HTML code: <div class="labels"> <div class="checkbox-element" ng-repeat="suggestN ...

Arrange search results using $in array parameter in MongoDB

My challenge involves managing two collections: users and items. Within the user.profile.savedItems array, items are saved in the following format: {"itemId" : "yHud5CWpdPaEc6bdc", "added" : ISODate("2014-09-12T22:28:11.738Z")} My goal is to retrieve ...

What is the process for passing data to an HTML element using Express and NodeJs?

Is there a way to transfer data from a web or local file to an HTML element using Node.js/Express? Here is a snippet of what I am trying to achieve: Retrieve data from a source, parse it, and then send it to a specific element in index.html app.get("/", ( ...

Every time I attempt to insert a background image into a div using jQuery, I am consistently faced with a 404 error message

When I hit enter in my search bar, a new div is created each time. However, I am struggling to assign a background image to the created div as I keep receiving a 404 error in the console. Below is the code snippet I'm working with: function appendToD ...

Creating an identifier for the jQuery slideToggle function involves assigning a unique class or ID to

I am currently working on creating an identifier for the jQuery slide.Toggle function. In my PHP code, I have a loop that prints values, each with a button and a div that should be able to slide toggle. So far in PHP, here is what I have attempted, withou ...

Executing a program on a website that records every single keystroke made while the webpage is active

Just to clarify, I am not developing a keylogger. I am working on a website for a video game where users can earn items by completing specific in-game actions, like typing something in the chat. Given the nature of this game, I believe the most effective ...

The message that keeps popping up says "TypeError: Unable to access the 'execute' property of an undefined object."

I am currently working on creating a Discord bot and encountering an error that says "TypeError: Cannot read property 'execute' undefined". Despite trying multiple solutions, I am still facing some issues with my code. Any help in solving this pr ...

How can I store items in a JavaScript array so that they are accessible on a different page?

I'm running into an issue with my PHP website. Each item has a "request a quote" button that, when clicked, is supposed to add the item name to an array using JavaScript. This array should then be added to the "message" field on the contact.php form. ...

How can I locate the initial link with a certain class using jQuery?

If there is a link somewhere within the tree structure as shown below: <div id="foo"> <div> <div> <a href="asdf.com">link</a> <a href="#bar" class="specialLink">link</a> <a href="#bar2" ...

Troubleshooting Vue.js 2: Difficulty with Vue locating files stored in the /assets directory (v-for loop)

My Vue-cli 3 project with Webpack has the following folder structure: /public /src /assets p1.jpg p2.jpg App.vue main.js I have read that in order for Webpack to recognize the /assets directory, require() should be used in JavaScript files ...

AngularJS toaster doesn't seem to be appearing

I have integrated the angularjs toaster library into my project. Here are the necessary references: https://github.com/jirikavi/AngularJS-Toaster Added the following references for the library: <link rel="stylesheet" href="bower_components/Angu ...

What's preventing me from changing module.exports? Why is my browser suddenly giving me errors after `npm run build` ran smoothly?

How come the browser is preventing me from executing the following code: Getters.js let Getters = {} Getters.foo = function(){ return 1; } Getters.bar = function(){ return 2; } module.exports = Getters; However, after running npm run build and serve -s ...

What is the process for implementing a CSS theme switch in a CHM file and ensuring that it remains persistent?

Welcome to my tech world Greetings! I am a tech writer with a side interest in scripting JavaScript/jQuery for our help file (CHM file). While I consider myself a beginner in JS scripting, I have ventured into the realm of dynamic theme swapping within CH ...

React with Typescript - Type discrepancies found in Third Party Library

Recently, I encountered a scenario where I had a third-party library exporting a React Component in a certain way: // Code from the third party library that I cannot alter export default class MyIcon extends React.Component { ... }; MyIcon.propTypes = { ...