🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

3rd cam player position

Started by
15 comments, last by HighCode 4 years, 3 months ago

Hello everyone.

I have my camera in third person, as shown below.

As you can see, the player is in the center, covering the crosshairs and making it difficult.

How do I move the player to the side as shown in the image below (there I used a free camera to demonstrate), such as the GTA 5 camera.

if I add a value to the x axis of the player model it even works. But if I keep turning the mouse to look around, start and unconfigure the camera:

glm::vec3 pos = camera.Front;
pos.y = 0;
pos.x += -0.06f; //to keep left

model = glm::translate(model, pos);

My Front is Position + glm::vec3(0.0, 0.0, -0.36f); and viewMatrix is glm::lookAt(Position, Front, Up);

My mouse movement:

void updateCameraVectors()
	{
		POINT mousePos;
		int mid_x = wndWidth >> 1;
		int mid_y = wndHeight >> 1;
		float angle_y = 0.0f;
		float angle_z = 0.0f;

		GetCursorPos(&mousePos);	// Get the mouse cursor 2D x,y position					

		if ((mousePos.x == mid_x) && (mousePos.y == mid_y)) return;

		SetCursorPos(mid_x, mid_y);	// Set the mouse cursor in the center of the window						

		// Get the direction from the mouse cursor, set a resonable maneuvering speed
		angle_y = (float)((mid_x - mousePos.x)) / 1000;
		angle_z = (float)((mid_y - mousePos.y)) / 5000;

		Front.y += angle_z * 2;

		// limit o mouse olhando para cima e para baixo
		if (Front.y > 0.26f) {
			Front.y = 0.26f;
		}
		if (Front.y < -0.05f) {
			Front.y = -0.05f;
		}
		
		glm::vec3 vVector = Position - Front;
		float speed = angle_y;
		
		playerYaw += speed;
		
		if (rotateAround == 1) {
			playerYaw = oldPlayerYaw;
		}
		else {
			oldPlayerYaw = playerYaw;
		}
		
		Position.z = (float)(Front.z + sin(-speed)*vVector.x + cos(-speed)*vVector.z);
		Position.x = (float)(Front.x + cos(-speed)*vVector.x - sin(-speed)*vVector.z);
	}

can someone help me ?

Tank you

Advertisement

Im a bit drunk (i dont drink at all but today huh) right now so i wont provide you a full solution for that, but first of all you can't use if statements for such things.

You are better using all alone vectors for that imagine a player position named ppos you have a direction at which player is facing named pfront.

Say the camera is slightly higher than player so it is somewhat situated ppos-pfrontdstfromcamtoplayer+vectorup_normalized * some quantinity now i played only first gta so i dont know what type of aim is what you need but you should just need now to move a bit right by some factor (so having pfront and up vector you comoute right vector)

And just adjust it by some value which depends on dst from player to camera value and this is what i wont tell you right now

_WeirdCat_ said:

Im a bit drunk (i dont drink at all but today huh)

HAHAHAHAHAHA, are you celebrating something ?

_WeirdCat_ said:

ppos-pfrontdstfromcamtoplayer+vectorup_normalized * some quantinity

is this part I need to know :D

Look this image:

when you rotate the mouse to rotate the camera, it rotates around the model of the player, in my case it is rotating in the center, as in the image below:

if I add Front.x - = -0.06f. The camera loses all viewing angle and position.

The camera should be parented to the player and properly positioned relative to the player.

This is a good video for camera movement. I don't know if it is useful to you but you would probably enjoy it nonetheless.

50 Game Camera Mistakes

https://www.youtube.com/watch?v=C7307qRmlMI

🙂🙂🙂🙂🙂<←The tone posse, ready for action.

well basically you do following simple things…

you have ur camera position which changes with mouse …keep it that way…

now what you do is find the right or left direction vector of camera from its view matrix … normalized…

set the position of character model at camera position..

then set a suitable magnitude to right or left vector u obtained from view matrix and add it to the character's position.

allow the player to strafe and set the appropriate offset , left or right depending upon in which direction player is strafing…

you can do opposite also that is by offsetting camera positions instead of character and make character a pivot point…

solved…

i think this is the right Vector3(-_11, -_12, -_13);

Hello guys, thank u for replys…all right, i understood ( or not hahahha) but…

Enzio599 said:

then set a suitable magnitude to right or left vector u obtained from view matrix and add it to the character's position.

……………….

i think this is the right Vector3(-_11, -_12, -_13);

Can u help me with code ?

I was a little confused with the "right", I used it as an example but the camera started to roll in the "roll"

this is a common view matrix … r is right , u is up and d is lookat…

Did not help.

But thanks.

I will look for another way.

HighCode said:

Did not help.

But thanks.

I will look for another way.

my approach should work,

i assume you already know basic vector maths like, “how to get a vector of desired length”, also assume you can retrieve a row of matrix in the form of vector …

if my assumptions are correct then i am eager to know ur another way around… do post it here how you plan to do it…

even a pseudo code would be ok to understand what u r doing…

Enzio599 said:

HighCode said:

Did not help.

But thanks.

I will look for another way.

my approach should work,

i assume you already know basic vector maths like, “how to get a vector of desired length”, also assume you can retrieve a row of matrix in the form of vector …

if my assumptions are correct then i am eager to know ur another way around… do post it here how you plan to do it…

even a pseudo code would be ok to understand what u r doing…

I used the R values, but the camera got "gelatinous" effects.

Maybe I'm doing it wrong. But I just wanted to put the player's model a little more to the left.

glm::mat4 viewMatrix = glm::lookAt(Position, Front, Up);
viewMatrix[0][0] = -0.05f;
/*
viewMatrix[0][0] = -viewMatrix[0][0];
viewMatrix[0][1] = -viewMatrix[0][1];
viewMatrix[0][2] = -viewMatrix[0][2];
*/
		
return viewMatrix;

I tried several ways, but it always had strange effects on the camera.

This topic is closed to new replies.

Advertisement