# Rotating a Sphere Not on its Axis

**URL:** <https://discourse.processing.org/t/rotating-a-sphere-not-on-its-axis/8968>\
**Category:** Coding Questions\
**Created:** [March 5, 2019, 1:58pm UTC](https://discourse.processing.org/t/rotating-a-sphere-not-on-its-axis/8968 "2019-03-05T13:58:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![drbme](https://avatars.discourse-cdn.com/v4/letter/d/d26b3c/32.png) [@drbme](https://discourse.processing.org/u/drbme)\
**Post date:** [March 5, 2019, 1:58pm UTC](https://discourse.processing.org/t/rotating-a-sphere-not-on-its-axis/8968/1 "2019-03-05T13:58:52Z")

</div>

I’m trying to rotate a sphere around another sphere using PShapes, translate and rotate, but I can’t figure out how to do it since the sphere automatically draws at 0,0,0 and as such I can only get it to rotate around its axis not some other point.

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [March 6, 2019, 6:09am UTC](https://discourse.processing.org/t/rotating-a-sphere-not-on-its-axis/8968/2 "2019-03-06T06:09:43Z")

</div>

basically you need a

- static translate ( like to the middle of screen )
- make the fix sphere
- dynamic translate ( with rotation math )
- make the moving sphere

+++ make something that you can look at it from all sides…  
so the 3D gets visible / usable

without it you could just use 2 circles moving ( in 2D )

```auto
float er = 40,ang,speed=0.02;

void setup() {
  size(200,200,P3D);
  noStroke();
  info_print();
}

void draw() {
 background(0,0,80);
 PTZ();
}

void draw_object() { // _________________ called by / from inside PTZ
 fill(200,200,0); // yellow
 sphere(20); // sun
 translate(er*sin(ang),er*cos(ang));
 fill(0,0,200); // blue
 sphere(5); // earth
 ang+=speed;                      
}

// _____________________________________ PTZ tab
int mode = 0;
float Zmag = 2;
int Zaxis=-10;                                                       
float Xmag, Ymag = 0;
float newXmag, newYmag = 0; 
int newZmag = 0;
int zoomf = 3;
float newxpos, newypos = 0; // for PAN
float xposd, yposd = 0; // for PAN

// _________________________________________________________________ ROTATE / TILDE and MOVE / PAN
void mousePressed() {
  if (mouseButton == LEFT) { 
    mode=1;
  } // ORBIT
  else if (mouseButton == RIGHT) { 
    mode=2;
  } // PAN
  // else if (mouseButton == CENTER) { mode=3; } // zoom mouse wheel works same , presssed or not
}

// _________________________________________________________________ mouse PT end
void mouseReleased() { 
  mode = 0;
}

// _________________________________________________________________ mouseWheel ZOOM
void mouseWheel(MouseEvent event) {
  int newZmag = event.getCount(); // +- 1
  //if (Zmag > 10) { Zmag += newZmag * 5; } else { Zmag += newZmag; }// from 1 to 11 go in step 1 else in step 5 
  Zmag += newZmag*0.1;
  //println("Zmag: "+nf(Zmag, 1, 2));
}

void keyPressed() {
  if ( keyCode == UP ) Ymag -= 0.1 ;
  if ( keyCode == DOWN ) Ymag += 0.1 ;
  if ( keyCode == RIGHT) Xmag -= 0.1 ;
  if ( keyCode == LEFT ) Xmag += 0.1 ;
  if ( keyCode == 16 ) Zmag -= 0.1 ;
  if ( keyCode == 11 ) Zmag += 0.1 ;
  //println("key: "+key); println("keyCode: "+keyCode);
}
// _________________________________________________________________ Pan Tilde Zoom
void PTZ() {
  pushMatrix(); 
  translate(width/2, height/2, Zaxis);
  // get new mouse operation  
  if ( mode == 2 ) { // PAN ( right mouse button pressed)
    xposd = (mouseX-float(width/2));
    yposd = (mouseY-float(height/2));
  }  
  newxpos = xposd;// xposd=0;
  newypos = yposd;// yposd = 0; 
  translate(newxpos, newypos, 0); // move object
  if ( mode == 1 ) { // ORBIT ( left mouse button pressed)
    newXmag = mouseX/float(width) * TWO_PI;
    newYmag = mouseY/float(height) * TWO_PI;

    float diff = Xmag-newXmag;
    if (abs(diff) > 0.01) Xmag -= diff/4.0;
    diff = Ymag-newYmag;
    if (abs(diff) > 0.01) Ymag -= diff/4.0;
  }
  rotateX(-Ymag);   
  rotateY(-Xmag);   
  scale(Zmag);
  draw_object(); // THE OBJECT see main tab
  popMatrix();
}

// _______________________________________________ SETUP PRINT INFO
void info_print() {
  println("PTZ info:");
  println("key UP DOWN RIGHT LEFT -> rotate // key PAGE UP DOWN -> zoom");
  println("mouse LEFT press drag up down right left -> rotate // mouse RIGHT press -> move // mouse WHEEL turn -> zoom");
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 7, 2019, 10:54pm UTC](https://discourse.processing.org/t/rotating-a-sphere-not-on-its-axis/8968/3 "2019-03-07T22:54:03Z")

</div>

Note that if you need rotation of de-centered objects around a central point, the PeasyCam library is ideal for this.

[http://mrfeinberg.com/peasycam/](http://mrfeinberg.com/peasycam/)

This example is based on the demo peasycam sketch, but with two spheres instead of two boxes.

```auto
import peasy.*;

PeasyCam cam;

void setup() {
  size(200, 200, P3D);
  cam = new PeasyCam(this, 100);
  cam.setMinimumDistance(50);
  cam.setMaximumDistance(500);
}

void draw() {
  background(0);
  rotateX(-.5);
  rotateY(-.5);
  fill(255, 0, 0);
  sphere(30);
  translate(0, 0, 30 + 15);
  fill(0, 0, 255);
  sphere(15);
}

```

![TwoSphere](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/4/47c7f2cdab597538c873afcde08c9e90dba39339.png)
