Spinning in a circular motion around the twelve-sided shape

I am currently working on a Three.js project involving a dodecahedron. My goal is to have the camera zoomed in on one side of the dodecahedron and upon pressing a button, I want it to zoom out, rotate to face another side, and then zoom back in.

To clarify:

Imagine the camera is focused on side 1 and when I press "5", I expect the camera to zoom out, rotate towards side 5, and then zoom back in. It's crucial that the camera remains parallel to the base of the pentagon it's facing, maintaining the correct perspective throughout the movement. https://i.sstatic.net/qt4sd.jpg

Initially, I decided to start with a cube instead of diving into something more complex. I experimented with tweens (triggered by pressing 'G') to demonstrate basic movements, but the results weren't satisfactory in the fiddle link provided. jsfiddle

In an attempt to streamline the process, I began documenting the positions and rotations of each side view of the cube to identify any patterns. Although I noticed some recurring values, I struggled to translate them into a functional algorithm, especially for a dodecahedron. The recorded values are as follows:

side1 (0, 0,  600) (0, 0, 0)
side2 (600, 0,  0) (0, pi/2, 0)
side3 (0, 0, -600) (0, pi, 0);
side4 (-600, 0, 0) (0, -pi/2, 0);
side5 (0, 600,  0) (-pi/2, 0, 0);
side6 (0, -600, 0) (pi/2, 0, 0);

Although I can discern some patterns and relationships among these values, I'm unsure how to incorporate them into a cohesive function. This step is essential before attempting the same approach with a more intricate shape like a dodecahedron. Any advice or guidance on where to focus my efforts next would be highly appreciated. While using multiple if clauses might seem feasible, I believe there's a better solution waiting to be discovered.

Answer №1

In order to tackle the issue at hand, our initial step involves determining the central coordinates of each facet of the dodecahedron.

Within the realm of three.js, a mesh comprises of triangles, with each triangle consisting of three points. The faces and vertices can all be located within mesh.geometry.faces and mesh.geometry.vertices. In the case of a dodecahedron, every side is made up of three faces and five vertices. By employing the normal of each face, we classify them into 12 groups based on shared normals and coplanar positioning. Subsequently, by calculating the average coordinates of the five points on each side, we arrive at the central coordinate.

Upon ascertaining these coordinates, our next course of action pertains to rotation. While one option entails rotating the dodecahedron while maintaining the camera's position, I opt for the alternative approach of fixing the dodecahedron and translating the camera instead. https://i.sstatic.net/yOuT1.png

The camera now gazes towards centerA, with the green circle marking its trajectory. This is necessitated by the need to preserve the distance between the camera and the dodecahedron's center.

To determine the target position, we simply scale the vector from centerB. As this vector's coordinates are in the object system, it becomes imperative to apply a matrix transformation to align them with the world system.

Subsequently, through an animated translation, the camera embarks on a curved path. Leveraging the parametric equation of a circle in 3D space facilitates this maneuver. For additional insights regarding this equation, refer to Parametric Equation of a Circle in 3D Space. Utilizing this formula, parameters θ1 for the camera position and θ2 for the target position are derived. The camera's position is duly updated using θ1 within each subsequent animation frame.

I have included explanatory notes on jsfiddle. (Although some bugs persist, updates are forthcoming.)

A different approach, elaborated upon in this post, involves maintaining the camera's perspective while rotating the object.

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

Converting from Async.js parallel to Bluebird: A Step-by-Step Guide

