# How to rotate object around a point?

Posts: 18

Given a vector as center of rotation, how could I rotate an object around it Y degrees in the y-axis?

• Posts: 473
edited October 2017

Just parent the item to a null object (tool bar or create menu) placed at that vector. Rotating the null rotates the child object.

Post edited by MarcCCTx on
• Posts: 616

This script might help you, to see it working you need to create two spheres, name them, "sun", and "earth", run the script.

``````var Program = (function () {
function Program() {
this.programName = "Orbit";
}
Program.prototype.run = function () {
var sun = Scene.findNodeByLabel("sun");
var earth = Scene.findNodeByLabel("earth");
this.orbit(sun, earth, new DzVec3(0, 1, 0), 45 / 2);
};
Program.prototype.orbit = function (planet, moon, axis, angle) {
var DEG2RAD = Math.PI / 180;
var RAD2DEG = 180 / Math.PI;
beginUndo();
var origin = new DzVec3(0, 0, 0);
var planetPosition = planet.getWSPos();
var moonPosition = moon.getWSPos();
var pointToRotate = origin.subtract(planetPosition.subtract(moonPosition));
var q = new DzQuat(axis, angle * DEG2RAD);
this.applyQuaternion(pointToRotate, q);
var currentRot = moon.getWSRot();
acceptUndo(this.programName);
};
Program.prototype.applyQuaternion = function (v, q) {
var x = v.x;
var y = v.y;
var z = v.z;
var qx = q.x;
var qy = q.y;
var qz = q.z;
var qw = q.w;
var ix = qw * x + qy * z - qz * y;
var iy = qw * y + qz * x - qx * z;
var iz = qw * z + qx * y - qy * x;
var iw = -qx * x - qy * y - qz * z;
v.x = ix * qw + iw * -qx + iy * -qz - iz * -qy;
v.y = iy * qw + iw * -qy + iz * -qx - ix * -qz;
v.z = iz * qw + iw * -qz + ix * -qy - iy * -qx;
return v;
};
return Program;
}());
var program = new Program().run();``````

• Posts: 261
edited December 2017

It seems like you are constructing `a privat ? not `anonymous` function called Orbit` - can I have acces to it from outside like from the main anonymous` function and how do I execute it if so?`

``````var Program = (function () {
function Program() {
this.programName = "Orbit";
}``````

This seems to start an endless loop right?

``var program = new Program().run();``

Post edited by Syrus_Dante on
• Posts: 616

As a matter of fact, all member methods are public. All that is needed to access them is an instance, like:

``````var sun = Scene.findNodeByLabel("sun");
var earth = Scene.findNodeByLabel("earth");

var program = new Program();

program.orbit(sun, earth, new DzVec3(0, 1, 0), 45 / 2);

``````

The `run` method just contains 3 statements, no loop. It was supposed to be a Fluent like method but forgot to return `this`.

HTH

• Posts: 261

Sorry it's my lack of knowledge of this syntax that has led me to believe that this is an endless loop. I'm shure its an insult to any programmer.

• Posts: 616

Actually it is not a syntax, it's called a Design Pattern, a fancy name to help you write S.O.L.I.D. code.