Rotate "World"

Topics: Developer Forum
Feb 17, 2011 at 12:38 PM


I'm making a game like Loco Roco, where you have to "rotate" the world so gravity let's your character roll the right direction.

In order for that it seems I should rotate the World object you get in a game.

I'm new to farseer and XNA, but have programmed before in C#. So go easy on me (A).

Does anyone have a tutorial of some kind, or a very small example project so i can get started.

Thanks in advance,


Feb 17, 2011 at 12:50 PM

I think the best solution is to rotate the camera and manually change the World's gravity value based on the camera angle.

Feb 17, 2011 at 12:52 PM
Edited Feb 17, 2011 at 12:53 PM

You can't really rotate the world object. Just modify the World.Gravity vector instead and rotate your view e.g. the spritebatch or whatever you use for rendering about the angle between (0,1) and your gravity vector.

Edit: Guess we wrote at the same time... what Pnikosis said ;)

Feb 18, 2011 at 10:45 AM

Thanks for the responses.

I'm trying to adjust the gravity in game (while running) but it simply get's ignored. It seems like World calls Island.Solve only one time to give a reference of the gravity. After that point the updated gravity just doesn't do anything.

i used this code to change the gravity


        public override void HandleKeyboardInput(InputHelper input)
            if (input.CurrentKeyboardState.IsKeyDown(Keys.X))
                World.Gravity.Y = 20f;
            if (input.CurrentKeyboardState.IsKeyDown(Keys.S))
                World.Gravity = new Vector2(World.Gravity.X, (World.Gravity.Y + 0.05f));

            if (input.CurrentKeyboardState.IsKeyDown(Keys.W))
                World.Gravity = new Vector2(World.Gravity.X, (World.Gravity.Y - 0.05f));

            if (input.CurrentKeyboardState.IsKeyDown(Keys.A))
                World.Gravity = new Vector2((World.Gravity.X + 0.05f), World.Gravity.Y);
            if (input.CurrentKeyboardState.IsKeyDown(Keys.D))
                World.Gravity = new Vector2((World.Gravity.X - 0.05f), World.Gravity.Y);

            if (input.CurrentKeyboardState.IsKeyDown(Keys.Q))

            if (input.CurrentKeyboardState.IsKeyDown(Keys.E))

            if (input.CurrentKeyboardState.IsKeyDown(Keys.I))
                if (input.IsNewKeyPress(Keys.I))
                    _circle[0].Body.IgnoreGravity = !_circle[0].Body.IgnoreGravity;


if I use a Console.Writeline i can see the world's gravity changed. But the game ignores it.

In Island.cs in the function "public void Solve (ref TimeStep step, ref Vector2 gravity)" I added a Console.WriteLine to see the gravity and it seems it doesn't get changed there.

Does anyone know a solution for this?

Feb 18, 2011 at 10:55 AM

the object in the game is;

_circle[i] = FixtureFactory.CreateCircle(World, 2f, 1f, _position, material);
_circle[i].Body.BodyType = BodyType.Dynamic;
_circle[i].Restitution = _restitution;
_circle[i].Friction = 1.0f;
_position.X += 6f;
_restitution += 0.2f;

Feb 18, 2011 at 11:43 AM

It looks like the problem was the constructor:

Body body = BodyFactory.CreateBody(World, new Vector2(-15f, 6f));
_circle[i] = FixtureFactory.CreateCircle(2, 1, body, material);

The only problem i got now is that when the figure comes to a complete standstill, It totally ignores gravity.

But after clicking or dragging it once. It comes back to life.

Anyone got a solution for this one?

Feb 18, 2011 at 12:09 PM

I fixed the above problem by saying:

_circle[0].Body.Awake = true;

after updating the gravity.


Since the gravity is changed, the world should visually rotate. As sead above, it seems that i have to change the camera angle / spritebatch.

I have absolutely no idea how to start. Could someone please help me getting started. Some sample code or tutorial would be even more appreciated :D

Thanks in advantage

Feb 18, 2011 at 12:12 PM

Several solutions are possible. First you could set AllowSleep to false in Settings.cs, which may affect performance if you have many objects though. Second you could set SleepingAllowed to false on idividual bodies, who are affected by the gravity change. Last solution would be to set gravity to zero and manually apply it to all bodies in each step you could implement your on controller for this task... like the gravityController for point gravity sources.

Feb 18, 2011 at 12:17 PM
Edited Feb 18, 2011 at 12:19 PM

Regarding the Camera: Have a look at Rotation Matrices. Use Matrix.CreateRotation(...) to create one. You can pass it to SpriteBatch.Begin(0, null, null, null, null, null, MyRotationMatrix) and everything you draw will be rotated. There is all kind of fancy stuff you can do with matrix transformations once you get the concept behind it, but it's a bit out of scope for this discussion board. Fear not though... you'll find tons of material on this thing called the interwebs ;)

Edit: Oh I forgot... have a look at the camera in the updated HelloWorldXNA sample in source control for starters. It even had rotation build in once but unfortunately genbox removed it to make the sample easier to understand ;)

Also in source control is the new SamplesXNA Camera2D class which supports rotation but is a little more complex.

Feb 18, 2011 at 12:27 PM

Hi Elsch, an  little off-topic question. The new SamplesXNA Camera 2D class is not the "broken" one, right? I mean, the previous was a little rough on some areas as far as I remember, and I recall someone mention it as "broken".

Feb 18, 2011 at 12:33 PM

No it is a new one. Im still working on the new samples however and have not yet tested how smooth scrolling and object tracking is. There are some constants in there which might still need some tweaking. But if you want to play around with it and find some errors just go ahead and let me know :)

Feb 18, 2011 at 1:20 PM

Sounds great. Thanks Elsch, I'll take a look to it!

Feb 18, 2011 at 2:53 PM

I just adjusted the speeds a bit and uploaded a new version. Position tracking works fine and should also work for different framerates and non fixed timestep now. Rotation tracking is still a bit buggy though but probably not that important anyway oO

Have phun :)

Feb 18, 2011 at 5:07 PM


Feb 22, 2011 at 10:03 PM

It looks like a simple

ScreenManager.Camera.Rotation += 0.1f;

Does the trick.

Thanks anyway.

Feb 23, 2011 at 7:05 AM

Yes, camera rotation should work now. Just fixed it a few days ago.