0
\$\begingroup\$

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;
\$\endgroup\$
1
  • \$\begingroup\$ 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 \$\endgroup\$
    – Stephen J
    Commented Jan 30, 2018 at 17:30

2 Answers 2

4
\$\begingroup\$

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.

The most interesting trigonometry wave gif I could find

\$\endgroup\$
4
  • \$\begingroup\$ how one vector dir is composed into 3 components v+ ucos(theta) + rsin(theta) into the equation in the code ? \$\endgroup\$
    – Mahmoud
    Commented Oct 1, 2014 at 21:53
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Oct 2, 2014 at 0:35
  • \$\begingroup\$ could you explain more please, about if vectors are orthogonal how can they be used a one direction vector ? \$\endgroup\$
    – Mahmoud
    Commented Oct 2, 2014 at 11:41
  • \$\begingroup\$ I will try to edit that into my answer. You might also consider visiting Game Development Chat for back-and-forth questions. \$\endgroup\$ Commented Oct 2, 2014 at 15:28
1
\$\begingroup\$

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:

enter image description here

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.

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, V is the 'U`' that's pointing up in the picture:). That was a typo when I made the picture. \$\endgroup\$
    – Arun R
    Commented Oct 2, 2014 at 14:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .