Creating camera view toggle 1st person to 3rd person

Posted By: emo10001

Creating camera view toggle 1st person to 3rd person - 07/02/11 20:54

Hey everyone,

I've been experimenting with what I was hoping would be a simple camera toggle between 3rd person and 1st person point of view. The functionality seems to work. It switches between the 2 views, but after I switch the first time...my pan starts to look shaky and stutters. Any thoughts?

Here's the function:

Code:
function camera_follow(ENTITY* ent)
{	
	var camx = -150;
	var camy = 10;
	var camz = 50;
	var camtilt = -13;
	
//	var camx;
//	var camy;
//	var camz;
//	var camtilt;
		
	while(1)
	{

		if(key_9)
		{
			camx = -150;
			camy = 10;
			camz = 50;
			camtilt = -13;
		}
		if(key_0)
		{
			camx = 5;
			camy = 0;
			camz = 5;
			camtilt = 0;
		}
			
		vec_set(camera.x,vector(camx,camy,camz));  // camera position relative to the player    
		vec_rotate(camera.x,ent.pan); // rotate the camera position with the player
		vec_add(camera.x,ent.x);      // add player position
		vec_set(camera.pan,vector(ent.pan,camtilt,0)); // look in player direction, slighty down 
		wait(1);	
	}
}


Posted By: MrGuest

Re: Creating camera view toggle 1st person to 3rd person - 07/02/11 23:30

at a guess it'll be because you have the function running twice?
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/03/11 04:45

I think I know what you mean, but even if I do it like this it makes no difference:

Code:
function camera_follow(ENTITY* ent)
{		
	var camx;
	var camy;
	var camz;
	var camtilt;
	
	proc_mode = PROC_EARLY;
		
	while(1)
	{
		if(key_9)
		{
			camx = -150;
			camy = 10;
			camz = 50;
			camtilt = -13;
		}
		if(key_0)
		{
			camx = 5;
			camy = 0;
			camz = 5;
			camtilt = 0;
		}
			
		vec_set(camera.x,vector(camx,camy,camz));  // camera position relative to the player    
		vec_rotate(camera.x,ent.pan); // rotate the camera position with the player
		vec_add(camera.x,ent.x);      // add player position
		vec_set(camera.pan,vector(ent.pan,camtilt,0)); // look in player direction, slighty down 
		wait(1);	
	}
}



Is that what you're talking about?
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/04/11 05:32

Well I've made a discovery...the shaky part only happens after I've switched from 3rd person to 1st, and then back to 3rd. Then..at that point...if I switch back to 1st person and then back to 3rd person REALLY fast 1 time....it goes away.

But I'd rather not have to tell my players to do that LOL!

I'm hoping I can get this working...seems like a nice way to do camera perspective switching in just one camera function.

Anybody got anymore ideas?
Posted By: Superku

Re: Creating camera view toggle 1st person to 3rd person - 07/04/11 12:37

Where do you call camera_follow?
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/04/11 14:49

I call it in my player_move() code as:

camera_follow(me);

It's the first line in the player_move() section.
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/06/11 04:45

Any thoughts? I'm kind of stuck... blush
Posted By: Superku

Re: Creating camera view toggle 1st person to 3rd person - 07/06/11 10:45

Use a different key than key_0 because 0 toggles the camera movement that is included in debug.c, or set "on_0 = NULL;" one frame after game start or don't include debug.c at all.
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/07/11 03:57

Thanks for the advice Superku, but that didn't make any difference either. I think the function and the "if then" statement just didn't sit to well together (at least for camera functions). I broke out each camera as it's own function and then call them from another function...and the shaky/jittery camera has stopped.

Here's what I wound up doing:

Code:
function camera_1stperson()
{
	while(1)
	{
	   camera.x = my.x + 11 * cos(my.pan);
	   camera.y = my.y + 11 * sin(my.pan);
	   camera.z = my.z + 25;
	   camera.pan = my.pan;
	   camera.tilt = 0;
	   camera.roll = 0;
	   wait(1);
    }
}

