Why is $\cos (90)=-0.4$ in WebGL?

16.9k Views Asked by At

I'm a graphical artist who is completely out of my depth on this site.

However, I'm dabbling in WebGL (3D software for internet browsers) and trying to animate a bouncing ball.

Apparently we can use trigonometry to create nice smooth curves.

Unfortunately, I just cannot see why.

I can accept this diagram:

triangles and circle

However, running some calculations just do not make sense to me:

Let's set $\alpha$ to 45 (around where it appears to be in the diagram) and find the cosine value, thus giving us the green line. $$\cos(45) = 0.5$$

Fair enough. $\cos(\alpha)$ / the green line is $0.5$ units.

But now this is where it all falls apart. I would have thought if we set $\alpha$ to $90$, $\cos$ would become $0$. Do you see why I think this? Look at the digram, isn't that reasonable to think? Similarly, $\cos(0)$ I would have said should equal $1$ (twice that of $\cos(45)$ )

While $\cos(0)$ does equal $1$, this does not check out: $$ cos(90) = -0.4$$

Just do not get that $0.4$? Could someone explain? That just makes no sense to me. None.

I'm using the google calculator and I would stress I have not touched maths for about $6$ years (ever since I left school!) so please lots of examples and words to explain!

9

There are 9 best solutions below

12
On BEST ANSWER

The cosine function takes radians as its argument in most computational languages.

Indeed, $\cos 45^{\circ} = \sqrt{0.5}$, and $\cos 90^{\circ} = 0$. However, in math, for various reasons, we don't like working with degrees. We work with radians, where $2\pi\ \textrm{radians} = 360^{\circ}$.

In fact, $\cos 90\ \textrm{radians} \approx -0.44807$ and $\cos 45\ \textrm{radians} \approx 0.525$.

90 radians would be about 5156 degrees, or about 14.3 turns around the circle!

0
On

There are two common units of angle measurement: degrees, with $360$ degrees making one full circle, and radians, with $2\pi$ radians making one full circle.

