Exploring a Ring with Three.js OrbitControls Rotation

I am having an issue with my OrbitControls camera setup. Currently, the camera rotates around a fixed point at player.position.x, y, and z coordinates. While it works fine, I need the camera to rotate around a ring instead. In the examples provided, the first picture shows the target placed in the center, while the second has the target positioned slightly to the left of the center

To elaborate further:

controls.target.x = player.position.x;
controls.target.y = player.position.y+3; 
controls.target.z = player.position.z;

As you can see from the code snippet above, we are currently rotating around a fixed point. However, I require the target position to change dynamically based on controls.getAzimuthalAngle() or some other parameter. I seek to replicate the camera behavior found in games like GTA V, where the player is not centered on the screen but positioned off to the left slightly for better aiming capabilities without obstructing the central view.

Your assistance with this matter would be greatly appreciated...

Answer №1

When implementing a 3rd person camera, the key is to always keep the camera focused on the player or an offset from the player.

The approach involves having the camera smoothly follow the player's movements over time.

In the provided code snippet, there is a cameraRig that trails the player. The purpose of the camera rig is to handle the positioning of the camera above the ground without requiring manual calculations. Additionally, a camTarget object is connected to the player, ensuring that the camera maintains a view at shoulder height.

function main() {
  // Code for initializing scene, camera, lights, and objects
}

main();
body {
  margin: 0;
}
#c {
  width: 100vw;
  height: 100vh;
  display: block;
}
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r113/build/three.min.js"></script>
<canvas id="c" tabindex="0"></canvas>

Developing effective 3rd person cameras can be challenging. The explained technique may seem basic, comparing it to dragging a camera rig behind the player like a tether. If the camera exceeds the maximum distance threshold, it adjusts its position while maintaining proximity to the player.

An alternative common strategy is to place the camera on an extendible rod so that moving backward extends the camera's distance accordingly.

function main() {
  // Similar setup as before for camera and scene initialization with added functionality for handling camera distance
}

main();
body {
  margin: 0;
}
#c {
  width: 100vw;
  height: 100vh;
  display: block;
}
<script src="https://threejsfundamentals.org/threejs/resources/threejs/r113/build/three.js"></script>
<canvas id="c" tabindex="0"></canvas>

Further considerations in 3rd person camera design involve managing obstacles obstructing the player-camera view and addressing scenarios like passing through doorways. These complex situations require special handling such as fading out obstructive elements or intelligently adjusting the camera perspective during transitions.

It's noteworthy that parts of the implemented code assume objects have simple world positions (no parent-child relationship affecting coordinates). For instances where objects have parents, additional steps are needed to accurately determine and apply world positions.

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 second tab on Bootstrap5's tab content feature seems to generate an endless amount of white

I am working on a project where I need to display statistics for both the current month and year. To organize this data, I created a card with tabs for each - one tab for the month and another for the year. By default, the month tab is loaded first. Howev ...

React window resizing actionsWould you like some more information on this

