Rotating Camera

Started by
3 comments, last by _WeirdCat_ 7 years, 9 months ago

Hello!

I am have been reading quite a lot lately on how to rotate the camera around a point (my character) lately but I don't really understand the tutorials online because most of them use their own concepts or different techniques and so on.

So, currently when a user drags his finger across the screen I am registering the distance (deltaX). Now I need to rotate the camera around the Y-Axis accordingly to the maginitude of deltaX. I am using a right-hand coordinate system, so the X-Axis is pointing to the left, the Y-Axis is pointing straight up and the Z-Axis is pointing straight out of the screen. I also have three vectors that describe the orientation of the camera and they are listed below.

eye = new Vector3(0, 1.2f, 0); //The camera's position
up = new Vector3(0, 1, 0); // Well the up vector
center = new Vector3(0, 1, -2); //The look at point.

Now I am not looking for somebody to do the work for me but rather explain how to rotate the camera properly around the Y-Axis. All I have is the deltaX from the user input and theese three vectors that make up the ViewMatrix which I then later on use to transform the objects in the world.

Regards!

Advertisement
You talk about rotating around your character, but you then don't include the character anywhere in the details of your problem. If you mean to rotate the camera without displacing it, you only need to rotate the center point around the eye point. If the character and the camera are not at the same point (unclear from your post), you'll need to rotate both `center' and `eye' around the character.

In order to rotate a point around another point, first apply a translation that makes the center of rotation be the origin, then rotate, then undo the translation.

Point rotate_around_point(Point p, Point center, Rotation rotation) {
  return rotate_around_origin(p - center, rotation) + center;
}

Are you missing anything?

Well incase it was unclear my character is centered around the center point of my camera. So I want to rotate around that point.

So you need to translate to make the camera be at the origin, rotate the "look-at" point and then undo the translation.

Here's some C++ code that does what you want:
#include <iostream>

struct Vector {
  float x, y, z;
  
  Vector(float x, float y, float z) : x(x), y(y), z(z) {
  }
};

struct Point {
  float x, y, z;
  
  Point(float x, float y, float z) : x(x), y(y), z(z) {
  }
};

std::ostream &operator<<(std::ostream &os, Point p) {
  return os << '(' << p.x << ',' << p.y << ',' << p.z << ')';
}

Vector operator-(Point p1, Point p2) {
  return Vector(p1.x-p2.x, p1.y-p2.y, p1.z-p2.z);
}

Point operator+(Point p, Vector v) {
  return Point(p.x+v.x, p.y+v.y, p.z+v.z);
}

// --- Start reading here ---

Vector rotate_about_y_axis(Vector v, float cos, float sin) {
  return Vector(v.x * cos - v.z * sin, v.y, v.x * sin + v.z * cos);
}

Point rotate_about_y_axis_around_center(Point p, float cos, float sin, Point center) {
  return center + rotate_about_y_axis(p - center, cos, sin);
}

int main() {
  Point eye(0.0f, 1.2f, 0.0f);
  Point lookat(0.0f, 1.0f, -2.0f);
  
  float cos = 0.0f; // std::cos(angle)
  float sin = 1.0f; // std::sin(angle)
  
  Point rotated_lookat = rotate_about_y_axis_around_center(lookat, cos, sin, eye);
  std::cout << "lookat after rotation = " << rotated_lookat << '\n';
}

Sorry if im wrong but if rhand sys y is up the equation is like following x = radius*sin(@);
z = radius*cos(@); that gives you a relative vector in direction lets aay wherecamera should look at (point) then you use math to create view mattrix like described in glulookat code if you need more information then reply ;0

This topic is closed to new replies.

Advertisement