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

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!


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.


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).