Question

I'm working on a DCC-like tool with a 3D viewport that you can move around with your mouse, similar to Maya, Blender, etc.

I need help with some of the math.

There are two points I am trying to nail down. The first is dolly speed based on distance to target. On small scales (say, dist=10^-3), it dollies too fast, and on large scales (say, dist=10^6), it dollies too slow. I would like a flat "camera speed" input that will scale the effect overall, but at all scales the camera should feel the same. It shouldn't matter if I'm 0.1 or 1000 units away, it should feel like I'm approaching the target at the same speed for a given mouse delta.

The other is camera panning, also based on camera distance. Effectively I want the camera target to track the mouse cursor perfectly along a plane that is aligned with the camera and distance_to_target units away. I am unsure if perspective distortion will affect this?

Inputs are:

// Distance in world units to camera's centre of interest
real distance_to_target

// Number of pixels mouse has moved horizontally since last frame
int mouse_delta_x

// Number of pixels mouse has moved vertically since last frame
int mouse_delta_y

// The vector of the mouse delta, normalized into -1 to 1 space based on
// viewport dimensions where a value of (-1,-1) or (1,1) means the cursor
// moved from one corner of the viewport to the other, or further, since
// the last frame.
vec2 mouse_delta_vector

// Scale the dolly speed
real dolly_speed

Usually I do stuff like this empirically, trying different values until I find something I like, but for this I think it's time to try theory.

Was it helpful?

Solution

For problem 1, simply use proportional speed scaling.

    currSpeed = currDistToTarget * refSpeed / refDistToTarget

where refSpeed and refDistToTarget are values that you deem acceptable by experimentation.

As for problem2, the idea is the same - that is, you compute a distance-weighted angle. If your FOV never changes, you could try the same linear speed scaling as mentioned above. The main thing here is to find the correct value for refDistanceToTarget and refSpeed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top