Topics: Developer Forum
Feb 14, 2007 at 1:51 AM
I was looking at how to write an implementation of the collision grid and when I looked at yours I realized it could benefit from some optimization and refactoring. I would like to help this project what is your policy on contributing?
Feb 14, 2007 at 2:27 AM
Hi BioSlayer,

Optimization is on my list and I'd love to hear what suggestions you have for refactoring.

Due to the sporadic work habits I have and my lack of time to coordinate with other developers I prefer to do all the code edits to the base source myself. If you want to make some changes to the released code and send them to me I will gladly take a look and add them if they make sense.

Thanks for your interest.

Feb 14, 2007 at 5:04 AM
I implemented a Grid like yours for my Physics2D project and it’s under 100 lines of code, and it works. Would you be interested it this?
Feb 14, 2007 at 11:49 AM
Edited Feb 14, 2007 at 11:49 AM
Yes, I'd like to see it. Is your Physics2D project posted somewhere I can take a look?

What did you do differently?
Feb 14, 2007 at 4:30 PM
Edited Feb 14, 2007 at 4:48 PM
The project is posted on sourceforge but it is not up to date. Because the version I’m working on is a complete rewrite, and it is not complete.

I have only one 2D array storing the distances and only one class.
Here’s the code (it uses my classes but should be easy to convert):

    public sealed class Grid
        float gridSpacing;
        float gridSpacingInv;
        BoundingBox2D box;
        float[,] nodes;
        public Grid(IShape2D shape, float spacing)
            //prepare the shape.
            Matrix2D old = shape.Matrix;
            Matrix2D ident = Matrix2D.Identity;
            shape.ApplyMatrix(ref ident);
            this.box = shape.BoundingBox2D;
            this.gridSpacing = spacing;
            this.gridSpacingInv = 1 / spacing;
            int xSize = (int)Math.Ceiling((box.Upper.X - box.Lower.X) * gridSpacingInv) + 1;
            int ySize = (int)Math.Ceiling((box.Upper.Y - box.Lower.Y) * gridSpacingInv) + 1;
            this.nodes = new float[xSize, ySize];
            Vector2D vector = box.Lower;
            for (int x = 0; x < xSize; ++x, vector.X += spacing)
                vector.Y = box.Lower.Y;
                for (int y = 0; y < ySize; ++y, vector.Y += spacing)
                    nodes[x, y] = shape.GetDistance(vector);
            //restore the shape
            shape.ApplyMatrix(ref old);
        public bool TryGetIntersection(Vector2D vector, out PointInfo result)
            if (BoundingBox2D.TestIntersection(box, vector))
                int x = (int)Math.Floor((vector.X - box.Lower.X) * gridSpacingInv);
                int y = (int)Math.Floor((vector.Y - box.Lower.Y) * gridSpacingInv);
                float xPercent = (vector.X - (gridSpacing * x + box.Lower.X)) * gridSpacingInv;
                float yPercent = (vector.Y - (gridSpacing * y + box.Lower.Y)) * gridSpacingInv;
                float bottomLeft = nodes[x, y];
                float bottomRight = nodes[x + 1, y];
                float topLeft = nodes[x, y + 1];
                float topRight = nodes[x + 1, y + 1];
                if (bottomLeft <= 0 ||
                    bottomRight <= 0 ||
                    topLeft <= 0 ||
                    topRight <= 0)
                    float top, bottom, distance;
                    MathHelper.Lerp(ref topLeft, ref  topRight, ref xPercent, out top);
                    MathHelper.Lerp(ref bottomLeft, ref bottomRight, ref xPercent, out bottom);
                    MathHelper.Lerp(ref bottom, ref top, ref yPercent, out distance);
                    if (distance <= 0)
                        float right, left;
                        MathHelper.Lerp(ref bottomRight, ref topRight, ref yPercent, out right);
                        MathHelper.Lerp(ref bottomLeft, ref topLeft, ref yPercent, out left);
                        Vector2D normal;
                        normal.X = right - left;
                        normal.Y = top - bottom;
                        Vector2D.Normalize(ref normal, out normal);
                        result = new PointInfo(vector, normal, distance);
                        return true;
            result = null;
            return false;
Feb 14, 2007 at 8:09 PM
Edited Feb 14, 2007 at 8:09 PM
Well, I must agree, your code looks much nicer than mine. :-) I like your use of the MathHelper methods. That seems to clean up a lot.

I will definately look at refactoring and streamlining my grid code. Thanks for the code snippet.

My whole engine needs a development iteration... it's just matter of having the time.

I look forward to seeing your finished engine.
Feb 14, 2007 at 11:34 PM
I also look forward to my project being complete :) and I wish you well on yours.
Maybe someday we could do a merging of sorts. (If that doesn’t sound too presumptuous)
Feb 15, 2007 at 4:30 PM
Edited Feb 15, 2007 at 4:32 PM
What are your plans for your physics engine? Are you creating if for a particular game, for the community of c# game developers, or just for the fun of it?

You should start a CodePlex project for it. SourceForge seems kinda clunky to me.
Feb 15, 2007 at 11:07 PM
It’s meant to be used by the C# community and a game I want to rewrite from scratch. (The Ur-Quan ReMasters also on sourceforge) Mine doesn’t use XNA at all so it’s possible to get it working on Linux using mono.

I managed to get a demo working and it works rather well. Except for a strange problem with it. I tried getting a tower of boxes to test stacking and they stack but then they start to rock back and forth then fall. Also sometimes the boxes start rotating when resting. Did you have problems like these with your implementation?

It uses the sweep and prune for broad phased collision detection. The Grid for narrow and the sequential impulses from Box2D for the solver.
Feb 16, 2007 at 1:40 AM
I figured out what my problem was: I was not removing all of the invalid arbiters each time step.
Feb 19, 2007 at 3:24 AM
Well I've released a version of my rewrite of Physics2D.
Its called Physics2D.Net so if you want to download it and take a look you can.
Heres a Link:
For some reason the the newest version of Tao.OpenGl is very slow. so It might run rather slowly.

I'm impressed by codeplex and I would be willing to change over if it has SVN support. Do you know if it does?
Feb 23, 2007 at 5:24 PM
Cool, I will take a look.

If you have VS 2005, you can use codeplex as a Team Services server which has all the normal code control features. If you poke around the codeplex site you should find documentation on it.
Feb 24, 2007 at 3:14 PM
Guys, keep up the awesome work. I am currently working on a game project using the Physics2D (the new one already - its super!), and I'll keep an eye on the progress etc. Hope I could be able to help at some point.
Feb 24, 2007 at 11:34 PM
Well I decided to go with Google Code. The entire site Is made using Google stuff so all you need is a gmail account to post and stuff.
I’ve also released another version on it. This one is updated to use the newest version of Box2D's Solver. And it has allot of bug fixes and optimizations.

Here’s the new address: