This project has moved and is read-only. For the latest updates, please go here.

How to create track (map / level) - best practices

Oct 10, 2011 at 11:10 PM


i've been working for past full two hours to create a racing track that looks like this;

It contains 62 vertices and is pain in the a** to work like this.. also, it's a string of 62 edge bodies, and it could be only 2,

inner and outer, and car could move between them.. how to achieve this?? Creating polygon body is limited to 8 vertices??


I've got track from a friend that looks like this ( alpha + texture );

and don't know what to do with it, is there any way to load this track (semi)"automatically", because creating track from vertices is headbanging work.

I've tried all decomposers but no progress so far.. :( If i have to go back to vertices to create tracks i will start crying.. literally.


While we're on track topic, these are "just" 1900x1200 tracks, what if i want to cover area close to 10 square kilometers in 64 px / 1 meter ratio, what are my options?


Thanks for your help in advance!!

Oct 11, 2011 at 12:03 AM

I would create a simple editor that allows you to create curves to represent each side of the track. This is semi manual, but allows a much higher level of control. Once the curves are correct you can easily convert them to edge shapes by using the Path class.

For huge tracks you would probably need some kind of dynamic loading system for both the texture and physics bodies.

Will your AI be path based? I.E. - will you run several times around the track recording the position for AI movement and then using the recording to have bodies follow the path etc....