I am attempting to create a resize function that will retrieve the window width and dynamically display it using react. Here is my current code: class Welcome extends React.Component { constructor() { super(); this.state = { ...

Exploring the process of iterating through a JSON post response and displaying its contents within image divs

After receiving a JSON post response, I am looking to iterate over the data and display it in image divs. Can anyone provide guidance on how to achieve this using JavaScript? Thank you. Here is the JavaScript code that handles the JSON post response: cor ...

Is it possible to implement H-Screen in Tailwind without causing the page size to expand? My website has a fixed navbar at the top, and I

My setup includes PanelMaster, along with Panel 1 and Panel 2 components. PanelMaster: flex Panel1: flex-col Panel2: flex-col I have a Navbar component at the top of the page with 'h-1/5' applied to it. However, I'm facing an issue where ...

Creating a dynamic div with various paragraphs using only Javascript

My goal is to dynamically generate paragraphs with their respective icons inside individual div elements. For instance, if the service API returns 30 items, I will create 30 div elements with the class "tile". However, if only one item is returned, then I ...

`ACCESS DENIED: Unauthorized access attempt detected in Node.js``

When attempting to connect, MySQL is establishing a connection with an unfamiliar IP address. Refer to the code below: .env MYSQL_HOST=domain.example.com MYSQL_USER=**** MYSQL_PASSWORD=**** MYSQL_DB=**** MYSQL_PORT=3306 connection.js const mysql = requir ...

What is the best way to incorporate a class creation pattern in Typescript that allows one class to dynamically extend any other class based on certain conditions?

As I develop a package, the main base class acts as a proxy for other classes with members. This base class simply accepts a parameter in its constructor and serves as a funnel for passing on one class at a time when accessed by the user. The user can spe ...

The dot operator cannot be used to access Json objects

Utilizing a4j jsFunction to transmit data to the server and receive JSON in return <a4j:jsFunction name="submitData" action="#{imageRetriveBean.saveData}" data="#{responseNodesPathsBean}" oncomplete="processData(event.data)"> <a4j:param name= ...

What are effective strategies for safeguarding my AngularJS application code, particularly from unauthorized access through the browser's source code?

I am currently working on an AngularJS application. I have encountered a challenge where the end user is able to view the app code from the browser's source code. I am seeking advice on how to address this issue effectively. Is there any recommended ...

Requesting data from a server using JavaScript/Ajax/Flash technologies over

Here is the code snippet I am currently using: swfobject.embedSWF("/Content/open-flash-chart.swf", "my_chart", "750", "300", "9.0.0", "expressInstall.swf", ...

Relocate the preview division below the button in the Kartik File Input Widget

There are two input file buttons that allow users to upload an image on each button. Upon selecting an image, a preview of the image will be displayed above the button. The current layout is shown here: https://i.sstatic.net/aLAbo.png When images of diff ...

An AJAX script for dynamically adding, modifying, and removing records from a database

How can I implement a functionality where by pressing "-" the vote is removed from the database, and when any other number is selected, the vote will be updated in the database with that value? The default value of the dropdown list is votacion.votCalific ...

Working with Garber-Irish in Rails: Streamlining Administration and Keeping Code DRY

I am currently implementing the innovative garber-irish technique to organize my JavaScript files. Here's my issue: I have a Model (let's call it Item) with an init function located in app/assets/javascripts/item/item.js For example: MYAPP.ite ...

Are there any other benefits to utilizing the Decorator pattern besides enhancing dynamic behavior?

Currently, I am diving into the intricacies of the Decorator design pattern and a particular thought has been nagging at me. What if we simply had one base class with boolean values representing its features? Consider this example: Imagine a textview tha ...

Opting for <button> over <a>

I am currently working with ReactJS in conjunction with a Bootstrap Nav Bar. Bootstrap utilizes an <a> tag for the navigation buttons. I am aiming to have the buttons scroll down to a different component on the page without resorting to using an href ...

What is the TypeScript equivalent of the Java interface.class?

Can you write a Java code in TypeScript that achieves the same functionality as the code below: Class<?> meta = Object.class; and meta = Processor.class; // Processor is an interface In TypeScript, what would be the equivalent of .class? Specifica ...

Execute my function when the Redux state changes in the store in ReactJS

I am facing a specific issue in my React-Redux application. I am using Redux-saga to communicate with a REST API, which does not return a promise. Within my application, I also utilize state (in addition to the store) that I wish to update after receiving ...

Storing an Excel file with JavaScript: A comprehensive guide

I've been struggling to save an Excel file using Javascript, but I'm facing compatibility issues with different browsers. I initially tried using BASE64 along with data URL, which worked well in Chrome and Firefox but failed in IE and Safari. ne ...

Update the appearance of a cell if the value within it is equal to zero

I have discovered a way to achieve this using inputs. input[value="0"] { background-color:#F7ECEC; color:#f00;} Now, I am looking for assistance in applying the same concept to table cells. Can anyone provide guidance? Thank you. ...

How can I pass an object into EJS templates from views in Express 3.x?

Currently, I am utilizing ejs templates in combination with node.js and express 3.x. Is there a way to display the data object that is passed into the view? Can it be achieved similar to this example in index.ejs: <%= dump(session) %> ...