Creating a Cubic Bezier Curve connecting two points within a 3D sphere using three.js

I'm currently working on a project where the user can click on two points on a sphere and I want to connect these points with a line along the surface of the sphere, following the great circle path. I have managed to obtain the coordinates of the selected points and draw a QuadraticBezierCurve3 between them, but now I need to use a CubicBezierCurve3. The challenge is that I am not sure how to determine the two control points needed for this.

My main struggle is that most resources I find deal with circular arcs and only [x,y] coordinates, while I am working in 3D space with [x,y,z]. I have explored various links such as this one that helped me create a semi-functional solution using QuadraticBezierCurve3. Other sources include math/code references like this, this, and this, but I am unsure which approach to apply. I also came across suggestions involving tangents, their intersections, and midpoints, but again, implementing these concepts in 3D space poses challenges due to tangent directions possibly being varied (i.e., forming a plane).

An example snippet of my code: http://jsfiddle.net/GhB82/

To render the line, I am using:

function drawLine(point) {
   var middle = [(pointA['x'] + pointB['x']) / 2, (pointA['y'] + pointB['y']) / 2, (pointA['z'] + pointB['z']) / 2];
   var curve = new THREE.QuadraticBezierCurve3(new THREE.Vector3(pointA['x'], pointA['y'], pointA['z']), new THREE.Vector3(middle[0], middle[1], middle[2]), new THREE.Vector3(pointB['x'], pointB['y'], pointB['z']));
   var path = new THREE.CurvePath();
   path.add(curve);
   var curveMaterial = new THREE.LineBasicMaterial({
      color: 0xFF0000
   });
   curvedLine = new THREE.Line(path.createPointsGeometry(20), curveMaterial);
   scene.add(curvedLine);
}

In the above function, pointA and pointB are arrays containing the [x,y,z] coordinates of the selected points on the sphere. I need assistance transitioning from using QuadraticBezierCurve3 to CubicBezierCurve3, particularly in determining the required control points.

Answer №1

If you're interested in fitting cubic curves to circular arcs, I have some information that may be helpful over at . The process for the 3D case is similar - you need to determine the circular cross-section that your two points form on the sphere, and then create the cubic Bezier section along that circle.

One downside is that if your arc is greater than a quarter circle, one curve may not be enough. You may need two or more curves to approximate the arc accurately. Bezier curves cannot perfectly model true circular curves, so using cubic instead of quadratic allows for a better approximation over longer segments.

