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

I need some help with some very weird behaviours

Topics: Developer Forum
Sep 30, 2009 at 4:42 PM



For the last 2 days I've tried to get this working.

I've got it all setup. Created an instance of PhysicsSimulator, some bodies and geometry.


But collision seem to be very bad, and I get some other weird behaviour.



GameObject goNewBody = new GameObject();
goNewBody.Height = 64;
goNewBody.Width = 64;
goNewBody.TheSprite = GetSprite("test.png");
goNewBody.Body = BodyFactory.Instance.CreateRectangleBody(PhysicsEngine, goNewBody.Width, goNewBody.Height, 1);
goNewBody.Geometry = GeomFactory.Instance.CreateRectangleGeom(PhysicsEngine, goNewBody.Body, goNewBody.Width, goNewBody.Height, 1);
goNewBody.Geometry.FrictionCoefficient = 0.8f;
goNewBody.Body.IsStatic = false;
goNewBody.Body.Position = new Vector2(0, 0);//new Vector2(512, 128);
//goNewBody.Body.Rotation = AdvanceMath.MathHelper.RadiansPerDegree * 45;


GameObject is a class that contains information about the object, and stores information on what to draw, etc. (I'm using C# and OpenGL (SDL.NET))
test.png is a simple png file, 64x64. (It's a crate).

I recently changed the gravity to 10,10 to see how the engine would behave.



PhysicsEngine = new FarseerGames.FarseerPhysics.PhysicsSimulator(new Vector2(10, 10));//(new Vector2(0, 900));


I've also tried changing PhysicsEngine.Iterations, etc.


The boxes starts floating from top left towards bottom right. That looks all fine, but as soon as they collide with the floor I have, they bounce off, going up, then suddenly changes direction downwards again, and they continue like that, like saw teeth.

And the boxes seem to have no corners, etc. They stand on top of each other just fine, but slides off, and cuts through the corners.

The floor is just a 1024x64 box, created just like the crates.

I am drawing the boxes, with the center based positioning in mind.

Here's my code for updating the physics. I'm using plain C# so I'm using Environment.Tickcount:

public int LastTick = Environment.TickCount;

public void Draw(object sender, EventArgs e)
    /*Drawing code here*/
    float delta = Convert.ToSingle(Environment.TickCount - LastTick) / 1000f;
    LastTick = Environment.TickCount;

I don't know if it's a good idea to do it in the draw event. I don't see how that would affect it, though? 




I've tried everything, and I don't know what to do about it.



Sep 30, 2009 at 4:51 PM

I just put the update code into a timer, and it's exactly the same.


public int InitialTick = Environment.TickCount;
public int LastTick = Environment.TickCount;

private void PhysicsTimer_Tick(object sender, EventArgs e)
    float delta = Convert.ToSingle(Environment.TickCount - LastTick) / 1000f;
    LastTick = Environment.TickCount;


Sep 30, 2009 at 5:15 PM

I just made a small video to illustrate the problem:

Sep 30, 2009 at 5:31 PM

A debug view enabled on the physics simulator would give show you the problem. We officially support a debug view for XNA, but since you run OpenGL you will have to create one yourself. In your case I guess you just need to show the polygon points of all geometries.

Take a look at the PhysicsSimulatorView.cs file included in the XNA samples projects (or click here to view it from the source control). There is a method called DrawVerticesAndEdges that draws the points of each geometry in the physics simulator - just write a similar method that utilizes SDL.

This will tell you if you have positioned your geometries right. It is also a great help for future problems that might arise.

Are you using SAT narrow phase by any chance?

Sep 30, 2009 at 6:25 PM

I'll try to draw the vertices.

SAT? I haven't turned anything on, at least. Just a new instance of PhysicsSimulator, and then bodies.

        private void DrawVerticesAndEdges(SpriteBatch spriteBatch)
            //draw vertice texture
            for (int i = 0; i < _physicsSimulator.GeomList.Count; i++)
                int verticeCount = _physicsSimulator.GeomList[i].LocalVertices.Count;
                for (int j = 0; j < verticeCount; j++)
                    if (_enableEdgeView)
                        if (j < verticeCount - 1)
                            _edgeLineBrush.Draw(spriteBatch, _physicsSimulator.GeomList[i].WorldVertices[j],
                                                _physicsSimulator.GeomList[i].WorldVertices[j + 1]);
                            _edgeLineBrush.Draw(spriteBatch, _physicsSimulator.GeomList[i].WorldVertices[j],
                    if (_enableVerticeView)
                        _verticeCircleBrush.Draw(spriteBatch, _physicsSimulator.GeomList[i].WorldVertices[j]);


Sep 30, 2009 at 7:32 PM

Okay. I tried drawing the vertices, and it certiainly is my drawing code, that's wrong.

I just can't seem to grasp the origin of the problem.


The problem turned out to be the part of my code that creates my sprites. It creates 359 images (360 degrees) based on a sprite.

So, degree 0 was of course a 64x64 image.

But when it creates rotated sprites like 45 degrees, the image would resize (like the AABB box), and my drawing code draws the sprite, only took the initial size into account.


Now, I've fixed it, and it works like it should.




Also. Are the crates supposed to be so unstable? I mean, 4 crates stacked on top of each other, and they seem jitter around like crazy?


Sep 30, 2009 at 7:39 PM

Ah, rushed post... The reason I first said "can't seem to grasp", and then says "the problem turned out to be", was because after I posted the link, I realized what the problem was xD

Sep 30, 2009 at 7:41 PM

It was as I suspected. Glad to hear you got it working.

As for the stability, even tho we use a simple numeric integrator and have quite a high error in normal simulations, you should be able to stack quite a few boxes. It depends on the jittering you are experiencing. You can take a look at the SimpleSamples demo #4 - it shows a stable pyramid. You should be able to stack a bunch of boxes without any jittering.

- As a sidenote, jittering can become more violent if you apply a large amount of force (like if you have a large gravity value).

Oct 1, 2009 at 8:36 AM

My current gravity is 900 downwards. If I stack 2 boxes, they jump around.


I almost suspect my drawing code to be too slow.


But it's weird, If I place just one box on my "floor", then it also seem to jitter.