# How to rotate around a sphere?

**URL:** <https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072>\
**Category:** Coding Questions\
**Created:** [October 31, 2020, 10:17am UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072 "2020-10-31T10:17:21Z")\
**Posts on this page:** 5\
**Page:** 2

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [October 31, 2020, 9:47pm UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072/22 "2020-10-31T21:47:50Z")

</div>

> [@raboytniksings](#):
>
> `328, 43, `

add this to camx and camy

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 31, 2020, 11:21pm UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072/23 "2020-10-31T23:21:58Z")

</div>

> [@Chrisir](#):
>
> The angle is in radians
> 
> 0…2 Pi
> 
> Use map() to get this angle.
> 
> frameCount doesn‘t do the job
> 
> You can say : radians(angle)
> 
> angle += 1; // in degree

map() is not necessary if you work directly with radians.  
frameCount() works just fine.  
You can work directly in radians.  
frameCount is already counting.

Simple one line solution for 1 deg increments:

`float angle = frameCount* (TAU/360); // 1 deg = TAU/360 rad = TWO_PI/360 rad`

> [@raboytniksings](#):
>
> Why wouldn’t frameCount do the job?

It does the job! I use it all the time.

@raboytniksings An example for you using TAU (same as TWO\_PI):

```auto
void setup() 
	{
  size(500, 500, P3D);
	}

void draw()
  {
  background(0);
  
  float angle = frameCount* (TAU/360);
  
  translate(width/2, height/2, 0);
  rotateY(angle);
  
  sphereDetail(10);
  noFill();
  stroke(256, 128, 0);
  sphere(100);
  }

```

Reference:  
_[TAU / Reference / Processing.org](https://processing.org/reference/TAU.html)_

`:)`

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [October 31, 2020, 11:50pm UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072/24 "2020-10-31T23:50:54Z")

</div>

Hello,

I just did a bit of cleanup and did not make the final tweaks:

```auto
void setup() 
	{
  size(600, 600, P3D);
  textAlign(CENTER, CENTER); //Centers text
  textSize(24); //Much cleaner text output
	}

void draw() {
  background(0);
  lights();
  
  float angle = frameCount*TAU/360;
  
  float cam_x = 350 * cos(angle);
  float cam_z = 350 * sin(angle);
  camera(cam_x, 43, cam_z, 128, 43, 0, 0, 1, 0);

  //fill(128, 0, 128);
  noFill();
  stroke(128, 0, 128);
  //lights();
 
 push();  
  translate(128, 43, 0);
  sphereDetail(10);
  sphere(100);
  fill(255, 255, 0);
  text("S1", 0, 0); //Center of sphere
 pop(); 

  text("S2", 128, 43, 0); //Center of sphere Same as S1!
  
  fill(255, 0, 0);
  //text("0, 0, 00000000000000000000000", 0, 0);
  text("O", 0, 0, 0); //(0, 0, 0) co-ordinates
  }

```

Added push() and pop() so you can compare coordinates.  
Some comments in code.

You are well on your way to completing this!

`:)`

---

<div class="post-metadata">

**Author:** ![raboytniksings](https://avatars.discourse-cdn.com/v4/letter/r/c57346/32.png) [@raboytniksings](https://discourse.processing.org/u/raboytniksings)\
**Post date:** [November 1, 2020, 10:20am UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072/25 "2020-11-01T10:20:55Z")

</div>

Thanks a lot to both of you. I was indeed missing an argument in the x position of the camera. It works as expected now.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 1, 2020, 10:21am UTC](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072/26 "2020-11-01T10:21:25Z")

</div>

> [@glv](#):
>
> `camera(cam_x, 43, cam_z, 128, 43, 0, 0, 1, 0);`

I always write it with line breaks

```auto
camera(cam_x, 43, cam_z, // cam pos 
 128, 43, 0, // cam lookAt
  0, 1, 0);

```

* * *

**Here is an example**

```auto

float angleCam = 0; 
float angleBox = 0; 

float camRadius = 350; 
float camRelativeHeightAboveBox = 76; 

float box_x=500;
float box_y=400;
float box_z=-200;

void setup() {
  size(1600, 600, P3D);
}

void draw() {
  background(0);
  lights();

  float cam_x = camRadius * cos(angleCam) + box_x;
  float cam_y = box_y - camRelativeHeightAboveBox; 
  float cam_z = camRadius * sin(angleCam) + box_z;

  camera(cam_x, cam_y, cam_z, // pos cam
    box_x, box_y, box_z, // lookAt cam 
    0, 1, 0); // UP 

  showBox();

  angleCam += 0.01411;
}

void showBox() {
  // green box
  pushMatrix(); 
  translate (box_x, box_y, box_z); 
  rotateY(angleBox);

  stroke(0); 
  fill(0, 255, 0); //green
  box(21);

  angleBox-= 0.0401411;
  popMatrix();

  //------------------------------------------------

  // red sphere 
  pushMatrix(); 
  translate (box_x+133, box_y, box_z);
  noStroke();
  fill(255, 0, 0); //red
  sphere(9);
  popMatrix();
}

```

[Previous page](https://discourse.processing.org/t/how-to-rotate-around-a-sphere/25072.md?page=1)
