Find the coordinate of a point in a three-dimensional space that lies at a given distance between two specified points

In order to achieve a smooth camera movement in three.js, it is essential to compute the position of point C. This calculation will ensure that the camera can smoothly transition from point A to a specific distance away at point B.

Answer №1

It is quite simple to achieve this with the help of three.js methods.

If we assume that variables a, b, and c represent instances of THREE.Vector3(),


a.set( 2, 1, 4 );
b.set( 9, 4, 2 );

c.subVectors( a, b ).setLength( dist ).add( b );

This code snippet uses three.js version r.91

Answer №2

If you are looking to determine the coordinates of point C, which is positioned on the line connecting points B and A at a specified distance from B, the process can be accomplished through the following simple steps:

  • Find the vector from point B to point A (essentially, this would be A - B).
  • Normalize the vector (adjust its length to 1 unit).
  • Multiply the normalized vector by the desired distance.
  • Add this updated vector to point B.

To illustrate this concept, here is a basic example using JavaScript:

const A = [2, 1, 4];
const B = [9, 4, 2];

const dist = 3;

function addVectors(v1, v2) {
    return v1.map((x, i) => x + v2[i]);
}

function scalarMultiply(v, c) {
    return v.map(x => x*c);
}

function getLength(v) {
    return Math.hypot(...v);
}

const vecB2A = addVectors(A, scalarMultiply(B, -1)); // Step 1
const normB2A = scalarMultiply(vecB2A, 1/getLength(vecB2A)); // Step 2
const distB2A = scalarMultiply(normB2A, dist); // Step 3
const C = addVectors(B, distB2A); // Final step

console.log(C);

Answer №3

To find point C, simply subtract a certain distance in the direction of vector AB from point B. It's a straightforward process:

Calculate the vector v from A to B as (xB-xA, yB-yA, zB-zA) divided by the distance between A and B.

Next, determine C by subtracting d times v from B, where d represents how far away from B you want C to be located.

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

The React-Chartjs chart is displaying without any color rendering

I attempted to create a radar chart using react-chartjs library, but I am facing an issue where the colors are not appearing when it renders. https://i.stack.imgur.com/tjAsW.png What could be causing this problem? I followed a significant portion of the ...

Axios continues to encounter the issue of downloading a faulty PDF file

Every time I attempt to download a PDF file to the client side using axios, it ends up corrupted. In my MERN stack webapp, a user can click a button to initiate the download of a PDF file. This button triggers an axios GET request to the Node.js web server ...

Invert the normals in Three.JS post geometry flip

I recently came across a solution on stackoverflow that helped me mirror my geometry successfully. However, I noticed that after mirroring it, my geometry turned black. Is there a way to flip the normals at the same time as mirroring the geometry in order ...

React Native's fetch function appears to be non-responsive

I am experiencing an issue where the fetch function does not seem to fire in my React Native component: import { Button } from 'react-native'; export function Test() { function submit() { console.log('submit'); fetch('h ...

The Vue.js application is experiencing issues with displaying Google Maps functionalities

I have developed an application using Vue.js in Monaca and Cordova with onsenUI. My goal is to display my location on a Google map within the page. I attempted to achieve this by utilizing the npm package named vue2-google-maps, but unfortunately, it' ...

Does the image alter based on the selection in the dropdown menu?

When I utilize the Templating Example from the select2 library, everything seems to work for the first dropdown value change. The correct image is previewed as expected. However, when trying to change the value a second time, a second image is appended but ...

Conceal YouTube upon opening any model or light box

I have a YouTube video in the center of my page, and when I click on any link from the side navigation, a lightbox appears. However, in IE, the lightbox is behind the YouTube video. I have tried setting the Z-index, but it doesn't seem to work. Is the ...

Unexpected issue encountered for identifiers beginning with a numerical digit

Is it possible to select an element from a page with an id that begins with a number? $('#3|assets_main|ast_module|start-iso-date') Upon attempting to do so, the following error occurs: Uncaught Error: Syntax error, unrecognized expression: ...

How to hide an item in Ionic 2 using navparams

Is there a way to dynamically hide or show a list item on page load depending on certain parameters? Here is an example of the code I am currently using: HTML <button ion-item (tap)="goToPage2()" [hidden]="shouldHide">Page 2</button> TS ex ...

Three: Achieving smooth edges for a unique design

I've designed a unique shape Is there a way to add rounded edges for a more organic appearance? Subdivision doesn't seem like the right solution as it alters the geometry. ...

What steps should I take to implement asynchronous functionality in my code for a Google Chrome Extension?

I am currently working on a Google Chrome extension that needs to gather data from two different servers and send it to another service. I am facing an issue with making the process asynchronous, even though the requests are functioning properly. After se ...

Locating specific text within an <li> element using the provided value

So, I have this set of words: <li>name</li> <li>myname</li> <li>yourname</li> Then there's also an input box input type="text" value="name" id="the_value" with the value "name", along with a submit button identif ...

Using app.get('env') in local development and live production settings

var express = require('express'); var app = express(); var db_url; if(app.get('env') == "development"){ db_url = 'mongodb://127.0.0.1:27017/localhost'; }else{ db_url = 'something else'; } console.log(app.get(&apos ...

Combining photos seamlessly and bringing them to life through animation upon window loading

My main goal is to seamlessly fit the images together, but I'm struggling to achieve this. I tried using masonry, but unfortunately it didn't work for me. All I want is to tightly pack the divs together. For instance, in my fiddle example, I woul ...

Guide on Postman: Tracking the number of occurrences for a particular object in a JSON reply

I am just starting out with JSON and Postman. I have a simple task in mind. I have set up a GET request that will return a JSON response like the one below. In this example, I want to extract the count of all "IsArchived" attributes in the response. The ...

Displaying Date in Angular 2 Application with Proper Formatting

I'm currently working on formatting the date pipe in my Angular application to display correctly when used within an input template. Originally, without the date formatting, my code looked like this: <input class="app-input" [readonly]="!hasAdminA ...

Is there a way to execute this process twice without duplicating the code and manually adjusting the variables?

I'm looking to modify this code so that it can be used for multiple calendars simultaneously. For example, I want something similar to the following: Here is what I currently have: This is my JavaScript code: var Calendar = { start: function () ...

Navigate to items within a content block with a set height

I have a <div> that has a fixed height and overflow-y: scroll. Inside this div, there is mostly a <p> tag containing a long text with some highlighting (spans with background-color and a numbered id attribute). Just to note, this is an HTML5 a ...

What are some recommended methods in Angular for developing reusable panels with both controller and view templates?

I am still getting acquainted with angularjs, so there might be something I'm overlooking, but I'm struggling to find an efficient way to create reusable views that can be instantiated within a parent view. My specific scenario involves a web ap ...

Tips for creating an Ionic app in Production mode, the Ionic build process may exhibit peculiar behavior

I am in the process of preparing my Ionic application for production. After executing the ionic build --prod command, the application builds successfully. However, upon running it on the server, I encounter errors related to: cordova.js, main.js, Vendor.j ...