Rotate point around point

Hello,

I want to rotate a point (Vector3f) around another point (Vector3f). The problem is that i don’t want to rotate around the x,y or z-axis but the axis shown in the picture. The axis is defined by the Position of the points. P1, P2 and the angle are known. I want to calculate P1’.
Are there methods i can use to do this?

1 Like

Check out the SceneGraph for Dummies docmentation in the header. The solution is explained in the slides.

You want to put P1 in a node, offset this node to the location of P2 and then rotate the node.

You mean something like this?

Here is the rotation function:

rotate_dragon (ang) = { 
  
  var radius1 = 3
  var ang_rad = 3.14/180*ang
  var p1_x = p2_x+cos(ang_rad)*radius1
  var p1_y = p2_y+sin(ang_rad)*radius1
  var p1_z = p2_z
  
  p1.move to (p1_x,p1_y,p1_z) in 0.1 seconds
  run rotate_dragon(ang+3) async
}

Above is when you want rotate location only.

If you want rotate it fully. Then do like @remy_vd said, put Node(child) inside Node(parent)

and just rotate Node(parent), see example below:

you can get current angle rotations by:

rotation = #Node(parent)#quaternion.toAngles(null);

and then you can adjust specific angle rotation like:

            rotation[0] += moveDirection.x;
            rotation[1] += moveDirection.y; // use only one of them
            rotation[2] += moveDirection.z;
            #Node(parent)#quaternion = new Quaternion().fromAngles(rotation);

legend:

#Node(parent)#quaternion → Node rotation quaternion

1 Like

So , what would happen if :

Vector3f axis =new Vector3f(2,3,4);
Obj.rotate(new Quaternion().fromAngleAxis(axis.normalize(),Math.PI));
/*Your point that is rotated */
Vector3f point = Obj.getLocalTranslation();

Actually I havenot yet tried it :slightly_smiling_face: , I have used predefined values as in wiki UNIT_X , UNIT_Y, UNIT_Z which are actually normalized vectors that are coincident on their corresponding axes , so I think it would be the same

Thanks for the fast answer. But this will not solve my problem because you dont’t change p1_z. So you turn round the z-Axis. The axis i need to turn around depends on the relation of the to points (look at my jpg). So x,y and z of the point have to be changed.

Sorry, i don’t know how this solves my Problem. When i rotate a node i can only rotate it around x, y, or z-axis. But the rotation i need goes around another axis (see jpg).

Hello,

my problem is that i have to calculate the axis i’m rotating the Point around (see jpg)

Thanks for your answer but that doesn’t solve my problem (see jpg).

Hi ,
How do you use points not vectors ?
I cannot understand your question , if you are using

Vector2f pointVec=new Vector2f(P1,P2);

You must specify the 2 points of the axis at the same time , the axis is initialized or you will get a NPE

EDIT : if P1 represents a Spatial then just do

Vector3f P1 = spatial.getLocalTranslation(); 

//OR 
//In case if you added it to physical space 
Vector3f P1=rigidBody.getPhysicsLocation();

After your rotation procedure

Example:
float p1x = (float) random();
float p1y = (float) random();
float p1z = (float) random();
float p2x = (float) random();
float p2y = (float) random();
float p2z = (float) random();
float angle = 0.05f;

    Vector3f p1 = new Vector3f(p1x, p1y, p1z);
    Vector3f p2 = new Vector3f(p2x, p2y, p2z);
    
    Vector3f p1rotated = new Vector3f();
    
    p1rotated = ???????????????

(Keep in mind: i want to rotate around the axis shown in my jpg)

you said my solution does not work, but you didnt said why.

imo it will work, but you might be missing some information like:

  • use FastMath.DEG_TO_RAD
  • use getWorldTranslation() not getLocalTranslation() for x,y,z of global position, or local for local related to origin point.

you can even just use transform.combineWithParent(transform2); to skip “Node” part.

so, idk what to say more.

There is some way to build a node hierarchy to do what you want. It may require one more node than you think but it can definitely be done (I think it’s mathematically provable).

Make a node that’s positioned where you want it. Rotate it so that one of it’s axes aligns with the one you want to rotate around. Add another child and rotate that one around that axis.

1 Like