Camera Viewport targetting, quaternion or euler

I have this camera script in 3d space where i need the main character to stay within the viewport, and pointed towards the enemy while targetting is on.

The character will be relative to the camera viewport, either left or right, and it can target upwards or downwards too but never overshoot the viewport.
How can i do this?
I have used quaternion at least but i’m not sure how to clamp the angle.
It’ll be like prototype or legend of zelda targetting system.

The Quat vs Euler thing I think is the lesser problem here (and personally I’d just do Quat, because I almost always use Quat… I only use euler’s when I know a staticly defined orientation, since they’re human readable).

The problem is the ‘viewport’. So we need to consider the view frustum, which describes the entire 3d space that the camera can actually see.

The view frustum usually has the shape of a pyramid with its head cut off (though other shapes can be found with different projections, such as a cuboid shape for orthographic projection). It looks a bit like this:

Note, the frustum if it had its pyramid head, the pinnacle of the pyramid would be located at the camera lens. Where it’s chopped off is defined by the ‘near’ plane.

The problem that arises is where is the cannon point of origin located (where does the projectile begin)?

If it exists at the same position as the camera, then we know that we can rotate left or right from facing forward one half the horizontal field of view.

BUT, if the cannon point of origin is located somewhere other than the same position of the camera, well… how far you can turn depends on the range of the projectile.

If the cannon is in front of the camera, and it’s range is very short… it can turn very far to the right or left since it wouldn’t exit the view port. Where as if its range is very far, you wouldn’t be able to turn very much, else you’d strike the viewport edge way far out at some point. This also goes for the vertical, as well as angle of view relative to the camera. If the camera is slightly above the cannon and looking down at it, then you see much more below the cannon, but not above the cannon.

All of this complicates the calculation of it a bit more than the prior.

BUT, it all hinges on that view frustum.

OF COURSE

The easier method would be to just get a ‘best guess’. If the viewport and cannon stay in similar relative positions, just play with the numbers until you find what feels like a good max turning angle… damned if it slightly overshoots or undershoots the viewport edge. No complex view frustum math needed.

Then once you have that max change. You merely just compare the angle between the shot direction and the camera foward to make sure it doesn’t exceed this range.

1 Like

Ah, the lord of duct, on the throne of ash.
Come to enlighten me again.

I totally don’t catch the cannon thing…since i’m using a character that strafe around.

Currently, i managed to achieve a little…
The priority is : the character and the enemy can stay on the same position when “locked on”
And from there, we acquire the character rotation as the parent direction,
And it will the rotate by say, an amount of 60 to -60, as according to current direction, so the character
either maxed at left or right side of the camera, and then still aims at the enemy.

I’m not sure if i have solved it yet…
This frustum thing sure seems like the better goal for clipping solution…
But the problem with this, collision detection is that i am not sure whether i have to

  1. reduce the camera distance, to nearest point hit to avoid camera going in meshes…
  2. or rotate the direction of the camerato whereever it doesnt clip in, somehow, which may break the current
    algorythm of only having character and enemy as the LookAt priority…

This quaternion math is so confusing as i still don’t know how to…parent the
mouse axises to the main game object rotation, instead of constantl orbiting it…

Cannon is a term often used to name the origin of a projectile. You’re shooting things, the cannon is where it fires from.

A cannon could be a character with a gun, it could be a gunship, it could be a catapult, it could be really anything. I don’t KNOW what your cannon is, so I just call it a cannon.

Think like how in Physics whenever they teach you Newtonian Physics of projectiles, the problems are often phrased in terms of a cannon.