Rotation

What a simple sounding subject, at least thats what I had thought, after all don't computers do all that stuff for you ? Well, some of the time.

I came to needing to know about rotation for something very simple. Here we are looking down upon a scene with a camera at the bottom looking up at a board with a central pivot point, there is an object on the bottom left corner of the board, the black square thing Now we can instruct the camera to look at any coordinate in this view. So to track the object as it moves around the screen on the board we need to know where it is in two dimensional space (as opposed to an angle). This exactly the situation I found myself in using OpenGL 3D code, I would instruct the board to rotate but then the camera did not know where to look (plus the camera has to look first in OpenGL)
No problem all I needed to do was a bit of Maths, calculate the distance from the object to the rotational center (radius) and then find the angle then work out where that angle would lead to at the previous radius.
 This just draws a circle point by point, one degree at a time This draws the each point from the center and shows that the there are tiny holes in our circle. If the circle had a 4 mile radius imagine the distance between each line ! Click in the eight segments and you will see the angle found is always between 0-180(blue) Click in these eight segments and the code will compensate, working out your click via the radius and the angle. rotation01.java rotation02.java rotation03.java rotation04.java

A bit more detail.

Finding the radius from the center is actually the same as finding the hypotenuse of a right angled triangle so we finally have a use for Pythagoras' theorem.

`r=Math.sqrt(Math.pow(length((int)sx,(int)cx),2)+Math.pow(length((int)sy,(int)cy),2));`

Reads in English, radius is equal to the square root of the distance of X from there center plus the distance of Y from the center.

I did have the length function always returning a positive length but this proved problematic later on, so it just subtracts the second parameter from the first now.

Now we have the radius we need the angle, this time we use trigonometry.

``` precalc = length((int)sx,(int)cx) / r; angle = (180D/Math.PI) * Math.asin(precalc); ```
Now this looks complicated but is the infamous `sin(x) = Opp/Hyp` and is where I hit my first hurdle, although most books and people will tell you to use `sin to the power of minus one` that is a short cut of saying use the asin function in radian mode.
So the first line does the `Opp/Hyp` and the second line converts the answer to degrees.

We have our radius and the angle from the center, or do we ?

Using the third applet above click anywhere in the eight segments and you will see the result (in blue) is always on the right hand side, denoting an angle of between 0-180.
As this is a measurable error for each quarter of the circle a quadrantCorrection function is straight forward to write(see rotation04.java).

So how can we use the radius, the angle and nothing apart from the location of the center of the circle to plot and point denoting that location in 2D space ?
More trigonometry; ``` rangle = angle * Math.PI /180D; xa = Math.cos(rangle) * r + cx; ya = Math.sin(rangle) * r + cy; ``` Where angle is first converted from degrees to rangle in radians and then the X coordinate can be calculated with cos and the Y coordinate with sin (r = radius and cx,cy are the center of the circle).

And thats it, easy really !?

This page looks complicated but I did infact create these java programs to test my theories. they started life as full applications but have been altered just a little bit to allow them to work as web applets.
The animation was created in GIMP with blender providing the frames.

email

root

``` ```

``````Disclaimer:
This page is by me for me, if you are not me then please be aware of the following
I am not responsible for anything that works or does not work including files and pages made available at www.jumpstation.co.uk
I am also not responsible for any information(or what you or others do with it) available at www.jumpstation.co.uk
```
In fact I'm not responsible for anything ever, so there!
```