AdrianWoods

I'm playing around with shaking the screen when explosions occur and thought the best way to do this would be to randomly move the backbuffer.

How can I do this kind of effect!

Thanks!



Re: XNA Game Studio Express Can You Move the Backbuffer?

Jim Perry

As a backbuffer is nothing but a chunk of memory, you can't really move it. Are we talking 2D or 3D It sounds like 2D. One way to do this would be to redraw the current view several times, offsetting it in random directions. Can you provide more information on exactly what you're doing that you want to shake up




Re: XNA Game Studio Express Can You Move the Backbuffer?

AdrianWoods

Hi Jim!

Yes, I'm doing 2D and just want to "shake things up a bit".

So basicly, I just clear the graphics device, begin my spritebatch, draw all my stuff, end the batch.

Then call draw.

How might I draw that with an offset

Thanks for the help!

Adrian





Re: XNA Game Studio Express Can You Move the Backbuffer?

AdrianWoods

Oops sorry about the cross posting.



Re: XNA Game Studio Express Can You Move the Backbuffer?

XNA Rockstar

Check out the Shake method I made for my 2d game MissileCommandeEr.  It moves the ground and gun sprites around and changes the background color.  Some of the classes and methods are specific to my game, but should be simple enough to figure out how they work.  Good luck

GLOBALS NEEDED

bool shaking = false;

int shakeTimeMilli = 0;

 

TO START SHAKING

shaking = true;

 

CALL SHAKE WITHIN UPDATE METHOD

#region Shake

if (shaking == true)

Shake(gameTime.ElapsedGameTime);

#endregion

 

SHAKE METHOD

void Shake(TimeSpan ts)

{

#region Increase shakeTime and check if it is over limit

if (shaking == false)

shaking = true;

shakeTimeMilli += ts.Milliseconds;

if (shakeTimeMilli > 500)

{

shakeTimeMilli = 0;

shaking = false;

bgColor = Color.Black;

if (gs != GameState.gameover)

if (useController)

GamePad.SetVibration(PlayerIndex.One, 0, 0);

}

#endregion

#region Shake the screen and change background

int shift = 3;

if (shakeTimeMilli > 100 && shakeTimeMilli < 200)

{

ground.MoveTo(new Vector3(ground.Position.X, ground.Position.Y + shift, 0));

gun.MoveTo(new Vector3(gun.Position.X, gun.Position.Y + shift, 0));

foreach (Building c in cities)

c.MoveTo(new Vector3(c.Position.X, c.Position.Y + shift, 0));

foreach (Sprite n in nuclear)

n.MoveTo(new Vector3(n.Position.X, n.Position.Y + shift, 0));

bgColor = Color.AntiqueWhite;

if (gs != GameState.gameover)

if (useController)

GamePad.SetVibration(PlayerIndex.One, .5f, .5f);

}

if (shakeTimeMilli > 200 && shakeTimeMilli < 300)

{

ground.MoveTo(new Vector3(ground.Position.X, ground.Position.Y - shift, 0));

gun.MoveTo(new Vector3(gun.Position.X, gun.Position.Y - shift, 0));

foreach (Building c in cities)

c.MoveTo(new Vector3(c.Position.X, c.Position.Y - shift, 0));

foreach (Sprite n in nuclear)

n.MoveTo(new Vector3(n.Position.X, n.Position.Y - shift, 0));

bgColor = Color.MediumVioletRed;

}

if (shakeTimeMilli > 300 && shakeTimeMilli < 400)

{

ground.MoveTo(new Vector3(ground.Position.X, ground.Position.Y + shift, 0));

gun.MoveTo(new Vector3(gun.Position.X, gun.Position.Y + shift, 0));

foreach (Building c in cities)

c.MoveTo(new Vector3(c.Position.X, c.Position.Y + shift, 0));

foreach (Sprite n in nuclear)

n.MoveTo(new Vector3(n.Position.X, n.Position.Y + shift, 0));

bgColor = Color.Moccasin;

}

if (shakeTimeMilli > 400 && shakeTimeMilli < 500)

{

ground.MoveTo(new Vector3(ground.Position.X, ground.Position.Y - shift, 0));

gun.MoveTo(new Vector3(gun.Position.X, gun.Position.Y - shift, 0));

foreach (Building c in cities)

c.MoveTo(new Vector3(c.Position.X, c.Position.Y - shift, 0));

foreach (Sprite n in nuclear)

n.MoveTo(new Vector3(n.Position.X, n.Position.Y - shift, 0));

bgColor = Color.SlateGray;

}

#endregion

}





Re: XNA Game Studio Express Can You Move the Backbuffer?

mike_n

AdrianWoods wrote:

How might I draw that with an offset


Add the capability to be able to apply some global 'offset' to everything that you draw to the screen...

OR, draw to a target larger than the screen and then draw the final image to the screen using some offset





Re: XNA Game Studio Express Can You Move the Backbuffer?

AdrianWoods

Yeah, i was hoping to avoid having to update all the sprites and instead just shake things as a whole.

Oh well, thanks for the input!





Re: XNA Game Studio Express Can You Move the Backbuffer?

Hlubocky

You aren't technically updating the sprites any more than necessary unless you code it in an inefficient way. Technically, you need to draw each element on the screen anyway, so adding an offset for the shake isn't any extra work (besides the addition).




Re: XNA Game Studio Express Can You Move the Backbuffer?

mike_n

Yeah, don't embed the shake offset in the object's position on screen, only apply it as you draw the object - the object isn't moving logically in the world, you're just adjusting the place on the screen it gets drawn at.