3D Rotation Tutorial
This will take you through the simplest method of doing rotation in your 3D world, so simple I even thought of it myself! There is an example source to download at the bottom of the page.Rotation
This works on the same concept as the camera movement. Just set the camera to the origin (0,0,0) and have it facing forwards. To get the camera facing forwards we have to spin the dots round the camera. We actually spin it in 2D on 2 planes to keep things simple.To do this we use trigonometry because with a right angled triangle we can keep the hypotenuse the same and change the side lengths. We need 3 pieces of information to do this, the hypotenuse the starting angle of the point and the angle it has to be rotated.
To get the hypotenuse we just use pythagorus a^2 = b^2 + c^2. Applying this to the context we get:
hyp = sqr( x^2 + y^2 )
Next we need the original angle. To get this we need to use inverse trig.
ang = sin-1 * x / hyp or ang = cos-1 * y / hyp
However in VB its different because you have to use the ATN function.
The ATN function gives you the angle from the ration of 2 sides of a triangle. Don't forget that this is in radians(2 pi radians=360) because that is what VB does. So you just type in the ratio and hey presto (almost):
atn(x/y)
The problem is that VB can't work out whether it's upside down so you can mess around a bit like I did to get it working. Also it doesn't like 0's so you have to filter then out as well.
The two planes that I usually use for rotation are the x,z and y,z. The x,z spins it left and right, z,y spins it up and down and x,y does a barrel roll which I don't care for.
After that the trig is very simple just:
X = Sin(AngY - CamY) * HypY
Z = Cos(AngY - CamY) * HypY
This piece is for rotation around the y axis (so no y coordinate). Which is Sin as which is Cos depends on which you divided by which in the ATN() function.
Conversions
Radians * 180 / pi = Degrees
Degrees * pi/180 = Radians
pi = 4 * atn(1)

Download the example source
previous:3D basics
next:3D with matrices
© Jonathan Waller 2005; QuantumState Visual Basic




