3D Camera Rotation Help

edited October 2013 in Questions about Code

I want to use some of the 3D features of processing, so I have been messing around with it a bit. I am having trouble with rotation of the camera around an object. The camera is supposed to rotate at a rate of the distance the mouse is from the center of the window. It doesn't. Can someone please explain why this is and/or show me if there is an error? Thanks.

float playerX = 0;
float playerY = 0;
float playerZ = 0;
float cameraxAngle = 0;
float camerayAngle = 0;
float camX;
float camY;
float camZ;
int radius = 256;


void setup() {
  size(1080,720,P3D);
}

void draw() {
  camX = radius * sin(radians(cameraxAngle)) * cos(radians(camerayAngle));
  camY = radius * sin(radians(cameraxAngle)) * sin(radians(camerayAngle));
  camZ = radius * cos(radians(cameraxAngle));

  background(0);

  camera(camX,camY,camZ,playerX,playerY,playerZ,0,1,0);

  fill(255,0,0);
  translate(playerX,playerY,playerZ);
  box(64);

  cameraxAngle = cameraxAngle+(mouseX-width/2)/128;
  camerayAngle = camerayAngle+(mouseY-height/2)/128;
}

Answers

  • edited October 2013

    Dunno anything 'bout trigs. However, I feel you should use map() to transform mouse coords. into angles or something? :-&

    cameraxAngle += map(mouseX, 0, width, 0, TAU);
    camerayAngle += map(mouseY, 0, height, 0, TAU);
    
  • apart from that: shouldn't you add playerX to camX etc. to make the cam rotate around the player and not somewhere else (around 0 or so)?

  • edited October 2013

    also camY should be just = playerY

    the camZ is ok,

    camX must be like this but only with sin (line too long now!)

  • The camera is supposed to rotate at a rate of the distance the mouse is from the center of the window

    could you please clarify?

    you will probably want some to keep track of elapsed time between frames to make the camera's speed independent of framerate as well.

    take a look at millis()

    Is this more what you're looking for?

    ```java float playerX = 0; float playerY = 0; float playerZ = 0; float cameraxAngle = 0; float camerayAngle = 0; float camX; float camY; float camZ; int radius = 256;

    void setup() { size(1080,720,P3D); }

    void draw() { camX = radius * sin(radians(cameraxAngle)); camY = radius * camerayAngle; camZ = radius * cos(radians(cameraxAngle));

    background(0);

    camera(camX,camY,camZ,playerX,playerY,playerZ,0,1,0);

    fill(255,0,0); translate(playerX,playerY,playerZ); box(64);

    cameraxAngle = cameraxAngle + (millis()/1000000f) * (mouseX-width/2); camerayAngle = (mouseY-height/2) * (PI/720); println(cameraxAngle); } ``` can.I.use.markdown.or.not ? yes : no

  • edited October 2013 Answer ✓
    float playerX = 0;
    float playerY = 0;
    float playerZ = 0;
    float cameraxAngle = 0;
    float camerayAngle = 0;
    float camX;
    float camY;
    float camZ;
    int radius = 256;
    
    void setup() {
      size(1080,720,P3D);
    }
    
    void draw() {
      camX = radius * sin(radians(cameraxAngle));
      camY = radius * camerayAngle;
      camZ = radius * cos(radians(cameraxAngle));
    
      background(0);
    
      camera(camX,camY,camZ,playerX,playerY,playerZ,0,1,0);
    
      fill(255,0,0);
      translate(playerX,playerY,playerZ);
      box(64);
    
    
      cameraxAngle = cameraxAngle + (millis()/1000000f) * (mouseX-width/2);
      camerayAngle = (mouseY-height/2) * (PI/720);
      println(cameraxAngle);
    }
    
  • how do you post a block of code??

  • Highlight it then CTRL+K! (~~)

  • Thanks metameta, your solution works great. I see what I was doing wrong. :D

  • thanks GoToLoop! Zabuza, glad I could help.

Sign In or Register to comment.