Sign in to follow this  
ChandlerT

Source Camera Question

Recommended Posts

Currently working on my 3rd person camera for my HL2DM mod, and I'm running into one issue. The camera originally "twitched" when I moved too close to a wall, making it throw off the view as it completely changed angles to the player rather than smoothly transitioning into the wall. After finally being able to fix this problem, I ran into another one. Whenever you do move against a wall, the tracelines no longer work and you can see through the wall rather than the camera detecting the wall and moving against it. I don't know if this is all the code needed, but its all I've been messing with, everything else is default except a few code snippets in various files just to turn the camera on.
void ClientModeShared::OverrideView( CViewSetup *pSetup )
{
// Let the player override the view.
C_BasePlayer *pPlayer = C_BasePlayer::GetLocalPlayer();
if(!pPlayer)
     return;

pPlayer->OverrideView( pSetup );

if( ::input->CAM_IsThirdPerson() )
{
	Vector camForward, camRight, camUp;
	
        AngleVectors( pPlayer->EyeAngles(), &camForward, &camRight, &camUp );
 
	trace_t tr, tr2;
	Vector vecStart, vecStop, vecDirection, vecSetDirection; 
	static float camCurrentY; 
	static float camCurrentX=18.0f; //used for fluid camera transfers
	float camDelta=0.0f;
	vecStart=pSetup->origin;

	AngleVectors(pPlayer->EyeAngles(), &vecDirection); 
		
	vecSetDirection.Init(0,0,1.0f);
	vecDirection=vecDirection.Cross(vecSetDirection);
	vecStop = vecStart + vecDirection*52.0f;
	VectorMA( pSetup->origin, 18.0f, camRight, pSetup->origin);
	UTIL_TraceLine( vecStart, vecStop, MASK_ALL, pPlayer, COLLISION_GROUP_NONE, &tr );
	if (tr.fraction == 1) //are we far enough away to not be hugging a wall with the camera?
	{
		if(camCurrentX < 18.0f)
			camCurrentX +=camDelta;
		if(camCurrentX >=18.0f)
			camCurrentX=18.0f;
		//VectorMA( pSetup->origin, camCurrentX, camRight, pSetup->origin); //set the right offset
		VectorMA( pSetup->origin, 1.0f, camUp, pSetup->origin);
	 	vecStart=tr.endpos;
	}
	else 
	{	
		//we weren't clear on the right, lets check the left
			
		vecStop = vecStart + vecDirection * -52.0f;
			
		UTIL_TraceLine( vecStart, vecStop, MASK_ALL, pPlayer, COLLISION_GROUP_NONE, &tr );
					
		if (tr.fraction == 1) //are we clear on the left?
		{	
			if(camCurrentX > -14.0f)
				camCurrentX -=camDelta;
			if(camCurrentX <= -14.0f)
				camCurrentX=-14.0f;
			//VectorMA( pSetup->origin, camCurrentX, camRight, pSetup->origin);
			VectorMA( pSetup->origin, 1.0f, camUp, pSetup->origin);
			vecStart=tr.endpos;
		}
		else //not clear, so set the camera behind the player and raise it more than normal to maintain clear view
		{	
			//set camera behind player because left and right are not clear
			//VectorMA( pSetup->origin, 1.0f, camRight, pSetup->origin); 
				//check to see if there is enough room above
			AngleVectors(pPlayer->EyeAngles(), &vecDirection); 
			vecSetDirection.Init(1.0f,0,0);
			vecDirection=vecDirection.Cross(vecSetDirection);
			vecStop = vecStart +vecDirection*32.0f;
			
			UTIL_TraceLine( vecStart, vecStop, MASK_ALL, pPlayer, COLLISION_GROUP_NONE, &tr);

			if(tr.fraction == 1)
			{
				VectorMA( pSetup->origin, 1.0f, camUp, pSetup->origin);
				vecStart=tr.endpos;
			}
			else //not enough room on left, right, or above, so move the camera eye level
				 //TODO: Add code to make player translucent as well so the player can see better
			{
				VectorMA( pSetup->origin, 1.0f, camUp, pSetup->origin);
			}
		}

	}
		 
	AngleVectors(pPlayer->EyeAngles(), &vecDirection); 
	vecStop = vecStart + vecDirection * -70;
	UTIL_TraceLine( vecStart, vecStop, MASK_ALL, pPlayer, COLLISION_GROUP_NONE, &tr );
	
	vecStart=pSetup->origin;
	
	vecStop = vecStart+vecDirection*-70;
	UTIL_TraceLine( vecStart, vecStop, MASK_ALL, pPlayer, COLLISION_GROUP_NONE, &tr );
		
	//multiply the default distance by the percentage the traceline traveled, should put the camera infront of the object
	   
	/*if(tr.fraction != 1)
	{
		camCurrentY = -96 * tr.fraction + 10.0f;
	}
	else
        {
		camCurrentY=-96.0f;
		VectorMA( pSetup->origin, camCurrentY, camForward, pSetup->origin);
	}*/
	camCurrentY = -70 * tr.fraction + 10.0f;
	VectorMA( pSetup->origin, camCurrentY, camForward, pSetup->origin);
}
}
Thanks!

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