Jump to content
  • Advertisement
Sign in to follow this  
ogracian

Help "shaking" the camera...

This topic is 4844 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I am coding a game but I want to "shake" the camera when a explosion ocurrs, I have implemented a simple test but I am not really happy with it, because my current method is not frameTime sync, and show some image "tear" so I appreciate if someone could help me with about it. Here is my current code:
void ShakeCamera(void)
{
	if (!m_iShakeCam)
		return;

	CEngine * pEngine   = CEngine::GetInst();
	float	  frameTime = 0.001f * pEngine->GetFrameTime();
	CCamera * pCamera   = pEngine->GetCamera();

	//
	// Do shake
	int shakeDir  = 2 - (rand() % 5);
	pCamera->Pos  = m_InitialCamPos + pCamera->GetRight() * shakeDir * m_fShakePower;
	pCamera->Pos += pCamera->GetUp() * shakeDir * m_fShakePower;

	//
	// Update shake duration time
	m_fShakeDuration -= frameTime;
	if (m_fShakeDuration < 0.0f)
	{
		//
		// Shake done, reset data
		m_iShakeCam = 0;
		pCamera->SetPos(m_InitialCamPos);
	}
}


Thanks in advance, Oscar [Edited by - ogracian on August 9, 2005 2:25:22 AM]

Share this post


Link to post
Share on other sites
Advertisement
maybe its because you got VSync disabled?

turn vsync on, if it doesn't disappear, try using sin functions to translate the camera... <- works great! :)

Share this post


Link to post
Share on other sites
Hi,

Thanks for your help, I have turned on vsync and no more tears : ). But my
current method is not frame time sync so I am not very happy with it.

About the SIN methdo you suggest, could you develop a little about it?

Thanks,
Oscar

Share this post


Link to post
Share on other sites
I've implemented screen shakes in two ways in the past, one is better then the other, but I don't have the code in front of me so pseudo code will have to work.



void shakeCamera()
{
shaking = true;
}

void updateShake( float timeDelta )
{
if ( !shaking )
return;

//accumulate time
timePassed += timeDelta; // timeDelta is between frames

if ( !(timePassed >= timeToUpdateShake) ) //the smaller timetoupdateshake is, the faster it will shake
{
return;
}

timePassed = 0;

//keep count of number of shakes
numShakes++;

//random x offset
float xoff = rnd( range of offsets, shakeMagnitude or power );
//same for y
float yoff = rnd(...);

//maybe create these points as a vector? depends on how your camer is set up.
camera->setView( xoff, yoff );

//only shake for a certain amount of time (timePassed * numShakes)
if ( numShakes >= shakeNumber )
{
shaking = false;
camera->setView( 0,0 );
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!