0
votes

My probleme today is I want that an object turn around an other. Like the earth turn around the sun. I programm with Java.

I have my camera as the referential point (The sun), and on object that need to turn around this referential point (The earth).

I know :

  • The distance between the 2 objects : d
  • The position of the camera : pCam
  • The position of the object : pObj
  • The angle that I need to rotate : a

I want to find the new position of the object after it rotate of the a angle around the referential position pCam in a distance d.

Can you help me ?

1
what have you tried so far? This is example of very basic use of 3D transform matrices so if you google a bit then you should find tons of examples and tutorials. Hint: (combine: translate center of rotation to (0,0,0), apply rotation by a, translate back)Spektre

1 Answers

0
votes

Okay, so I resolve my problem,

I use this function to rotate a point around an other with an angle (In degrees) :

public SimpleVector rotateAround(SimpleVector center, SimpleVector point, float angle) {
    SimpleVector newPos = new SimpleVector(point.x - center.x, 0, point.z - center.z);

    newPos.x = (float) (newPos.x * Math.cos(angle) - newPos.z * Math.sin(angle));
    newPos.z = (float) (newPos.x * Math.sin(angle) + newPos.z * Math.cos(angle));

    newPos.x += center.x;
    newPos.z += center.z;

    return newPos;
}

And to memorise the position of the object, I use this snippet of code :

SimpleVector diff = this.getPosition().calcSub(ref);
SimpleVector s = rotateAround(center, ref, (float) angle);
s.x += diff.x;
// Rotation only on a plane (2 axis)
s.y = this.getPosition().y;
s.z += diff.z;
this.positioningObject(s);