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