What is the process of rotating a vector by 90 degrees to align it with a perpendicular plane, and then adjusting it further by 15 degrees out of

My ultimate objective is to determine a vector that represents the direction of the green line depicted in the image below, based solely on the positions of the yellow and green dots.
To clarify, the angle of the vector can vary as long as its endpoint lies somewhere on the green-blue surface of the cylinder. Hence, there is 360° freedom around the cylinder, with a limitation of approximately 15° at the edges. The cylinder is positioned perpendicular to the line connecting the yellow and green dots. The length of the vector is insignificant, only the direction matters. My primary challenge lies in figuring out how to derive a vector perpendicular to the one from the Yellow to the green dot. PS: None of these elements are aligned along the x, y, z axis. The grid is not based on the xyz coordinates, but is provided for visualization purposes. https://i.sstatic.net/4OFVc.png

Answer №1

Provided here is a code snippet that calculates a vector starting from a specified point in a perpendicular direction to the vector between two given points:

function perpendicularVector(pointStart, pointEnd, theta){
    let vDiff = new THREE.Vector3(0, 0, 0)
        .subVectors(pointEnd, pointStart) 
        .normalize()
  
    let V = new THREE.Vector3(
        vDiff.y + vDiff.x * vDiff.z,
        vDiff.y * vDiff.z - vDiff.x,
        -(vDiff.x * vDiff.x) - vDiff.y * vDiff.y
    )

    return
        V   .applyAxisAngle(vDiff, theta)
            .applyAxisAngle( new THREE.Vector3().multiplyVectors(V, vDiff).normalize(), 15*Math.PI/180 )
}

For illustration purposes, a basic demonstration of the functionality of the above code is provided below:

(Use the mouse to zoom, rotate, and pan in the rendered output after running the snippet)