Utilizing async.js allows me to define promises with handlers, providing polymorphism and separating results. Can this be achieved in bluebird as well? async.parallel({ cityPromises: (cb)=>{ City.find({ ...

JavaScript framework that is easily customizable to include support for XmlHttpRequest.onprogress, even if it needs to be emulated

Which JavaScript library or framework offers support for the "onprogress" event for XmlHttpRequest, even if it needs to be emulated using a plugin or extension? Alternatively, which JavaScript framework is the most straightforward to extend in order to add ...

Problem: Vue 3 build does not generate service worker

After adding the "@vue/cli-plugin-pwa": "^4.5.12" to my package.json and setting up workbox configuration in my vue.config.js, I encountered an issue. When running npm run build:prd with the script vue-cli-service build --mode prod.exam ...

Rendering nested routes in Vue router with the parent component

I attempted to nest my routes using Vue router, but encountered some difficulties { path: '/admin', name: 'Admin', component: () => import('pages/Admin'), children:[ { path: 'stock', name: ' ...

Leverage the selected values to dynamically generate a table using jQuery

Can someone help me figure out how to store multiple values from a selection and then create an array using jQuery? Here's the scenario: In my multiple selection, I have 5 values: <select name="animal" id="animal" multiple="multiple">    ...

What is the best way to initialize a JavaScript constructor in AngularJS, similar to using ng-init to define variables but calling the constructor in service.js?

I need help converting this code to AngularJS. The current code is in core JavaScript and I want to call a core JavaScript constructor from AngularJS. I have successfully defined how to call the service constructor in core, but I am unsure of how to call i ...

Reconfigure a portion of a string using Vue's dynamic replacement feature

Currently tackling a problem. In my possession is a string that essentially consists of HTML code: let htmlTitle = "<a href="/news/sky-sport-hd-in-italia-dal-18-novembr">Sky Sport HD in italia dal 18 novembre</a> | <a href="/news/ecco-il-g ...

Adding Data to a Database Using Ajax with PHP

I am trying to use Ajax to insert data into a database, but I keep encountering this error: Uncaught ReferenceError: insertData is not defined at HTMLInputElement.onclick (Modal.php:105) Error screenshot: https://i.sstatic.net/AmXka.jpg The HTML and ...

What is the best way to handle waiting for a React context provider that requires time to initialize the value it provides?

In my Next.js application, I have a global context that takes five seconds to compute the value provided: import React, { useContext, useEffect, useState } from 'react'; const GlobalContext = React.createContext(); export const GlobalContextPro ...

Emphasizing sections using a specific class for paragraph highlighting

Is it possible to dynamically change the style of paragraphs based on certain classes? Let's say we have a text with a list of p elements and we want to modify the styles of paragraphs that come after specific classes, such as 'alert' or &ap ...

How can you utilize Angular Signals in combination with HostBinding to dynamically update styles?

Within a component called app-test, the following code is present: @Input({ transform: booleanAttribute }) reverse: boolean = false; @HostBinding('style.flex-direction') direction: string = this.reverse ? 'column-reverse' : &ap ...

Enclose Angular $resource requests that do not return POST data

Currently, I am working on enhancing my $resource requests by implementing a straightforward wrapper. The primary objective is to incorporate some logic before the request is actually sent. For guidance, I referred to an informative article authored by Nil ...

Using Jquery to detect if there are any Space characters in the user input

In my form, users are required to set up a new Username. The problem arises when they include a space in their username, which I want to prevent. Currently, I am able to detect the presence of a space with this code: var hasSpace = $('#usernameValue ...

Using Typescript to establish the dependency of one parameter's type on another parameter

Let's imagine a scenario where interface Action<T> { assignAction(key: keyof T, value: any): void; } The type T is defined as { users: User[]; accounts: Account[]; } Now, when using the assignAction method, if we attempt to pass in ...

Utilizing ChartJS to convert a chart into a Base64 image within a Vue environment

I am currently utilizing the PrimeVue chart component, which is built on top of ChartJS. The configuration is very similar. The documentation indicates that I need to instantiate a new Chart() and then call toBase64Image(); https://i.sstatic.net/NMHjV.p ...

Encountering a 404 error in Codeigniter when making an AJAX call

After successfully implementing an upload form with ajax, I encountered some issues when attempting to delete uploaded photos. Initially, I received a "csrf protection error," which led me to disable csrf protection, only to then encounter a "404 not found ...

Warnings about index out of range may be thrown when using Three.js immediateRenderCallback with MeshShaderMaterial

I am encountering a situation in my Three.js scene where custom objects with an immediateRenderCallback method are causing warnings when I try to use a custom shader material called MeshShaderMaterial(myShader). Normal materials work fine on my custom imm ...

I'm having trouble using Discord.js to set up a custom role with specialized permissions for muting users

module.exports = { name: "mute", description: "This command is used to mute members in a server", execute: async function (msg, arg) { const muteRole = await msg.guild.roles.cache.find((r) => r.name == "Mute ...

Can you explain the differences between offsetHeight, clientHeight, and scrollHeight for me?

Have you ever wondered about the distinction between offsetHeight, clientHeight, and scrollHeight? What about offsetWidth, clientWidth, and scrollWidth? Understanding these differences is crucial for working effectively on the client side. Without this kn ...

Unable to download and install jspdf version 1.5.3

Currently, I am facing an issue where I need to convert HTML to PDF using jspdf 1.5.2. However, I am encountering an error that says "Cannot read property 'charAt' of undefined" when trying to utilize html2canvas. When attempting to upgrade to j ...