4

My plan:

1. Calculate mouse direction [x, y] [success]

I my Mouse Move event:

int directionX = lastPosition.x - position.x;
int directionY = lastPosition.y - position.y;

2. Calculate angles [theta, phi] [success]

float theta = fmod(lastTheta + sensibility * directionY, M_PI);
float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);

Edit {

bug fix:

float theta = lastTheta + sensibility * directionY * -1;
if (theta < M_PI / -2)theta = M_PI / -2;
else if (theta > M_PI / 2)theta = M_PI / 2;

float phi = fmod(lastPhi + sensibility * directionX * -1, M_PI * 2);

}

Now I have given theta, phi, the centerpoint and the radius and I want to calculate the position and the rotation [that the camera look at the centerpoint]

3. Calculate position coordinates [X,Y,Z] [failed]

float newX = radius * sin(phi) * cos(theta);
float newY = radius * sin(phi) * sin(theta);
float newZ = radius * cos(phi);

Solution [by meowgoesthedog]:

float newX = radius * cos(theta) * cos(phi);
float newY = radius * sin(theta);
float newZ = radius * cos(theta) * sin(phi);

4. Calculate rotation [failed]

float pitch = ?;
float yaw = ?;

Solution [by meowgoesthedog]:

float pitch = -theta;
float yaw = -phi;

Thanks for your solutions!

Max Mister
  • 129
  • 9

1 Answers1

2

Your attempt was almost (kinda) correct:

  • As the diagram shows, in OpenGL the "vertical" direction is conventionally taken to be Y, whereas your formulas assume it is Z

  • phi and theta are in the wrong order

  • Very simple conversion: yaw = -phi, pitch = -theta (from the perspective of the camera)

Fixed formulas:

float position_X = radius * cos(theta) * cos(phi);
float position_Y = radius * sin(theta);
float position_Z = radius * cos(theta) * sin(phi);

(There may also be some sign issues with the mouse deltas but they should be easy to fix.)

meowgoesthedog
  • 14,670
  • 4
  • 27
  • 40
  • Thanks for your answer. pitch and yaw work, but the position doesn't, before the X axis worked, but now neither axis works. – Max Mister May 11 '18 at 19:36
  • @maxmister apologies - ironically I made the very mistake that I pointed out. Fixed – meowgoesthedog May 11 '18 at 20:20
  • Thanks, now works x and z, but y works only to 70%, because if theta changes between 3.14159 and 3.13159, the model makes a huge step – Max Mister May 12 '18 at 18:48
  • @MaxMister `theta` (the angle to the XZ plane) is defined within the range `[-pi/2, pi/2]`. You are exceeding its range. (`phi` is in `[0, 2pi]`). – meowgoesthedog May 12 '18 at 18:55