Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce LookAt behavior #3

Open
Garanas opened this issue Feb 8, 2025 · 6 comments
Open

Introduce LookAt behavior #3

Garanas opened this issue Feb 8, 2025 · 6 comments
Labels
enhancement New feature or request

Comments

@Garanas
Copy link
Member

Garanas commented Feb 8, 2025

Description

Given two points, we can orientate the camera such that it is positioned at position A and looks at position B. We can re-use the MoveTo engine function to try and mimic the LookAt behavior.

We have the following data:

  • origin, a vector in in world coordinates.
  • target, a in world coordinates.
  • zoom, the current zoom level (height) of the camera. This is technically the y coordinate of the origin.

With that we need to make the camera look at the target position. We can do that by computing:

This allows us to feed the MoveTo function with:

camera:MoveTo(origin, { heading, pitch, 0}, zoom, duration)

Implementation details

We can determine the origin and target by using the same hotkey twice in succession. The first hit determines the origin, and the second determines the target. Once we have the target we can do the computations and start moving the camera.

What is necessary

We need a function that given an origin + zoom level, and a target that we can compute the necessary heading and pitch to look at the target from the origin + zoom level.

@Garanas Garanas added the enhancement New feature or request label Feb 8, 2025
@Garanas
Copy link
Member Author

Garanas commented Feb 8, 2025

We can also have an alternative to re-use the current camera position (the origin), and just switch up the target. This allows you to 'look around' without translating the camera. Great for large battles where a lot is going on.

@BlackYps
Copy link

BlackYps commented Feb 8, 2025

I don't quite understand it. Origin is the position of the camera and target is the position of the object to be looked at right? But is zoom really the height of the camera? If so it seems redundant to the position. Or is it the FOV and we just keep it constant?

@Garanas
Copy link
Member Author

Garanas commented Feb 8, 2025

Zoom would technically be the Y coordinate of the origin. The description of the pull request matches what the MoveTo engine function expects. In the function zoom is a separate value.

@BlackYps
Copy link

BlackYps commented Feb 8, 2025

ok, so even though it's a separate value for this function we don't need to calculate it separately.
This sounds like you just need some basic trigonometry to calculate the two angles.

@Garanas
Copy link
Member Author

Garanas commented Feb 8, 2025

I think you're right, but it would be nice if someone could help me with it 😃 . There will be more math-related suggestions in the (near, as in - March) future. This issue is a nice step into that direction.

@BlackYps
Copy link

BlackYps commented Feb 8, 2025

I think something like heading = arc-tangent(target.x - origin.x, target.z - origin.z)
The two coordinate directions x and z give you a right-angled triangle, the tan() describes the ratio of the sides of the triange for a given angle. But we have to use the inverse function, because we have the sides of the triangle and we want to know the angle. Most of the time in programming the inverse function takes two arguments, because with just the ratio of the two sides you get four solutions. My explanation is rather bad, but it's a fairly standard problem, so if you dump my explanation into chatgpt it probably does a better job explaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants