Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rotation about a given point

I have a point, let's say p(0.0, 0.0, 20.0) which I want to rotate about point a(0.0, 0.0, 10.0) in XZ plane. What is the simplest way to do it? I am using Qt with QVector3D and QMatrix4x4 to perform transformations. Everything I can think of is something like that:

QVector3D p(0.0, 0.0, 20.0);
QVector3D a(0.0, 0.0, 10.0);
QMatrix4x4 m;

m.translate(-a.x(), -a.y(), -a.z());
p = m*p;

m.setToIdentity();
m.rotate(180, 0.0, 1.0, 0.0);
p = m*p;

m.setToIdentity();
m.translate(a.x(), a.y(), a.z());
p = m*p;

But it seems conspiciously complex to me and I wonder if there are any simpler or more elegant solutions?

like image 210
majaen Avatar asked Aug 13 '10 16:08

majaen


People also ask

What does rotation about a point mean?

A rotation is a transformation in a plane that turns every point of a preimage through a specified angle and direction about a fixed point. The fixed point is called the center of rotation . The amount of rotation is called the angle of rotation and it is measured in degrees.


1 Answers

You can simplify the code by using simple vector subtraction/addition instead of the multiplication with a translation matrix:

QVector3D p(0.0, 0.0, 20.0);
QVector3D a(0.0, 0.0, 10.0);
QMatrix4x4 m;

p-=a;
m.rotate(180, 0.0, 1.0, 0.0);
// 3D vector has no overload for multiplying with a 4x4 matrix directly
p = m*p;
p+=a;
like image 88
Greg S Avatar answered Sep 23 '22 21:09

Greg S