Part 4a – Raising the floor

Since we want to use a slingshot-like pullback motion to launch our pig, we need to make some space available on screen below the pig. We will raise our world’s ground floor up 60 pixels (= 1/8 of the vertical screen resolution)

We will change our old definition from

//Define the ground
float simulatedHeight = GraphicsDevice.Viewport.Height / Scale;
float simulatedWidth = GraphicsDevice.Viewport.Width / Scale;
BodyFactory.CreateEdge(_world, new Vector2(0.0f, simulatedHeight), new Vector2(simulatedWidth, simulatedHeight));


//Define the ground
float simulatedHeight = Constants.FloorPosition.Y / Constants.Scale;
float simulatedWidth = (GraphicsDevice.Viewport.Width / Constants.Scale) * 2;
BodyFactory.CreateEdge(_world, new Vector2(0.0f, simulatedHeight), new Vector2(simulatedWidth, simulatedHeight));

The biggest notable change is the introduction of the Constants class, which we will use to store all our constants and magic numbers.

Constants.Pixels.FloorPosition is a Vector2 struct set to (0, 420), assuming our vertical resolution is 480 pixels high. We also widen our world’s width to twice the screen’s horizontal’s resolution.

Below is the source of the Constants class:

public static class Constants
	public const float Scale = 100f;
	public static float HalfScreenWidth { get; private set; }
	public static float ScreenWidth { get; private set; }
	public static Vector2 FloorPosition { get; private set; }

	public static void Initialize(GraphicsDevice graphics)
		HalfScreenWidth = graphics.Viewport.Width / 2f;
		ScreenWidth = graphics.Viewport.Width;
		FloorPosition = new Vector2(0, graphics.Viewport.Height * 0.875f); // = 7/8

To render our floor, I took this piece of existing artwork, added it the the Asset xml file and updated the Draw method.

// ...
// render bodies here ....
// ...

Continue to part 4B – hello piggy.


12 Responses to Part 4a – Raising the floor

  1. jfmiranda says:

    Hello, I’m following your posts, but in this I have a problem. The floor texture doesn’t expand to fit all the width of the screen.

    How could I solve it?

    Thanks in advance.

    • Guy Gervais says:

      The floor texture is 630 pixels wide. I fixed the problem by opening the texture in Paint.Net and resizing it to 800 pixels.

      • jodegreef says:

        I didn’t notice the texture I provided here was less than 800px wide. You can indeed resize it, or you can just draw it twice using an extra spritebatch.draw call.

  2. Pingback: Part 4 – Three, two, one… launch! « Jo De Greef | Blog

  3. sander says:

    hello guy
    why don’t you use a convertunits method instead of using value as this “0.875f”?
    in Farseer_Physics_Engine examples exists a class that do this work….probably you know it….;-)

    as ask before….is it possible to have the source code of this GREAT tutorial?

    best regards

    • jodegreef says:

      you’re right about the convertunits class. I could have used in in the PrefabBodyFactory, to go from pixels to simunits. I simply forgot about it as it’s only part of the samples and not part of the core farseer library. My way of multiplying and dividing by Scale is a little too simple and should be refactored into a method however.

      The case of 0.875f is shorthand for “x * 7 / 8” and has nothing to do with pixels vs simunits. It’s just a way to get 1 eight of the height of the screen without hardcoding it to 60 pixels.

      I’m not sure yet about releasing the source code. Technically, all source is already available in the posts.

      • sander says:

        i’m not so smart but at the moment is not so easy to recreate all the class that you have created.
        probably is only my problem….

      • sander says:

        hi guy
        i have used convertunits class and it work fine without problem.
        I think this feature is important if you need to create a level editor.
        you can store positioning data of all body in xml file using pixel data and not simulator unit.


        when you are planning to finish all episodes of this tutorial?

      • jodegreef says:

        No idea how far I will take this series. Next will be adding a camera and then some gameplay things, such as making boxes ‘explode’ when hit hard enough, hitting specific targets, …

      • sander says:

        great announce i will wating for it….regards

  4. Pingback: Dew Drop – May 6, 2011 | Alvin Ashcraft's Morning Dew

  5. Pingback: Russian Coding 4 Fun

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: