Over shoulder camera

Posted By: Altarius

Over shoulder camera - 01/01/11 06:30

Hi,

Anyone know how to modify this code for change the position of the camera relatively to player?

camera.x = my.x + fcos((camera.pan),cam_focus);
camera.y = my.y + fsin((camera.pan),cam_focus);
camera.z = my.z+60 + fsin(camera.tilt,cam_dist);

I want to make a 3rd person camera 'over shoulder' like in Gear of war, Spinter Cell...ect , where's the camera are placed a little from right of the player.

I tried to add a value (-35) like that:

camera.y = my.y - 35 + fsin((camera.pan),cam_focus);

But now the camera dont pan correctly whit the player model...

I know there's other way to code a camera like that but i want too know if its possible to just add something to this code for get it work.

Thx
Posted By: Rackscha

Re: Over shoulder camera - 01/01/11 09:14

okay, youre just right here laugh

to calculate an offset(iam just doing the offset, bobing/shaking) will be yours ;))
we will simply set an offset and rotate it. here we go:

Code:
VECTOR Offset;

function DoCam()
{
  Offset.x = -50;//50 units behind
  Offset.y = 10; //10 units to the right
  Offset.z = 50;//50 units up
  vec_rotate(Offset, my.pan);
  //now you can simply add this rotated offset to the player position to get
  //the offset point. Your code should work if you do this(hopefully):
  camera.x = my.x + Offset.x + fcos((camera.pan),cam_focus);
  camera.y = my.y + Offset.y + fsin((camera.pan),cam_focus);
  camera.z = my.z+6 Offset.z + fsin(camera.tilt,cam_dist);
}



Ofcourse you have to adjust my offset values to fit your setting wink.

Hope this works, havent tried, just rewritten from Memories.

If somethings not working, fell free to ask.

Greets
Rackscha
Posted By: SQS

Re: Over shoulder camera - 01/01/11 19:42

I programmed a camera like this a few days ago, but I deleted the file... so it's not tested, sry ^^

Code:
var distance = 100; //distance between the player and the camera

function players_camera()
{
  camera.x = my.x - distance*cos(20+my.pan);
  camera.y = my.y - distance*sin(20+my.pan);
  camera.z = my.z;
}



The idea was to change the angle used for the position to move the camera around the player a little bit, til it's on the right side of him.
But this is A7 and when I look at your codes it seems there is no "normal" sinus-Function anymore... grin Maybe you have to change it a little bit, shouldn't be a problem, if you understand my explanation although it is not good xD (if not, feel free to ask)
Just play with the 100 and 20 til the position is ok.
Posted By: MasterQ32

Re: Over shoulder camera - 01/01/11 19:59

Code:
vec_set(temp,vector(-256,-32,64));
		vec_rotate(temp,vector(camera.pan,cam_angle,0));
		vec_add(temp,my.x);
		vec_set(camera.x,temp);
		
		if(c_trace(my.x,camera.x,IGNORE_MODELS | IGNORE_PASSABLE | IGNORE_PASSENTS | IGNORE_ME))
		{
			vec_set(camera.x,target);
			vec_add(camera.x,normal);
		}



maybe this is what you need
i used it in a GTA "clone" grin
Posted By: Theil

Re: Over shoulder camera - 01/05/11 19:53

Try something like this:

//Basic player movement
vec_set(temp.x, my.x);
temp.z -= 10000;
temp.z = -c_trace (my.x, temp.x, IGNORE_ME | IGNORE_PASSABLE | USE_BOX) - 2;
temp.x = 50 * (key_w - key_s) * time_step;
temp.y = 50 * (key_a - key_d) * 0.6 * time_step;
c_move (my, temp.x, nullvector, IGNORE_PASSABLE | GLIDE | USE_BOX);

my.pan -= 50 * mouse_force.x * time_step;
camera.pan -= 50 * mouse_force.x * time_step;
camera.tilt += mouse_force.y * 8 * time_step;
camera.tilt = clamp(camera.tilt,-20,40);//30.10
cam_z -= mouse_force.y * 8 * time_step;
cam_z = clamp(cam_z,5,60);//30.10

//cam_z for moving the camera up and down when looking with the mouse
//-106 the distance from the cam to the player
//-16 position the camera on the right side, left side should be 16
//play around with this values
vec_set(temp2,vector(-106,-16,cam_z));
vec_rotate(temp2,vector(camera.pan,10,0));
vec_add(temp2,my.x);
vec_set(camera.x,temp2);


if(c_trace(my.x,camera.x,IGNORE_MODELS | IGNORE_PASSABLE | IGNORE_PASSENTS | IGNORE_ME))
{
vec_set(camera.x,target);
vec_add(camera.x,normal);
}

Edit: I already tested the code and it worked, take a look and tell me if this is what you need.


Posted By: painkiller

Re: Over shoulder camera - 01/05/11 20:55

You can also use this one:
Code:
var camera_tilt=0;
function camera_3rdperson(ENTITY* ent,VECTOR* offset)
{
        var factor=1;
	VECTOR camera_x;
	if (!ent) return;
	vec_set(camera_x,offset);
	vec_rotate(camera_x,vector(ent.pan, camera_tilt+ent.tilt, 0));
	vec_add(camera_x,ent.x);
	vec_set(camera.x,camera_x);
	vec_set(camera.pan,vector(ent.pan,ent.tilt*factor+camera_tilt*factor,ent.roll*factor));
}



you have to call it in a loop:

Code:
while(1)
{
		camera_tilt+= mouse_force.y;
		camera_person(player,vector(-tcamera_dist,tcamera_offset_y,tcamera_height),1);

wait(1);
}


Posted By: CHaP

Re: Over shoulder camera - 01/06/11 14:23

Oh great. This thread is exactly what I need.
I will add a offset at my camera too but there is no clue for me how I can make this done.

So, here it is:
(it is a standart 3rd person view)

Code:
//////////////////////////////////////////////////////////////////////////////////////////////
void CAMERA_calculate()
{
	while(1)
	{
		cameraContainer.currentDistance = (cos(cameraContainer.eulerTilt) * cameraContainer.totalDistance);
		camera.x = (cameraContainer.pivotX  - cos(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.y = (cameraContainer.pivotY - sin(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.z = (cameraContainer.pivotZ  + sin(cameraContainer.eulerTilt) * cameraContainer.totalDistance);
		camera.pan = cameraContainer.eulerPan;
		camera.tilt = -(cameraContainer.eulerTilt);
		camera.roll = 0;
		wait(1);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////



The cameraContainer contains all the informations about the player like xyz and pan/tilt.

It would be nice if anybody can help me and Altarius, feel free to copy this code and use it for yourself (if it works of course) grin
Posted By: Rackscha

Re: Over shoulder camera - 01/06/11 19:31

Set an offset, look at my code at the top wink
Posted By: CHaP

Re: Over shoulder camera - 01/06/11 20:13

@Rackscha: Yap, this is what I have tried at first. But the offset works not correctly. If I rotate the player, the camera will move around in a kind of ellipse.

Code:
//////////////////////////////////////////////////////////////////////////////////////////////
void CAMERA_calculate()
{
	VECTOR vecOffset;
	while(1)
	{
		vecOffset.x = 0;
  		vecOffset.y = 50;
  		vecOffset.z = 0;
  		vec_rotate(vecOffset,cameraContainer.eulerPan);
  		cameraContainer.currentDistance = (cos(cameraContainer.eulerTilt) * cameraContainer.totalDistance);
		camera.x = (cameraContainer.pivotX + vecOffset.x - cos(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.y = (cameraContainer.pivotY + vecOffset.y - sin(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.z = (cameraContainer.pivotZ + vecOffset.z + sin(cameraContainer.eulerTilt) * cameraContainer.totalDistance);

		// also this
		camera.x = (cameraContainer.pivotX + vecOffset.x + fcos(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.y = (cameraContainer.pivotY + vecOffset.y + fsin(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.z = (cameraContainer.pivotZ + vecOffset.z + fsin(cameraContainer.eulerTilt) * cameraContainer.totalDistance);


		camera.pan = cameraContainer.eulerPan;
		camera.tilt = -(cameraContainer.eulerTilt);
		camera.roll = 0;
		wait(1);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////



maybe wrong place?!
Posted By: Rackscha

Re: Over shoulder camera - 01/06/11 20:57

yust do this small steps:

1) rotate the offset by the PLAYER angle.
2) add the player position to the offset.
3) the result of 2) is the neede camera position
4) your camera is now at its perfect position, yust give it the same pan/tilt as the player
Posted By: CHaP

Re: Over shoulder camera - 01/09/11 01:26

I got it!

Code:
//////////////////////////////////////////////////////////////////////////////////////////////
void CAMERA_calculate()
{
	
	VECTOR vecOffset;
	while(1)
	{
		vecOffset.x = 0;
		vecOffset.y = 30;
		vecOffset.z = 5;
  		vec_rotate(vecOffset.x,cameraContainer.eulerPan);
		cameraContainer.currentDistance = (cos(cameraContainer.eulerTilt) * cameraContainer.totalDistance);
		camera.x = (cameraContainer.pivotX - cos(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.y = (cameraContainer.pivotY - sin(cameraContainer.eulerPan) * cameraContainer.currentDistance);
		camera.z = (cameraContainer.pivotZ + sin(cameraContainer.eulerTilt) * cameraContainer.totalDistance);
		vec_add(camera.x,vecOffset.x);
		camera.pan = cameraContainer.eulerPan;
		camera.tilt = -(cameraContainer.eulerTilt);
		camera.roll = 0;
		wait(1);
	}
}
//////////////////////////////////////////////////////////////////////////////////////////////



Thx @Rackscha for your help and thx @Altarius for the thread! laugh
© 2024 lite-C Forums