Shaking the world

Topics: Developer Forum
Jul 19, 2011 at 9:48 PM

Hi.

I am playing with Farseer on the Windows Phone 7. I am trying to let the user shake the phone and let that force affect the game. Reading the acceleration is easy, but I am unsure how to use the acceleration data to simulate the world shaking.

My game is quite simple. I have a player sprite (a dynamic object) moving in a world filled with static objects (walls). The walls can be sloped at any angle and I want to simulate the force the shaking would have on the player.So if the player moves the phone upwards then I want an upward force to be applied to the player if the player is touching a wall.

I tried to use the Body.OnCollision event to see if the player touches a wall. If he does I have tried to calculate the force using the dot product of the normal between the collision vector and the force vector. Unfortunately this doesn't work properly.

 

Is there a better way of doing this? Is it possible to make a static object apply a force on a dynamic object? Another challenge I am facing is that the player may touch several walls at the same time at different angles...

 

Any suggestions would be appreciated!

Jul 19, 2011 at 9:58 PM

Hmm. Well. I think you basically just want to shake the "camera", right?

Theres a number of different ways to do this, the "best" way would be to alter a "view matrix" and passing it into the spriteBatch.Draw(.,.,.,.,.,.,.Matrix translation) method. Essentially all this would do would alter the position vectors that you pass in. So if you wanted to do that I believe the Matrix is created like this:

             Matrix projection = Matrix.CreateOrthographicOffCenter(0, game.GraphicsDevice.Viewport.X, game.GraphicsDevice.Viewport.Y, 0, 0, 1);
             Matrix halfPixelOffset = Matrix.CreateTranslation(-0.5f, -0.5f, 0)

             Matrix translation = projection * halfpixelOffset;

 

Then you simply pass that into spritebatch.Draw(), and it will draw everything normally. To make the camera shake I think you could just do this:

translation *= Matrix.CreateTranslation(shakeAmountX, shakeAmountY, 0);

 

 

If this is too confusing and strange and doesn't fit into your system, then just make

Vector2 shakeAmount = (0,0);

alter the shakeAmount when you want the screen to shake

and draw everything at (positions + shakeAmount).

Jul 19, 2011 at 11:14 PM

That would make the camera appear to shake, but it wouldn't affect the simulation would it?

What I am interested in is to simulate the force the shaking would have on an object in my game. Say if the player controls a ball. When the phone is moved quickly upwards I want the ball to move upwards before falling down again due to gravity. But I don't need anything else to move. The shaking of the phone itself will take care of that. :-)

 

But thanks for the suggestion!

Jul 19, 2011 at 11:37 PM

Yes, it would merely shake the camera.

To model the force, (I think) simply apply an impulse on the desired objects. Your gonna have to tweak alot of things to get it right.

If that doesn't work properly you try adding an "upward vector" ie: Vector2(0,10), to the balls linear velocity.

Jul 21, 2011 at 9:18 PM

T3chn0 is on the right tracks, it's what I'd try anyway. Take whatever the acceleration is of the phone and apply the negative (I think) to each of the objects. You may need to reduce or increase the shake depending on its reaction, but you'll have to cycle through all the bodies you plan to apply the force to.

I remember trying a similar thing with a character that had an earthquake power. We tried moving the ground, but turning the ground to be not static then back to static can be problematic if the ground doesn't move perfectly back to where it was before. Sometimes it's better applying a force to all bodies touching the ground and/or shaking the camera.

Jul 22, 2011 at 9:44 AM

Thanks again for your suggestions guys (?)!

I think applying a force to the player (and any other moveable objects) will be the easiest way. How would you calculate the correct direction and size of the force if the ground is not flat or if the force is not moving straight upwards? The player may for instance move the phone up to the right while the player is on a sloped ground.

I am quite sure that this should be quite easy really, but I haven't done vector or force calculations in years, so I don't remember any more... :-)

Jul 22, 2011 at 2:43 PM

I would just move all objects off the ground in some random direction. Hopefully that would give the impression of the ground shaking. I don't think you need to worry about slopes since the whole ground would be moving in the same direction. Just push the characters about and let the physics engine decide how they all react.