Alternatively, if there is an arc command available, it might be more efficient to use that rather than creating your own method (and if three.js doesn't support arcs, consider submitting a feature request).

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

JavaScript: Responding to the fetch response based on certain conditions

I am currently working with a fetch() request that can either return a zip file (blob) or a JSON object in case of an error. The existing code successfully handles the zip file by sending it to the user's Downloads folder. However, when a JSON respons ...

VueJS - The application is unable to find the designated route

I've encountered an issue with the Signin page in my project. Despite having all other pages functioning properly, the Signin page doesn't seem to render anything when I navigate to it (http://localhost:8080/#/signin). import Vue from 'vu ...

Troubleshooting Recursive Logic Problem with hasOwnProperty in JavaScript and JSON

JSON data with a specific problem highlighted by the comment // doesn't get parsed currently: var oarsObject = [{ "coordinateReferenceSystem": "26782,15851 <-- not in a value", "positionReferenceType": "geogWgs84", "geogWgs84": ...

Exploring the methods for monitoring multiple UDP ports on a single address in Node.js within a single process

I am currently working on developing a Node.js application to manage a small drone. The SDK provides the following instructions: To establish a connection between the Tello and a PC, Mac, or mobile device, use Wi-Fi. Sending Commands & Receiving Responses ...

Prevent JavaScript Errors when performing a page postback

I am currently using the Telerik RAD Editor control in my ASP.NET 4.0 application, which is outside of any update panel. On the same page, I have multiple ModalPopUps and update panels to prevent unexpected full postbacks. The page contains PageMethods inv ...

Develop a custom directive that incorporates ng-model and features its own distinct scope

UPDATE - I have generated a Plunker I am in the process of developing a personalized directive to be utilized for all input fields. Each input will have distinct options based on the logged-in user's requirements (mandatory, concealed, etc), so I bel ...

Mastering the art of transforming JSON data for crafting an exquisite D3 area chart

I often find myself struggling with data manipulation before using D3 for existing models. My current challenge is figuring out the most efficient way to manipulate data in order to create a basic D3 area chart with a time-based x-axis. Initially, I have a ...

Creating crisp and clear text within a three.js texture

I am currently incorporating a 512x512 SVG onto a circular plane in my project using the following code snippet: const texture = new THREE.TextureLoader().load('img/plane.svg'); ​ const material = new THREE.MeshBasicMaterial({ ...

The CSS selectors wrapped in jQuery vary between Chrome and Internet Explorer 11

When utilizing a jquery wrapped css selector such as $($("#selector").find('input[type="textarea"])'), I am able to input values into the textarea in Chrome using $($("#selector").find('input[type="textarea"]).val(someValue)') but encou ...

The React MUI Tab component triggers a re-render and clears the states of its child components

In our endeavor to create a Tab Page using React MUI, we aim for each Tab to contain a child component. While there are no issues when adding these child components individually to a page without tabs, problems arise when incorporating them within the Tab ...

Validate whether the path parameter in NextJS is null or empty before executing the query

Currently seeking a method to determine if the query value is empty using the path parameter approach. Have a file named pages/search/[variable1].js Below is the code snippet: import { useRouter } from "next/router" const Variable= () => { ...

Is NextJS on-demand revalidation compatible with Next/link?

https://nextjs.org/docs/basic-features/data-fetching/incremental-static-regeneration#on-demand-revalidation According to the NextJS documentation, it appears that on-demand cache revalidation should function properly with regular <a> tags and when t ...

Is it possible to have a hidden div box within a WordPress post that is only visible to the author of the

How can I create a div box with "id=secret" inside a post that is only visible to the author of the post? I initially made it for the admin, but now I want the id to be visible exclusively to the post's author. For instance: If the author is curren ...

Link clicking does not trigger URL routing properly

I've been tasked with updating our web application, and I'm facing a challenge that I need help with. My boss wants users to be able to click on a link in an email and have the internal company web app directly navigate to a specific page indicat ...

generate a cookie within a pop-up window

Can someone help me figure out why my Modal Window with a cookie to display only once isn't working? I followed this tutorial at . Here is the link to my website: Here is my code: <div class="modal fade" id="myModal" tabindex="-1" role="dialog" a ...

Determining when a checkbox changes state using HTML and JavaScript

My main objective is to display divX2 when the checkbox for x2 is checked, either by directly clicking on x2 or by clicking on the "Check All" checkbox. The functionality works as intended when the checkbox for x2 is clicked, but it fails to work when the ...

Loading tabs dynamically in Angular 2 based on header click event

I successfully created a custom tab component using Angular 2, and it is functioning well with the following usage: <us-tab-verticle> <vtab-content [tabTitle]="'Basic Information'"><basic-info> </basic-info></vtab- ...

Disabling an anchor using the 'disabled' property is proving to be a challenge for me

I'm attempting to dynamically enable or disable an anchor element based on the user's role. So far, I've tried a few different methods: document.getElementById('myBtn').disabled = true; However, this returns an error: The propert ...

Utilizing Vue refs to set focus on a specific element target

Clicking on "span class="before-click" should hide it, and display input class="after-click" instead. The displayed input must be in focus! However, when trying to use $refs.afterClick to access the DOM and apply .focus(), an unexpected error stati ...

JavaScript's inability to properly export CSV data containing the "#" character has been causing issues

When exporting JSON data to CSV format and downloading it using JavaScript, everything typically works fine. However, there is a problem if the data contains the hash sign #. The function fails to export all the data in that case, for example: This is my ...