Polygon Body Collusion buggy?

Topics: Developer Forum, User Forum
Dec 8, 2007 at 9:43 AM
Well I really like this engine cause its like half-life in 2D ^^
But I have a little problem with my Polygonbodys. I'm currently trying to make a Vector Game where the ships and other things are basicly made out of Vertices but when my ship is pointing upwards and falling into another one pointing upwards then they sometimes slide into each other. I think this happens only when it hits with its top point thats in the middle the bottom right point of the other one.

Is there a way how I can fix this? currently the Geom-Set code looks like this:

Body = BodyFactory.Instance.CreateRectangleBody(PSim, 30, 30, 5);
Body.Position = Position;

Vertices bverts = new Vertices();
bverts.Add(new Vector2(0, -30));
bverts.Add(new Vector2(20, 20));
bverts.Add(new Vector2(-20, 20));
Geom = new Geom();
Geom = GeomFactory.Instance.CreatePolygonGeom(PSim, Body, bverts, 0);
Geom.RestitutionCoefficient = .4f;
Geom.FrictionCoefficient = .2f;
Geom.CollisionCategories = collisionCategory;
Geom.CollidesWith = collidesWith;

And the toppoint 0,-30 seems to slide in one other Body when it hits another vertice for example 20,20 at 19,20 or 19,19... but when it hits for example at 17,20 then the collusion is correct.
Coordinator
Dec 8, 2007 at 11:17 AM
If you have a body with sharp points, then you may need to use smaller collisionGridCellSize values then the default.

By default, if you pass 0 for the collisionGridCellSize to the CreatePolygonBody method, the method will calculate a collisionGridCellsize for you based on the size of the AABB. If you have a geom with sharp points you will probably want a smaller value than the default. You can either pass in your own non-zero value or you can adjust the default calculation by setting the GeomFactory.GridCellSizeAABBFactor property. This property is used to calculate the default collisionGridCellSize. Currently it is set to .1 which means the collisionGridCellSize will be 1/10th the smallest dimension of you geoms AABB.

If you follow the code into the GeomFactory, you should be able to see what I'm talking about.

-Jeff
Coordinator
Dec 8, 2007 at 11:46 AM
BloodyRain - I also sort of have the same problem. Could you tell me if it works for you?
Dec 8, 2007 at 5:07 PM
GeomFactory.Instance.GridCellSizeAABBFactor = 0.01f;
Geom = GeomFactory.Instance.CreatePolygonGeom(PSim, Body, HullVertices, 0.1f); <-- 0.01f or less makes the game load only after minutes and raises the RAM Usage by +100 MB, for above its the same at 0.001f

well I tried now this but now it only slides differently into the other one... what makes me wonder is that it ONLY slides into when both are pointing to 100% top and it seems only to work with the bottom right vertice. When i put the lower one thats static and into which it's sliding everytime to the left so that the bottom left vertice is hit it collides correctly. Even with the default values. I'm now gonna take a look into the GeomFactory.
Dec 8, 2007 at 5:50 PM
OK I don't understand a single thing in this factory ^^; was the same with http://www.ziggyware.com/news.php?readmore=442 but it worked. I now fixed it temporary with making the lower vertices to -10,9 + -9,10 (a small 45° line) and 9,10 + 10,9 for the other side, now it seems like nothings gonna slide into one other poly. But I hope I understand one day how this works ^^;

btw what I forgot to tell is that I use 1.0.0.1 because 1.0.0.2 is already for XNA Refresh 2.
Dec 8, 2007 at 11:28 PM
another thing that bugs me now is how I get my spaceship flying in a way that it's still controllable.

i tried this but its not very easy to handle nor what i want...

if (keyb.IsKeyDown(Keys.Left)) { Objs0.Body.RotationalDragCoefficient = 2000f; Objs0.Body.ApplyTorque(-10000); }
if (keyb.IsKeyDown(Keys.Right)) { Objs0.Body.RotationalDragCoefficient = 2000f; Objs0.Body.ApplyTorque(10000); }
Objs0.Body.ApplyForce(XNACmds.AddRot(new Vector2(0, -Objs0.Speed ), MyCmds.RadiansToDegree(Objs0.Body.Rotation)));

if (keyb.IsKeyUp(Keys.Left) && keyb.IsKeyUp(Keys.Right)) { Objs0.Body.ApplyTorque(-Objs0.Body.Torque*16); Objs0.Body.ClearTorque(); };

if (keyb.IsKeyDown(Keys.Up)) { Objs0.Speed += 25; }
if (keyb.IsKeyDown(Keys.Down)) { Objs0.Speed -= 25; }

the greatest problem is that the turning speed doesn't go down to zero like i would like it, maybe I make a video from my previous version, it's engine is more than crappy but the turnings worked well. the physics in farseer are perfect for later battles so that impacting missles could bounce a ship a bit around but currently the physics are in my way to get a ship flying cause I'm not good in physics ^^;
Coordinator
Dec 9, 2007 at 11:53 AM
I changed the collisionGridCellSize of my polygon geometries. I changed it to 0.1f, that does not fix the problem and it takes a lot of time to create the geometries. (~5sek)

My problem seems to be related, but it does not affect just one point (bottom right vertice like yours). It affects all corners (vertices).

I have a demo here: http://files-upload.com/files/668274/SmartDrawPhysics.avi

btw: sorry about the file host. (counter and slow speed)
Coordinator
Dec 9, 2007 at 10:46 PM
You really need to get some type of debug visualiation going so you can see the collision grid and the vertices. This would help debug the problem.
Coordinator
Dec 10, 2007 at 4:43 PM

crashlander wrote:
You really need to get some type of debug visualiation going so you can see the collision grid and the vertices. This would help debug the problem.


I would be greatful if you would help me debug this problem. It's been a pain so far.

I've uploaded a new video:
http://www.megafileupload.com/en/file/28183/SmartDrawGeometry-avi.html

This one shows the problem, but with debugging enabled.
Dec 16, 2007 at 5:52 AM

BloodyRain wrote:
if (keyb.IsKeyUp(Keys.Left) && keyb.IsKeyUp(Keys.Right)) { Objs[0].Body.ApplyTorque(-Objs[0].Body.Torque*16); Objs[0].Body.ClearTorque(); };


I think this line is your problem. Body.Torque is reset to zero every time you call update(), because it applies the torque and then the body starts rotating. You probably want Body.AngularVelocity instead. Something like

Objs[0].Body.ApplyTorque(-Objs[0].Body.AngularVelocity*16);

should be a good starting point.
Dec 16, 2007 at 3:06 PM
Ok thanks, I'll try that out when I get back my test version, I lost it sadly and I'm to tired from my apprenticeship to build a new one, what could help would be a way to convert the direction directly into the new force so the ship turns instantly its moving direction with the turning.
Dec 16, 2007 at 4:23 PM
Edited Dec 16, 2007 at 5:01 PM

BloodyRain wrote:
Ok thanks, I'll try that out when I get back my test version, I lost it sadly and I'm to tired from my apprenticeship to build a new one, what could help would be a way to convert the direction directly into the new force so the ship turns instantly its moving direction with the turning.


In other words, if you're facing up, how to apply a force so that you actually go up?

Basically, you convert the ship's rotation to a unit vector and apply a force in that direction. The resulting code should be something like this...

Vector2 forceDirection = new Vector2( (float)Math.cos(rotation), (float)Math.sin(rotation) );
Body.ApplyForce( forceDirection * acceleration );
Dec 16, 2007 at 5:11 PM
thanks, thats the code i needed ^^ I'll try to build a new test version when i have some spare energy in the next time.