function camera_3rdperson()
{
	while(1)
	{
	   camera.x = my.x - 150 * cos(my.pan);
	   camera.y = my.y - 150 * sin(my.pan);
	   camera.z = my.z + 50;
	   camera.pan = my.pan;
	   camera.tilt = -13;
	   camera.roll = 0;
	   wait(1);
        }
}
function camera_3rdperson2()
{
	while(1)
	{
	   camera.x = my.x - 250 * cos(my.pan);
	   camera.y = my.y - 250 * sin(my.pan);
	   camera.z = my.z + 200;
	   camera.pan = my.pan;
	   camera.tilt = -25;
	   camera.roll = 0;
	   wait(1);
        }
}

function select_cameras()
{
	
	while(1)
	{
		if(key_7)
		{camera_1stperson();}
		if(key_8)
		{camera_3rdperson();}
		if(key_9)
		{camera_3rdperson2();}
		wait(1);
	}
}


Posted By: Superku

Re: Creating camera view toggle 1st person to 3rd person - 07/07/11 11:09

That's not a good solution, every time you press a key, a new while loop starts and the old camera_Xstperson-loops never stop.
If-statements are fine with camera functions, why should they make any problems?

My advice: Return to your old code, use debug-features (don't forget DEBUG_VAR, diag_var, ...), reduce the code to a minimum until you isolate the problem.
Posted By: Anonymous

Re: Creating camera view toggle 1st person to 3rd person - 07/07/11 18:58

This is similar to what we posted in your other post Other cam post
You needed a cam state var. You can 1) tell the loops in each cam to While(cam_var==x). Or 2) remove the loops in the cam code and call the function from one loop inside select cam, like I did in the linked post above.


Malice
Posted By: emo10001

Re: Creating camera view toggle 1st person to 3rd person - 07/08/11 03:33

Superku, that's a good point about the "camera_Xstperson" loop never stopping. That would explain why my camera.tilt was not resetting...and why sometimes...it would just keep getting bigger and bigger as a variable.

And Malice, that's exactly what I needed, a cam STATE variable to "re-set" every time the function switched..

So, with both of your ideas/advice, now I've got this, and it works perfectly! Thanks guys!

Code:
var cam_mode; //global variable

function camera_1stperson()
{
		camera.x = my.x + 11 * cos(my.pan);
	   camera.y = my.y + 11 * sin(my.pan);
	   camera.z = my.z + 25;
	  	camera.pan = my.pan;
		camera.tilt += (key_pgup-key_pgdn)*5*time_step;
	   if(key_home)
	   {camera.tilt = 0;}
	   camera.roll = 0;
}
function camera_3rdperson()
{
	   camera.x = my.x - 150 * cos(my.pan);
	   camera.y = my.y - 150 * sin(my.pan);
	   camera.z = my.z + 50;
	   camera.pan = my.pan;
		camera.tilt = -15;
	   camera.roll = 0;   
}
function camera_3rdperson2()
{
	   camera.x = my.x - 250 * cos(my.pan);
	   camera.y = my.y - 250 * sin(my.pan);
	   camera.z = my.z + 200;
	   camera.pan = my.pan;
	   camera.tilt = -25;
	   camera.roll = 0;
}

function select_cameras()
{	
	while(1)
	{
		if(key_7)
		{
			camera.tilt = 0;                      
			cam_mode = 1;
		}
		if(key_8)
		{cam_mode = 2;}
		if(key_9)
		{cam_mode = 3;}
		
		if(cam_mode ==1)
		{camera_1stperson();}
		if(cam_mode ==2)
		{camera_3rdperson();}
		if(cam_mode ==3)
		{camera_3rdperson2();}
		wait(1);
	}
}


Posted By: rojart

Re: Creating camera view toggle 1st person to 3rd person - 07/08/11 11:30

Another good advice is to use camera.c library, or learning from it.

Quote:
camera_person(ENTITY*, VECTOR* offset, var factor)
First or third person camera, looks in the same direction as the entity and moves with it with an offset. The factor (0..1) affects the influence of the entity's tilt and roll angle. First person mode is activated with the [F5] key, third person mode with the [F6] key in the camera_startup function.

© 2024 lite-C Forums