How to rotate around a sphere?

add this to camx and camy

1 Like

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

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

@raboytniksings An example for you using TAU (same as TWO_PI):

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

:)

1 Like

Hello,

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

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!

:)

1 Like

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.

1 Like

I always write it with line breaks

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

Here is an example


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();
}