Utilizing THREE.js to guide a camera along a designated route

I'm currently working on developing a game using THREE.js. The game involves a path that consists of various curves and some straight paths.

After finding an example online, I attempted to incorporate TrackballControls.js into the project to enable the camera to follow a specific line. However, rather than looking in the intended direction, the camera's movement appears erratic.

Below is the snippet of code:

var controls = new THREE.TrackballControls(camera, render.domElement);
var numPoints = 50;
spline = new THREE.CatmullRomCurve3([
    new THREE.Vector3(0, 0, 0),
    new THREE.Vector3(50, 0, 0),
    new THREE.Vector3(0, 0, -100)
]);

var material = new THREE.LineBasicMaterial({
    color: 0xff00f0,
});

var geometry = new THREE.Geometry();
var splinePoints = spline.getPoints(numPoints);

for (var i = 0; i < splinePoints.length; i++) {
    geometry.vertices.push(splinePoints[i]);
}

line = new THREE.Line(geometry, material);
line.position.set(0, 0, 0);
scene.add(line);
var counter = 0;

if (counter <= 1) {
    camera.position.copy( spline.getPointAt(counter) );
    tangent = spline.getTangentAt(counter).normalize();
    axis.crossVectors(up, tangent).normalize();
    var radians = Math.acos(up.dot(tangent));
    camera.quaternion.setFromAxisAngle(axis, radians);
    counter += 0.005
} else {
    counter = 0;
}

Your help would be greatly appreciated.

Answer №1

Take a look at this demonstration showcasing the process of setting up a camera to track a spline curve. Simply press the "Camera Spline Animation View" button to witness the animation in action. You might find inspiration from the code snippet provided to address your own challenge.

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

React is unable to identify the `justifyContent` property on a HTML element

const anotherStyle = makeStyles((theme) => ({ container:{ margin: 10, textAlign: 'center', display: 'flex', }, })); return ( <Container className={anotherStyle.container}> <div className={classes.ro ...

I am facing an issue with my if statement not functioning properly when verifying a radio input

Trying to validate radio input to confirm if it's the correct answer, but encountering an issue where it skips the line if (answer == allQuestions[q].correctAnswer). Here is the complete code: https://jsfiddle.net/alcatel/sopfmevh/1/ for (let k = 0; ...

Encountering an issue where props are receiving a value of undefined within a component that is

After receiving a JSON response from getStaticProps, I double-checked the data by logging it in getStaticProps. The fetch functionality is working smoothly as I am successfully retrieving the expected response from the API. import Layout from '../comp ...

Updating content on a webpage via AJAX request without altering the original source code

Within the body of our document, referred to as "form.php," we have the following components: At the head section, there is a JavaScript code block: <script> function showUser(str) { if (str == "") { document.getElementById("txtHint").i ...

Creating PDFs with Rotativa that feature C3JS charts in an Asp.net environment

Currently, I am working on incorporating dashboard stats into one of my projects. I have successfully added charts to the dashboard using C3js, and everything is working perfectly. However, I encountered an issue when attempting to generate a PDF of this ...

Troubleshooting the issue of not successfully retrieving and sending values from form checkboxes in jQuery to $_POST variable

I am facing an issue with checkboxes that have identical names and use square brackets to create an array. <label> <input type="checkbox" value="Tlocrt objekta" name="dokument[]" > Tlocrt objekta </input> </label> ...

Count duplicated values in an array of objects using JavaScript ES6

I am working on creating a filter for my list of products to count all producers and display them as follows: Apple (3) I have managed to eliminate duplicates from the array: ["Apple", "Apple", "Apple"] using this helpful link: Get all non-unique values ...

Guidelines for simultaneously modifying two dropdown select options

Is it possible to have one dropdown automatically change its value based on the selection made in another dropdown? For instance, if 'Value 1' is chosen from 'dropdown 1', can we set 'dropdown 2' to automatically display the ...

Avoid running getStaticPaths for a specific locale

Is there a way to prevent Next.js getStaticPaths from generating static pages for a specific locale? In my Next.js configuration: i18n: { locales: ['default', 'en', 'hu', 'de', 'cz', 'eu', ...

Executing a JavaScript function at a specified interval

I am a beginner in the world of programming and javascript. What I aim to accomplish : Executing a JavaScript function on page load, specifically showVideo(). I would like this function to run for approximately 10 seconds before moving on to the next func ...

Issue with Event.target not functioning properly when clicking outside the div to close it

I am facing an issue with my main div and sub div, which is referred to as a form because it is enclosed within the main div. I am trying to close the form when clicking outside of the main div, but unfortunately, this functionality is not working as expec ...

How can I stop the counter from running when the modal is hidden?

I am looking to increment a counter only when a modal is being displayed. However, I am facing an issue where the counter continues to increase even after the modal has been closed. Can someone provide insights on what might be wrong with my code snippet ...

Guide to locating and substituting two integer values within a string using JavaScript

Given a string such as: "Total duration: 5 days and 10 hours", where there are always two integers in the order of days and then hours. If I need to update the old string based on calculations using fields and other values, what is the most efficient meth ...

The powerful combination of ES6 and sequelize-cli

How can I execute sequelize migrations and seeds written in ES6? I attempted to use babel-node, but encountered a strange error. Command node_modules/babel-cli/lib/babel-node.js node_modules/sequelize-cli/bin/sequelize db:seed Error node_modules/b ...

Conceal a column within a nested HTML table

I am facing a challenge with a nested table column structure: My goal is to hide specific columns based on their index within the table. Can someone explain the concept behind achieving this? I am unsure of how to get started on this task. <table clas ...

Tips for enhancing the refresh rate of OpenLayer Vector layers with 200 elements

I am working on developing a timemap that combines the jquery-ui slider component and the OpenLayer library. I am utilizing the Vector layer of OpenLayers with Rules style for this project. map = new OpenLayers.Map('map', {maxResolution:'a ...

Understanding Conditional Statements in JavaScript

Excuse my lack of experience, but I have a query. I usually learn by studying examples, but this specific part of the Javascript code is proving to be quite challenging for me. if (lvl == "level0" || lvl == "level2" || lvl == "level3") { ...

Creating a custom pipe that converts seconds to hours and minutes retrieved from an API can be achieved by implementing a transformation function

Can someone please provide guidance on creating a custom pipe in Angular 8 that converts seconds to hours and minutes? Thank you. <div class="col-2" *ngFor="let movie of moviesList"> <div class="movie"> {{ movie.attributes.title }} ...

Raycasting for collision detection is not very precise

I am facing a challenge in my project where I have multiple irregular shapes like triangles, trapezoids, and other custom designs in a 2D scene all located on the Y=0 axis. I am currently working on writing code for collision detection between these shapes ...

Utilize a text file to populate a Vector3 object in a three.js environment

My challenge lies in working with a .txt file containing x,y,z coordinates of a drone flight. For example: 121.12 98.12 1.02 122.98 98.88 3.05 The first column represents the x coordinate, the second column is for y, and the third column ...