Using Three.js to position objects along a curve and rotate them toward the center of a circle

I'm working on developing a sleek VR menu that presents menu items in a curved column below the camera as the user looks down. The challenge is to have the items rotated towards the camera while appearing uniform.

Here's what I've come up with so far https://i.sstatic.net/HmJkG.png

For a live example, you can check out this CodePen demo http://codepen.io/bknill/pen/BLOwLj?editors=0010

I found some code that helps calculate the position of the menu items

   var radius = 60; // radius of the circle
  var height = 60,
      angle = 0,
      step = (Math.PI /2 ) /  menuItems.length;

menuItems.forEach(function(item,index){

    var menuItem = createMenuItem(item.title);
       menuItem.position.y = - Math.round(height/2 + radius * Math.sin(angle));
       menuItem.position.z =  Math.round(height/2 + radius * Math.sin(angle));
      // menuItem.rotation.x =  -Math.round(Math.PI * Math.sin(angle));
    angle += step;
    menu.add(menuItem);

})

While this setup is almost there, the next step is to make sure the items rotate uniformly towards the camera. Simply using menuItem.lookAt(camera.position) doesn't produce the desired effect.

Instead, child.lookAt(camera.position.normalize()) achieves the desired rotation https://i.sstatic.net/L1O72.png

If anyone can enlighten me on the mathematical approach needed to ensure the items face the camera and maintain their curved appearance, please do share your insights.

Answer №1

If you want one object to face another, the easiest method is to use the lookAt function.

It's important to note that this function requires a direction vector, not the specific point you want the object to look at.

(desired position to look at - current position of your object).normalize();

In this scenario:

var dirToLookAt = new THREE.Vector3();
dirToLookAt.subVectors(menuItem.position, camera.position);
// or: dirToLookAt.subVectors(camera.position, menuItem.position);
dirToLookAt.normalize();

This process is based on the Vector3 documentation.

To delve deeper into this topic, check out the latest discussion related to this method.

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

Using form.submit() alongside preventDefault() will not yield the desired outcome

My login form needs to either close the lightbox or update the error box based on the success of the login attempt. I suspect the issue lies with the onclick="formhash(this.form, this.form.password);" which is a JavaScript function that hashes a passwor ...

Maximizing the potential of NestJS apps with Docker

I am working on a NestJS project that consists of multiple apps structured as follows: my-project: -- apps; --- app-one ---- src ---- tsconfig.app.json --- app-two ---- src ---- tsconfig.app.json -- libs -- package.json -- etc... Within this project, I ha ...

Error: The function _this[("render" + data.type)] is not defined as a valid function

https://i.stack.imgur.com/Y5Dz5.jpg I encountered an error in the console that I can't seem to figure out. Despite following the Syncfusion library's documentation, the error persists. Here is the code snippet that I implemented: import React f ...

Having trouble retrieving dynamic data from a JSON array

I am working with a JSON array. JSON.stringify(ar) When I attempt to display the results in an alert: alert(JSON.stringify(ar)); The output displayed in the alert is: [{"url":"link1","title":"title1"}] However, when I try to transfer the contents i ...

Checking Ember.js - How to retrieve the status of each checkbox individually

I am currently working on an older version (1.11) of an Ember app and I have multiple checkboxes generated within a loop. I am looking to determine whether an element has been checked or unchecked in an action. Ember ehbs: {{#each item in data}} <lab ...

Is there a way to exclude the element from being displayed when using ngIf in AngularJS?

Is there a way in Angular to conditionally add an element to the DOM without having it always added, even when its evaluation is false? I am looking for an alternative method to ngIf. ...

ng-select will solely output the term 'collection'

i am having an issue with a ng-select in my contact form. everything is being received correctly except for the value of the ng-select. Instead of getting the selected option from the ng-select, the system just returns the word "array". Below is the port ...

What is the best way to retrieve a linked filter in Vue from another?

I have created a file to store filters linked to my Vue object, and it is being imported into my App.js. One of the filters I have needs to use another filter: Vue.filter('formatDateTime', value => { if (value) return moment(String(value ...

I am tasked with creating an onScroll function that identifies the largest HTML element visible on the screen by utilizing the IDS contained within an array

I am looking to create an onScroll function that can identify the largest HTML element currently visible on the screen, based on the IDs stored in an array. For example, if "section1" takes up more space on the screen than "section2" as I scroll down the ...

Creating a responsive layout that sets the window height to 100% and automatically adjusts sibling divs when the content within parent divs exceeds the defined height

<section> <div class="section-wrap"> <h1>page1</h1> </div> </section> <section> <div class="section-wrap"> <h1>page2</h1> </div> </section> My attempt at impleme ...

Error in React: Attempting to access the first index of an undefined property

I'm trying to develop a function that calculates the total of individual columns in a matrix of 12 rows and then creates a new array with those total values. However, I keep running into the following error: TypeError: Cannot read property '0&ap ...

"Utilizing multiple optional parameters in Expressjs can lead to the request handler being

Hey there, I could use a fresh perspective on this issue I'm having. I am attempting to set up a request handler that can accept 0, 1, or 2 parameters like http://localhost:3000/{seed}/{size}. The parameters "seed" and "size" should both be optional. ...

Stop the occurrence of OpenCPU javascript error pop-up notifications

I'm currently experiencing an error related to CORs during a test deployment of OpenCPU. While I may create a separate question for this issue in the future, for now, I am wondering if it is possible for the deployment to fail without alerting the end ...

Current recommended method for updating innerHTML with properly formatted text?

My understanding is that using innerHTML on an element can be risky, as malicious users could potentially inject harmful content such as <script> tags. This question shows there are multiple alternative tags, some of which are non-standard. In my s ...

Having issues with the environmental value not functioning properly within the .js file for Next.js

Currently, I am in the process of testing an app that features a widget which relies on an API key within a script. This script needs to be placed within my <head> tags. My .js file is located in my public folder and is being imported into my layout. ...

What do you prefer: defining properties with the JSON object or with objectName.property in JavaScript

Can you tell me which approach is considered the best practice? Is it better to use the "this" statement in the following way: var obj = { x: 20, y: 10, width: this.x, height: this.y, render: function () { // renders object on canvas ctx.fi ...

The bar chart in chartjs is not displaying properly due to incorrect grouping

I attempted to generate a multi bar chart using Chart.js, but encountered an issue where the jobType and jobCount were not displayed correctly based on each companyName. Below is the table: https://i.sstatic.net/ZyKZH.png Here is the PHP file (CompanySel ...

Ways to Export HTML to Document without any borders or colorful text

How can I make a contentEditable area visible when filling text and then disappear when exporting the document? I found a script online that allows you to do this, but the issue is that the contentEditable area is not visible until clicked on. To address t ...

My code seems to be malfunctioning

I'm attempting to conceal the chatname div. Once hidden, I want to position the chatid at the bottom, which is why the value 64px is utilized. However, I encountered an error stating The function toggle3(); has not been defined. <div id="chat ...

Display additional content button, dynamic div identification

I've created a small script that includes a "show more" button at the end of displaying 10 entries. Here is the code: <div id="more<?=$lastid;?>"> <a onclick="showmore(<?=$lastid;?>);">More</a> </div> And here ...