Body Insists On Spawning On Top of Environment...

Topics: Developer Forum, User Forum
Jul 26, 2009 at 3:12 AM
Edited Jul 26, 2009 at 3:13 AM

i'm currently making a game (a platformer/shooter type thing) where i need one body (the player) to spawn inside another body (the level). the level is composed of a bunch of rectangles and things surrounded by a boundary. the vertices of the environment are found via the texture > vertices technique found in the advanced samples.


however, when the game starts, the player spawns on top of the box, even when i explicitly tell it to spawn inside the rectangle. can anybody enlighten me as to why this is occurring and how i can fix it? maybe a better way to make the environment?

if there's any question as to what this environment looks like, here's the pic i'm using for it. it's by no means a finished product.

oh, and i tried taking the top off the level but it screwed up all of the collisions.

Jul 26, 2009 at 3:30 AM

I'm guessing that your level is one big texture?

We don't support geometries with holes in them, that means that you need to create your level from multiple smaller geometries. Not only will this work better than your current solution, it will also give you better performance.

You simply need to cut your level into several geometries. For example in a 3x3 grid or something like that. Cut your texture into a grid of 3x3 and give it to the texture to vertices function and it will make 9 geometries (3*3) that looks like your level. Then you simply draw all 9 pieces and and add all 9 geometries to the physics simulator.

You can actually make all the geometries part of the same body. This is highly recommended as geometries from the same body is not checked for collisions, further improving your performance. I have an example of how to chunk up a landscape right here in our manual.

Jul 26, 2009 at 3:39 AM

ah, thanks. i was just considering splitting it up into a bunch of rectangles, but a grid sounds fantastic.

also genbox, you seem to reply crazy fast... kudos.

Jul 26, 2009 at 4:08 AM

also, possibly a stupid question, but would i cut up the texture via code or by literally cutting up the picture into 9 tiles?

Jul 26, 2009 at 4:18 AM

If you have hundreds of levels, it might be better to code your way out of it, but Farseer Physics does not have any such tools.

Otherwise, you literally cut the texture up into tiles. The border of your level might give you problems, We do have multitexture detection which means you can split up a texture into tiles and get several geometries if the texture has several different items on it. But it might be easier if you created the border on all levels. Simply create a border like the one in our samples and add all the platforms to the level from textures instead.

Jul 26, 2009 at 4:23 AM


alright, alright, sounds good, thanks.

Jul 26, 2009 at 4:50 AM

okay, one more question. how would i go about making one body from all the geometries as you said before?

Jul 26, 2009 at 2:34 PM

You first create the body, and then the geometries.

You create the body as you always do:

Body body = BodyFactory.Instance.CreatePolygonBody( ... )

and when you create the geometry, you reference the body in the arguments:

Geom geom = GeomFactory.Instance.CreatePolygonGeom( ... body ... )

I omitted the rest of the arguments, ... means the rest of the arguements. To use a single body in several geometries, you simple reference the same body. You can put an offset on the position and even an offset in the rotation relative to the body. Take a look at the arguments in the CreatePolygonGeom() method.

Aug 2, 2009 at 2:09 AM

alright. so i cut up my map into 11 tiles, made a system that puts them together appropriately and makes the geometry, and it all flowed together quite nicely. however, now the player character sort of... trips over every seam i have. any ideas how to solve this? i thought i might make a buffer zone of sorts, and overlay a few pixels, but i'm not sure if that will cause some screwy collisions... will it?

Aug 2, 2009 at 10:57 AM

That is one of the known problems of sequential impulse based physics engines. All the engine sees are two corners, so it is not sure if it should fall from going over the edge or stay up because of the other corner.

The problem has discussed before, but there are no conclusion to such a discussion as there are several ways of handling it. It all depends on your movement code. How are you moving your player and what is the shape of that geometry?

As for the buffer zone, if I remember correctly, the problem will still persist even tho you create an overlap.

Aug 3, 2009 at 2:38 AM

currently, the player is simply a rotating ball (acting as a wheel). this is still subject to change, but i'm fairly happy with this method, so it'll prolly stay.

Aug 3, 2009 at 3:41 AM

I don't have a solution to this problem, but I have some ideas.

1. Use rays instead of direct contact with the gound. You simply read of the distance and make sure the player keeps a distance from the ground.

2. Only chunk up the terrain horizontally. The problem is most apparent when the character moves on the ground, so splitting up the geometry horizontally, but not vertically would prevent this.