body {
  font-family: sans-serif;
  margin: 0;
  background-color: #e2cba9;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

canvas {
  width: 100%;
  height: 100%;
}
<div id="app"></div>

<script type="module">
import { OrbitControls } from "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="26524e5443436616081714170817">[email protected]</a>/examples/jsm/controls/OrbitControls.js";

import * as THREE from "https://cdn.jsdelivr.net/npm/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a5e42584f4f6a1a041b181b041b">[email protected]</a>/build/three.module.js";

var scene = new THREE.Scene, theta = 0;
let point1 = new THREE.Vector3(4, 2, 1),
  point2 = new THREE.Vector3(0, 3, 3);

// Rest of the code provided for visualization and demonstration

...

// End of demonstration code snippet for showcasing the functionality

</script>

Answer №2

Utilizing mathematical calculations, achieving this is entirely feasible. The concept you are referring to is that of "Orthogonal vectors", which denotes vectors that are perpendicular to one another. The radius of the cylinder is perpendicular to the line connecting the blue and yellow points.

Nonetheless, given that you are already leveraging Three.js, you can delegate the complex tasks to it by using an Object3D.

// Defining vectorA (center, green)
const vecA = new THREE.Vector3(xA, yA, zA);

// Defining vectorB (destination, yellow)
const vecB = new THREE.Vector3(xB, yB, zB);

// Creating a helper object
const helper = new THREE.Object3D();

// Positioning the helper at vecA
helper.position.copy(vecA);

// Orienting the helper towards vecB
helper.lookAt(vecB);

// Moving the helper perpendicularly along its own y-axis
const cylinderRadius = 27;
helper.translateY(cylinderRadius);

// The final position is now determined!
console.log(helper.position);

In the illustration below, the helper Object3D is represented as a red line solely to provide a visualization of its rotation and position, however, in actuality, it remains invisible unless a Mesh is added to it.

https://i.sstatic.net/jRxgE.jpg

If you wish to adjust by adding or subtracting 15 degrees from the perpendicular, you can rotate the helper along its own x-axis prior to using translateY()

const xAngle = THREE.MathUtils.degToRad(15);
helper.rotateX(xAngle);

const cylinderRadius = 27;
helper.translateY(cylinderRadius);

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

Guide to retrieving PDFs and images from a Spring Application as an API response and manipulating the data using JS/React

For my current project, I am working on a Spring Boot and React application where I need to create an API that takes the file name as input and returns the file content in Java/Spring Boot. The goal is to display the content in a new browser tab. Below is ...

Is there a way to identify when a radio button's value has changed without having to submit the form again?

Within this form, I have 2 radio buttons: <form action='' method='post' onsubmit='return checkForm(this, event)'> <input type = 'radio' name='allow' value='allow' checked>Allow ...

Is there a way to modify the material of individual objects in THREE.js without creating separate Meshes?

We have developed a unique PCB Viewer using THREE.js, but now we want to enhance it with selection functionality. While the task itself isn't complex and I have managed to make it work, I am encountering performance challenges. Our goal is to allow us ...

Obtain the value stored in the session

How can I hide a form based on a specific session being set? Here is my approach: <form action="" method="post" <?php if ((isset($_SESSION['start']) )|| (isset($_SESSION['visitor']) )){ echo 'style="display:none;"'; } ? ...

Raspberry Pi encountering a TypeError with Node.js async parallel: "task is not a function" error

I am new to nodejs, so I kindly ask for your understanding if my questions seem simple. I am attempting to use nodejs on a Raspberry Pi 3 to control two motors, and I keep encountering the error message "async task is not a function." Despite searching fo ...

Navigate through intricate nested JSON array structures in JavaScript

nested json structure Json Tree Structure: { "id": "30080", "dataelements": { "Name": "abc" }, "children": [ { "id": "33024", "dataelements": { "Name": "a" }, "children": [ { "id": "33024", ...

Utilizing a function within the App.js file located in the public folder using React JS

I need to execute a function called callMe that is defined in src/App.js from the public folder. In App.js import messaging from './firebase-init'; import './App.css'; function App () { function callMe() { console.log('Call m ...

AngularJS: Advanced Routing for Dynamic Web Applications

Hello, I am currently exploring the possibility of implementing something similar to this code snippet using AngularJS: $routeProvider .when('/root/:controllerName/blah/:blahId/blah/:blah', { templateUrl: '/tmpl/:controllerName ...

What is the best way to modify a CSS property using logical operators in JQuery?

If an element with the class ".has_padding" does not have any text content, it should be set to "display:none;". However, those elements with text inside should remain visible. Below is some code where I have styled the elements to demonstrate the issue. ...

Use jQuery's change method to initiate a hidden file input

Want to create a fake file input using an anchor tag and trigger the hidden file input with jQuery? Looking for some advice on how to make this happen. Check out my current attempt here. I'm not sure if I'm on the right track with this, so any g ...

Creating a repository of essential functions in AngularJSDiscover the steps to set up a

I am looking to create a set of reusable functions in AngularJS for CRUD operations that can be used across multiple entities in my project. I have already set up a factory using $resource for server communication, which looks like this: Model File: var ...

A bounding box aligned with the camera's perspective

One issue I am grappling with is determining the camera's position, considering a given lookAt vector when the camera is not aligned with the z-axis. The goal is to ensure that the camera captures all objects within its field of view and aspect ratio. ...

Clicking to enter fullscreen mode on a website will result in the Fullscreen API automatically closing

For my current project, I am creating an offline website and would like it to display in full screen when opened. I have been using the Fullscreen API, but it exits fullscreen mode when a user navigates to another page. After researching the issue, it seem ...

The Vue template is not able to recognize the Pug language syntax within the .vue file

According to the Vue documentation: Template processing differs from other webpack loaders, as pug-loader and similar template loaders return a function instead of compiled HTML. Instead of using pug-loader, opting for original pug is recommended. Test ...

Stop users from being able to copy text on their smartphones' internet browsers

I am currently working on creating a competitive typing speed challenge using JavaScript. Participants are required to type all the words they see from a div into a textarea. In order to prevent cheating, such as copying the words directly from the div, o ...

Is there a way to temporarily toggle classes with jQuery?

Incorporating ZeroClipboard, I have implemented the following code to alter the text and class of my 'copy to clipboard button' by modifying the innerHTML. Upon clicking, this triggers a smooth class transition animation. client.on( "complete", ...

What is the best way to transition an absolute positioned element from right to center?

When hovering over an overlay element, I want the <h3> tag to appear with a transition effect from right to center, similar to the example shown here. Could someone please assist me in achieving this? Thank you in advance. HTML <div class="row m ...

What could be the reason for the lack of controller updates despite changes made to the service

Could someone please help me solve the issue with my code? I expected that after clicking the button, the text would be updated. However, it seems to not be working as intended. Any assistance you can provide would be greatly appreciated. main.js x = a ...

JQuery clockpicker failing to fire change event for input field

While working on a codebase, I encountered a callback binding scenario where a specific action needs to take place whenever any input is altered. $(document.body).on('change', '.input-sm', function (){ ... }) The challenge arises whe ...

The application is functional, however, the initial controller within is experiencing difficulties

Setting up a controller in my application based on a tutorial. The first two divs are displaying correctly but the ContraAss controller is not rendering and only shows {{info}}. What am I missing here? (NB. Probably something simple, but with limited exper ...