Or will your AI be something else? (I'm not sure how else you could do race car AI, never tried)

Oct 11, 2011 at 10:35 PM

Thx for the reply!

OK, i'll see what i can do regarding the track creation via editor, just for the tip, what type of object / control would you use for drawing surface and which one for the curves?

So you think generating complex track with a lot of curves won't impose performance issues because one track could easily consist of couple of hundreds, even thousands, edge bodies?

About AI - yes, i had in mind path which will bodies follow - but i'm still far away from solution, do you have some good example of AI path implementation, link or project?

Thx man, cu!


Oct 13, 2011 at 8:15 PM

1. Performance can easily be solved by using a custom broad phase style test to add edges which are near a car and remove any which are not. I am using this technique in a Terraria style game I am working on and I can have upwards of 5 million ground blocks (static) and several 100 dynamic blocks and I have no performance issues.

2. The Path class could easily be extended to get a list of lines representing the curve (for rendering the curve) and a list of the points (for modifying the curve). Then It's up to you to add all the editor style UI to allow movement of the points and whatnot.

3. AI, this is a tough one... I will have to get back with you later on this.

Oct 13, 2011 at 11:56 PM

Thx man, and i would REALLY like to see some good AI path implementation, so please don't forget about me, thx! :)


BTW i have an issue which i don't know how to approach to..

I've created car 180 px in length, and 90 in width, set up all the physics so i have believable car movement, and than decided

that i wont scroll the screen, it's going to be single screen action. So i've, of course, made 3 times smaller car so it could proportionally  fit on 1900x1200 size map,

and of course, due to change of size, driving was disaster, and i had to set it all from the beginning, it was pain ..... but i said OK, never mind.

But now i see that by changing the display resolution, i will need to resize (scale) all the bodies accordingly, so that will, obviously, affect my current driving set-up.

Is there any way to avoid this, or do i have to pair-up in dictionary resolution / car-setup for all available resolutions, what is your approach to this issue?

Thank again.

Oct 14, 2011 at 1:43 PM

I think a good idea on the AI could be to set a grid on the track (decompose the track in small squares). Then for each car you calculate a path using A*. Probably this article can help 7you:

Oct 18, 2011 at 12:45 AM

Thx Pnikosis, i'll take a look at that asap.

Can i get an answer for the question in my previous post, changing resolution / body size, thx!

Oct 22, 2011 at 3:14 AM

Your physics world should always be a fixed size. Think real world units. Use meters for the sizes. If your car is top down it should be around ~1.5m wide and ~3m long (top down view). Now when you render you can just scale the position to whatever fits your graphics resolution.  Personally I use a resolution independent method where I create an orthographic projection of however big I want my view port to be (regardless of how many pixels it actually contains). You can use this method to create real world sized view ports, like 80m x 60m or just an independent resolution like (800x600). That way even if the user switches to 1080p everything should still render the same.

Nov 22, 2011 at 7:39 PM

Another tool you might consider looking into for track editing is Inkscape.  It is free and saves out svg which is relatively easy to parse (and can be directly manipulated within the tool).

Also, I just wrote a tutorial about writing extensions for it: (in case you're interested?).

Feb 22, 2012 at 3:26 PM
Edited Feb 22, 2012 at 3:30 PM

I've edited the question, potential solution just crossed my mind, sry. :)

Feb 24, 2012 at 11:43 PM

Hi all!

In the update method i set the world step like this

mWorld.Step((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f);

I've tried fixing it explicitly like this;

mWorld.Step(Math.Min((float)gameTime.ElapsedGameTime.TotalMilliseconds * 0.001f, (1f / 60f)));

and after that i apply force to all of the car wheels, everything works great regarding the physics, car feels realistic,

but display of car movement on screen is blurry and somewhat choppy, my guess is frame rate is to low, wrong world step??

Even if i apply solution from topic: it doesn't help at all. :((

I thought 1/60 for game and 1/1000 for Farseer engine is the right ratio?

Did you had any problems like this??

Can you please give me an advice, thank you!!

Feb 25, 2012 at 9:15 PM

Anyone? :(

Feb 26, 2012 at 1:18 PM

Your code looks good.

Please confirm that you are using the correct scale for you physics for example as mattbettcher suggested. It's not very clear from your posts. It makes no sense to talk about pixels - converting to pixels from the simulation should be done only in the draw method. Only there you should care for your pixel/meter ratio. The physics works in meters and should be realistic.

For example a choppy movement could be due to a huge force being applied to an object which shoots it at high speed that quickly goes to 0 due to significant dampening/top-down-friction (which are exponential functions so they brake fast). Next time step you rinse and repeat and your object looks like it's skipping forward as it has no "time" to achieve "constant" velocity (smooth movement).

Large scale also can lead to slow speed. F=ma so for a large mass (proportional to dimensions*density) it will take a while to build the acceleration (accelerating a freight train to a certain velocity vs accelerating a bike). If necessary increase Farseer's velocity iterations, also decrease the time step to see if it becomes smoother. 

You can log your values to a file or on screen to see visually what's going on and make some test cases that can be solved mathematically beforehand. Sometimes this helps a lot.

Mar 2, 2012 at 10:30 AM
Edited Mar 2, 2012 at 10:34 AM

I've started to write a post a few times, but i don't have the right words to describe the motion, so i'll let this

(motion :) picture say a thousands words instead of me. :) Here are the links;

Any help or suggestion on how to solve this issue is welcome, thank you all in advance!!

p.s. choppy movement and car jerks are little more expressed because of the FRAPS, (specially in second video) but the movement displayed is not far from the truth..

Mar 2, 2012 at 7:16 PM

Hi, I would assume you've played with the parameters of the engine and set up your world properly with no luck. So therefore you might have a problem that's more technical than changing configuration values (please double-check). Basically I can see you have a fast moving object and you've tried setting continuous collision detection or even IsBullet and it's still misbehaving. In other worlds Farseer's facilities don't help. More technically it could be said the simulation is unstable (we've excluded it's XNA graphics issue). To be honest the current simulation I'm working on sometimes has the same problem just that high speeds are not typical use-case so it doesn't bother me.

Here's a link that might shed some more light about the issue. Note that it gets quite complex (you'd need smoothing algorithms): 

Mar 28, 2012 at 11:24 AM
Edited Mar 28, 2012 at 11:29 AM

Hi all!
I've created everything, from graphics to engine to fit 24" monitor, 1920x1200 resolution, and all works fine, here is the picture to prove it..

Now, i'm trying to scale everything to fit 19" monitor, that would, obviously be 1280x1024 resolution..

It's a lot of code, so i've extracted only the important stuff, so, in the update method i'm creating matrix transform like this;

// Farseer units: 1m = 20px, in the update method i call my CreateTransform method..

float projectionWidth = MyConstants.ToFarseerUnits((float)graphics.GraphicsDevice.Viewport.Width);
float projectionHeight = MyConstants.ToFarseerUnits((float)graphics.GraphicsDevice.Viewport.Height);
mView = mCamera2D.CreateTransformation(projectionWidth, projectionHeight, mScreenScalingFactor);

// my CreateTransformation method public Matrix CreateTransformation(float viewportWidth, float viewportHeight, Vector3 resolutionScale) { return _transform = Matrix.CreateTranslation(new Vector3(-_position.X, -_position.Y, 0)) * Matrix.CreateRotationZ(Rotation) * Matrix.CreateScale(resolutionScale * new Vector3(Zoom, Zoom, 1)) * // thought was to implement both scale and zoom.. Matrix.CreateTranslation(new Vector3(viewportWidth * 0.5f, viewportHeight * 0.5f, 0)); } -----------------------------------------------------------------------------------------------------------
//Vector3 that's used for scaling i create like this; // screen cailing vector3 (mScreenScalingFactor) float horScaling = (float)graphics.PreferredBackBufferWidth / 1920f; float verScaling = (float)graphics.PreferredBackBufferHeight / 1200f; resolutionScale = new Vector3(horScaling, verScaling, 1); // it's single screen action so camera position is calculated by screen center mScreenCenter = new Vector2((float)graphics.GraphicsDevice.Viewport.Width / 2f, (float)graphics.GraphicsDevice.Viewport.Height / 2f); // farseer screen center mCamera2D.Position = mScreenCenter / new Vector2(resolutionScale.X, resolutionScale.Y); // since it's 2D i divide it by X and Y of the V3 scale
// draw method looks like this // draw method spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, null, mView); // drawing stuff goes here.. spritebatch.end();

As seen in first screenshot, when it's run natively (1920x1200, or to be exact, when scaling = 1) everything is working perfect, but when i run it in 1280x1024 resolution than it looks like this;

than, i was fixing position manually in Matrix.CreateTranslation(new Vector3(-_position.X + 17, -_position.Y + 4.5f, 0)) just to find out that where did the car go, and found out it's drawn heavily in offset too, like this..

Of course, that moved graphics too (i suppose i need to recalculate body position according to scale somehow), but track and texture are aligned now, what part of the puzzle am i missing, or should i change the approach?
Thank you all in advance for help!!

Mar 29, 2012 at 4:42 PM