Calculators can usually be switched from one "angular mode" to another. Your calculation of $\cos(90)$ comes from using a calculator in radians mode, while your angle is in degrees. (You're perfectly correct that the cosine of $90$ degrees is $0$.)

5
On

Your problem is with units. As mathematicians, we generally measure angles in radians not degrees. The conversion is $$ x\text{ degrees}=\frac{x}{180}\pi\text{ radians} $$ Most software generally takes the argument of trig functions as radians. Thus $\cos(45^\circ)$ is computed as $$ \cos(45^\circ)=\cos\left(\frac{45}{180}\pi\right)=\cos\left(\frac{\pi}{4}\right)=\frac{1}{\sqrt 2} $$ You are correct that $\cos(90^\circ)=0$, but when you input $\cos(90)$ into a calculator, the calculator reads $90$ in radians, not degrees. What you should input is $$ \cos(90^\circ)=\cos\left(\frac{90}{180}\pi\right)=\cos\left(\frac{\pi}{2}\right) $$ which will return as $0$.

For evidence, type cos(90) into google (here google interprets 90 in radians, not degrees). Compare by typing cos(90 degrees) into google.

To summarize, if you want to find the value of $\cos(x^\circ)$, type $$ \cos\left(\frac{x}{180}\pi\right) $$ into your calculator.

0
On

Others have already explained that you were getting results from the calculator for radians and not degrees.

You can make the Google calculator (and most others) work in degrees as well. When you bring up the calculator, the upper left button will say "Rad" and have a blank button (slider) next to it. If you click the slider, you change the calculator to "Deg" (degrees) mode. Then hit AC to clear the calculator, and type in a new calculation right in the calculator answer window (not the google serach bar).

12
On

Your written question may be: Could somebody explain cos to an idiot please? I will not answer this, as it wouldn't be helping.

The question answered is more like: Why is cos not behaving like I expect it to? I won't answer this either, as it is already answered sufficiently. And it's probably a duplicate...

However I want to answer your intended question, at least as I anticipate it. For this I will concentrate on this part of your question:

However, I'm dabbling in WebGL (3D software for internet browsers) and trying to animate a bouncing ball.

Apparently we can use trigonometry to create nice smooth curves.

I anticipate your real question might be something like: How do I get a nice path to animate a bouncing ball?

The answer is: Not with trigonometric functions as they help you with circular movement, e.g. a ball fastened on a string whirling around a fixed point. You should try a parabolic path. The function y = x² is the easiest example for a parabel. But you probably want to set a starting point, swap and stretch it.

So instead of an explicit function for each point on the path you could use a simple iterative process:

Have a starting position (x,y), a starting speed (m,n) and some kind of gravity (g, ~10m/s² will look natural but that's something for advanced). For each iteration you update your position like this:

x := x + m
y := y + n

and your speed:

n := n - g

To make the ball bounce you invert the speed when it hits an obstacle, e.g. bouncing from the ground at level b:

if (y < b) then n := -n

This will provide a quite basic behaviour and needs some tuning and extensions, especially for natural motion, sharp borders of obstacles, decreasing bounce height, etc.

Note that I use := to resolve the problem that x = x + m would require m = 0 to be correct in a mathematical context. The "operator" := is meant as a redefinition to spare you an iteration index or parameter, i.e. x_i or x(i).

1
On

Cosine and sine functions create smooth curves when plotted as a function of time. If you graph $$\cos\left(2\pi t\right)$$, where t is time in seconds, the ball will be start at height 1, drop to height 0 at 1/4 sec, bounce back up to 1 at 1/2 sec, 0 at 3/4 sec, 1 at 1 sec, and so on.

You can change the frequency by changing what is in the parentheses. For example, $$\cos\left(4\pi t\right)$$ will bounce twice as fast and $$\cos\left(\pi t\right)$$ will bounce half as fast.

If you multiply the whole function by a value it will change the height of the function. So $$4\cos\left(2\pi t\right)$$ will cause the ball to bounce four times as high.

Many oscillating things, like light and sound waves, follow this kind of trigonometric function. But a bouncing ball does not, so it might not look natural. Here's a site that gives the correct equation for a bouncing ball.

0
On

You've made multiple errors in your question.

  1. Let's set α to 45 (around where it appears to be in the diagram) and find the cosine value, thus giving us the green line.

    cos(45) = 0.5

    Fair enough. cos(α) / the green line is 0.5 units.

    The green line is not 0.5 units.

    The green line, red line, and the $x$ and $y$ axes form a square whose diagonal is 1 unit. By the Pythagorean Theorem,

    $$ \begin{array}{} (\cos \alpha)^2 &+\quad (\sin \alpha)^2 &= 1^2 \\ (\cos \alpha)^2 &+\quad (\cos \alpha)^2 &= 1 \tag{green length = red length}\\ \end{array}\\ \cos \alpha = \cos 45^\circ = \frac{1}{\sqrt 2} \approx 0.707 $$

    Therefore, the length of the green line is about 0.707.

  2. Similarly, cos(0) I would have said should equal 1 (twice that of cos(45) )

    While it is true that $\cos 0 = 1$, there is no justification for "twice that of cos(45)".

  3. cos(90) = -0.4

    Just do not get that. 0.4? Could someone explain? That just makes no sense to me. None.

    I'm using the google calculator

    Google Calculator uses radians as the default unit for angles. The conversion between units is: $360^\circ = 2\pi\ \textrm{radians}$. Therefore, Google Calculator interpreted your request as

    $$ \cos 90 = \cos \left(90 \cdot \frac{360^\circ}{2\pi}\right) \approx \cos 5156.62^\circ $$

    To get the result you intended, you have to switch it to Degree mode before typing c90=

    Degree mode in Google Calculator

0
On

I've looked over the answers so far, and I it very likely with all that 'accurate' information, you may well have the answer you need. If not, I can sympathise with your dilemma so if I may, I can make a few basic suggestions from my initial "experience" with Fourier Calculus. It can be confusing and may not address 'fundamental question':

  • Unfortunately, I just cannot see why.

As I get this, the emphasis is on "why"? In that spirit, I suggest that the issue is firstly that you may be a visually oriented person, I take my lead on that because you 'get' the diagram above. So you understand that the COSINE() function of an angle, is a ratio of two sides of any right-angle triangle: Adjacent and Hypotenuse.

Related, geometrically, to the angle with the adjacent adjacent to the sloping side (hypotenuse).

If I can suggest the "lazy way" first; the fraction created from the ratio between these sides as the corner-angle, alpha, changes is-a *curve*in Euclidean space. So if you take a grid and plot sides: (x, y) at the centre for angle alpha ...

  • x = (Length of the Adjacent side)
  • y - (Length of the Hypotenuse side)

On an axis ... as you change the angle you will see a Cosine curve traced at the opposite point. Common tools and web graphics use this to make 'magical' sprials and stuff. As a boy I had a Spirograph ... it creates patters. ALL of these are based on the same rations from a circle/triangle. It is a kind of magic, just like the Greeks thought. Beauty may be truth.

Why? If you don't have a Spirograph - You can makes some cool substitutes.

  1. Create a triangle shape with three flat lengths of wood.
  2. You can ensure the triangle retains rectangle with a nail or tight screw.
  3. Leave the other two sides - FREE to move.

When you attach a pen to the arms of this right-angle, and move the sides(arms); you will trace curves. Congratulations, they are cos or sin curves!

Once upon a time 'science' like Geometry included philosophy and 'manners' for conduct. We don't live in that world any longer, do we? The aim of Geometry is to consider every thing (material item) objectively in 3-Dimensional space. At that time, 3,00 years ago Science was a kind of magic.

The "Smooth CURVES" you asked about, were considered part of the bounty or reward to those of enquiring minds. Recall that is is like, 2,000 years before Picasso -- Euclid had abstract art. And pretty cool stuff too!

......

0
On

Wrong unit. You talk about deg, while the function obviously expects rad, which gives -0.4.