# Explaining vector math for a pendulum camera

I came across a code that I guess is doing a pendulum camera, that's the class name. I would like to know the math behind that. I would like also to know how to decipher those stuff in game math, what are your recommendations ? I already own the book 3D Math for computer graphics by lengyel, but those stuff are not explained there.

        Vector3 r = transform.right;
Vector3 u = transform.up;
Vector3 v = transform.forward;

Vector3 dir = v + u * Mathf.Sin(Time.deltaTime * 0.01f)  + r * Mathf.Sin(Time.deltaTime *0.01f + 0.1f) * -0.1f;
dir.Normalize();

Quaternion rotation = Quaternion.identity;
rotation.SetLookRotation(dir, Vector3.up);
transform.rotation = rotation;

• If you ignore what they do, sin/cos functions basically go up to 1 and down to -1 continuously. Time gives a constant change. Sin and cos are basically what you use when you want things to bob back and forth staying within a constrained space – Stephen J Jan 30 '18 at 17:30

The three component vectors right, up, and forward probably point along the axes x, y, and z relative to the camera. So by adding these vectors together you can compose any other vector. It works the same as if you built a vector by specifying its three elements individually, except that you are adding three vectors which each have one non-zero element. Normlizing that sum guarantees that the total length of the sum is a vector of length 1.

If Time.deltatime is the time since last update then, except for small variations, dir will be not vary from frame to frame. It's essentially taking the sine and cosine of constant values, which yields a constant camera direction. If that time value were accumulated rather than constant, then this would yield a circular motion of the camera. On the x-y plane, the x and y coordinates could be specified by taking the sine and cosine of a single angle. Your code sample appears to be doing that, using Time.deltatime as its angle (but I think it's doing it wrongly, as it's essentially constant each frame).

This picture is loosely related, please ignore the complex part; I was just trying to find an animation of the unit circle.

• how one vector dir is composed into 3 components v+ ucos(theta) + rsin(theta) into the equation in the code ? – Mahmoud Oct 1 '14 at 21:53
• Guessing from their names, each of those components is orthogonal to the others. So they could be combined to produce any direction, and the following call to Normalize would turn it back into a unit vector. – Seth Battin Oct 2 '14 at 0:35
• could you explain more please, about if vectors are orthogonal how can they be used a one direction vector ? – Mahmoud Oct 2 '14 at 11:41
• I will try to edit that into my answer. You might also consider visiting Game Development Chat for back-and-forth questions. – Seth Battin Oct 2 '14 at 15:28

He's trying to oscillate between V and V+U*0.01+R*0.1, I think. If you want to visualize, it is trying to do something like the picture below:

The arrow at the top represents the path DIR will take as time proceeds and it will go back to V when the sin value becomes zero. The summation, just adds the 'perturb' effect to the DIR vector, so that it doesn't always end up as V.

• thanks, but where is V in the picture? – Mahmoud Oct 2 '14 at 7:08
• Yeah, V is the 'U`' that's pointing up in the picture:). That was a typo when I made the picture. – Arun R Oct 2 '14 at 14:17