Posistion of point after rotation

Associate
Joined
27 Jun 2008
Posts
1,538
I'm terrible with Maths (and probably always will be) and need some help with this. :p

Basically I want to find the position of a point after the centre has rotated. This image will help explain I hope.

2wf4xfo.png


I just need to know the new position in 3D space of the point after the centre point has rotated 45 degrees. The centre is at 0,0,0 and the point is at 4.78, 5.20, 9.77 (just an example).
 
Surely you need to know the axis of rotation to be able to give an answer to this?

It was just a simple transform example so I didn't think it mattered. Lets just say it's X.

Simple rotation matrix (rotations about X, Y or Z):

http://mathworld.wolfram.com/RotationMatrix.html

Or quaternion (rotations about any axis)

The latter is preferred because they are infinitely more awesome

http://www.gamedev.net/page/resource...n-powers-r1095

I thought it would involve a rotation matrix. :( The last looks like what I'm looking for though, thanks.
 
I'm actually working in 3D space and not 2D (image properly didn't help). The point is a vertex from a 3D mesh. I only know the current position of the vertex respective to its origin which is usually 0,0,0. I basically want to be able to enter angles in degrees to rotate the whole model from the origin and need to calculate the new position of each vertex.

Using the method you showed will it allow me to input these values and get out the new position? Example: A vertex at 5, -5, 0. The model is rotated 20 degrees on the X-axis and 30 degrees on the Z-axis. That vertex is now at 6.679358482, -1.568988323, -1.710100651. I understand that they'll probably be some precision error too.

Annoyingly that gamedev article won't load now.
 
For a point x, y, z

Rotation about X:
new x = x
new y = yCosθ - zSinθ
new z = ySinθ + zCosθ

Rotation about Y:
new x = xCosθ + zSinθ
new y = y
new z = -xSinθ + zCosθ

Rotation about Z:
new x = xCosθ - ySinθ
new y = xSinθ + yCosθ
new z = z

So to do as you require, chain the rotations together and use the result of the first rotation as the x y z of the next rotation.
 
Last edited:
Incidentally, these are the same operations that are performed in the matrix approach and BunnyKillBot has just unravelled them into a flat/linear calculation for each axis of the point for you, so after getting a handle on the trig here for learning/understanding how it works purposes, going back to use matrix transforms will help you in the long run (not least because that's also how the graphics APIs do it, but also because such ways of operating can be combined with other transformations you may end up doing like scaling and translating of your points and objects).

Just note that if you're going to do lots (or specific sequences) of rotations on a point, there's an anomaly known as gimbal lock that you might run into and need to overcome. Don't worry too much about overcoming it right now, just look it up to know it can account for some odd behaviour in case you hit it and need the explanation as to why it is happening.
 
Thanks, I've give that a shot.

I have already written a function before to convert a rotation matrix to degrees and it worked well although there were very slight discrepancies (probably due to gimbal) and it would sometimes get the values for each axis the wrong way around. It didn't do it all the time and mostly got it correct for all axis.

All written in PHP too. :D Not the most ideal language for this but it works.

PHP:
function vmd_rotmatrix2deg($m0, $m1, $m2, $m3, $m4, $m5, $m8)
{
	$angle_y = -asin(round($m2, 6));
	$c = cos($angle_y);
	$angle_y = rad2deg($angle_y);
	if(abs($c) > 0.005)
	{
		$trx = round($m8, 6) / $c;
		$try = round(-$m5, 6) / $c;
		$angle_x = rad2deg(atan2($try, $trx));
		$trx = round($m0, 6) / $c;
		$try = round(-$m1, 6) / $c;
		$angle_z = rad2deg(atan2($try, $trx));
	}
	else
	{
		$angle_x = 0;
		$trx = round($m4, 6);
		$try = round($m3, 6);
		$angle_z = rad2deg(atan2($try, $trx));
	}
	return array(vmd_rf2p($angle_x), vmd_rf2p($angle_y), vmd_rf2p($angle_z));
}

I don't think it's useful for this though as I'm not working with a matrix this time round and don't need to covert them to one.
 
It appears to be working although the results are hit and miss. I did a test for 45 degrees. The model rotates on the correct axis but it is not at 45 degrees. It also seems to scale it weirdly along the X and Y axis making the model look stenched. The amount it scales by is dependent on the angle of rotation. I guess it's not performing the calculation correctly or I've dun goofed.

PHP:
$offset_rot_x = $vert_data_x * cos(45) + $vert_data_y * -sin(45);
$offset_rot_y = $vert_data_x * sin(45) + $vert_data_y * cos(45);

This is the code for 45 degrees on Z. I then apply the offset to the vertex later on.

It runs in a while loop for each vertex.

Here's a screenshot of how it looks afterward. The colored boxes are its original orientation and scale.

25k6h3q.jpg


EDIT: OK, sorted the scaling issue, was me being stupid but the rotations still seem to by the wrong amount. Do they need to be radians instead of degrees?

EDIT2: Durp, looks like I answered my own question with that. They should be radians. :D
 
Last edited:
Hehe, yes indeedy, rads not degrees.

What allot of programmers seem to do is somewhere near the top write a compiler directive:

#define pi 3.1415926535

Then, when you need it, 180 deg = pi, 90 def = pi/2, 45 def = pi/4 etc etc

edit: If your using php, i doubt this post will be any use to you!

If you want the quarternion method let me know and i will expound it for you.
 
Last edited:
You can try if you want but it's probably beyond my understanding.:p

Anyway, I Just want to say thanks to all for the help. Everything is working nicely now and I finally understand how rotation works in a mathematical sense... at least this anyway.:rolleyes:;)
 
Back
Top Bottom