Sign in to follow this  
TheFearlessHobbit

Making camera follow object

Recommended Posts

Hey everybody, I recently just setup my camera scene:

Camera Class

class Camera
{
public:
Camera();
~Camera();

glm::mat4 GetViewToWorldMatrix() const;

private:
glm::vec3 m_Position;
glm::vec3 m_ViewDirection;
};

Definition:


Camera::Camera() :
m_ViewDirection(0.0f, 0.0f, -1.0f) // Always look in the negative z 
{}


Camera::~Camera()
{}


glm::mat4 Camera::GetViewToWorldMatrix() const
{
return glm::lookAt(m_Position, m_Position + m_ViewDirection , glm::vec3(0.0f, 1.0f, 0.0f));
}

Obviously by adding the position vector of the camera with the view direction vector, that will return the lookAt. After that I'm simply multiplying the projection matrix with the ViewToWorld matrix and it's working fine, I can see my whole scene. However, I'm trying to make it so that the camera only follows one object in my scene. How can I achieve this?  

 

Thank you for your help.  :)

Edited by TheFearlessHobbit

Share this post


Link to post
Share on other sites

Why of course I tried that :)

 

Ultimately, this is what I tried to do:

extern float PlayerXCoord;
extern float PlayerYCoord;
float PlayerZCoord = -1.0f;
glm::vec3 PlayerPosition(PlayerXCoord, PlayerYCoord, PlayerZCoord);


CCamera::CCamera() :
m_ViewDirection(0.0f, 0.0f, -1.0f),
m_Position(0.0f, 0.0f, 0.0f)
{}


CCamera::~CCamera()
{}


glm::mat4 CCamera::GetViewToWorldMatrix() const
{
return glm::lookAt(m_Position, m_Position + PlayerPosition, glm::vec3(0.0f, 1.0f, 0.0f));
}
Edited by TheFearlessHobbit

Share this post


Link to post
Share on other sites

If you set the aim of the lookAt to the center of the object you want to follow, then this object will definitely be in the center of the screen. If this is not the case (regarding your screenshot, your player is at the right), then you are certainly doing other transformations that pervade your view. Or you might calculate things wrongly (ie the player position).

 

Also you seem to have a 2D game. In this case I would simply use an 2D orthographic projection matrix and set its value so that the player remains in the center. So something similar to this:

glOrtho(player.x-screen.width/2, player.x+screen.width/2, player.y-screen.height/2, player.y+screen.height/2, -1,1);

 

which you can also obtain with using glm.

Share this post


Link to post
Share on other sites

I tried setting up an orthographic projection as you suggested like so:

m_Projection = glm::ortho(PlayerXCoord - 800.0f / 2.0f, PlayerXCoord + 800.0f / 2.0f, PlayerYCoord - 600.0f / 2.0f, PlayerYCoord + 600.0f / 2.0f, -1.0f, 1.0f);

It seems to be working but my scene is so far away from the camera I can hardly see it. It's a mere pixel.  

Share this post


Link to post
Share on other sites

I tried setting up an orthographic projection as you suggested like so:

m_Projection = glm::ortho(PlayerXCoord - 800.0f / 2.0f, PlayerXCoord + 800.0f / 2.0f, PlayerYCoord - 600.0f / 2.0f, PlayerYCoord + 600.0f / 2.0f, -1.0f, 1.0f);

It seems to be working but my scene is so far away from the camera I can hardly see it. It's a mere pixel.  

Are you still using normalized 0 thru 1 coordinates? Try scaling up your objects if that's the case, or try using screen coordinates

Edited by GutenTag

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this