0
\$\begingroup\$

I created a breakable body but the pieces are not flying around if the ball touches it. What is wrong? Why is the breakable body not breaking into pieces?

I made a video and I enabled debugview, so you can see the bodies:

http://www.vidup.de/v/ihW2L/

//create the breakable body
BreakableBody Polygon;
List<Vertices> list = new List<Vertices>();
Vector2 _origin;
float _scale;
PolygonSprite = Content.Load<Texture2D>("polyplatform");   
uint[] data = new uint[PolygonSprite.Width * PolygonSprite.Height];

PolygonSprite.GetData(data);
Vertices textureVertices = PolygonTools.CreatePolygon(data, PolygonSprite.Width, false);
Vector2 centroid = -textureVertices.GetCentroid();
textureVertices.Translate(ref centroid);

_origin = -centroid;
textureVertices = SimplifyTools.ReduceByDistance(textureVertices, 4f);

list = BayazitDecomposer.ConvexPartition(textureVertices);
_scale = 1f;
Vector2 vertScale = new Vector2(ConvertUnits.ToSimUnits(1)) * _scale;
foreach (Vertices vertices in list)
{
  vertices.Scale(ref vertScale);
}

Polygon = new BreakableBody(list, world, 1);
Polygon.MainBody.BodyType = BodyType.Static;
Polygon.MainBody.Position = new Vector2(graphics.GraphicsDevice.Viewport.Width / 200f, graphics.GraphicsDevice.Viewport.Height / 200f + 1.15f);
Polygon.Strength = 2;
Polygon.MainBody.CollisionCategories = Category.Cat11;
world.AddBreakableBody(Polygon);

//Draw the breakable body
spriteBatch.Draw(PolygonSprite, ConvertUnits.ToDisplayUnits(this.Polygon.MainBody.Position), null, Color.White, this.Polygon.MainBody.Rotation, _origin, _scale, SpriteEffects.None, 0f);
\$\endgroup\$
  • \$\begingroup\$ You need to learn how to debug small programs. Take a very close look at PolygonTools.CreateRectangle (look at the Farseer source, read the doc comments). What parameters does it take? With the parameters you are giving it, will it output what you expect? If you step through in the debugger and inspect the output, does it match your expectation? What about the overload that takes more parameters - are they parameters that you need to set? Repeat the process for new BreakableBody. You will find the bug. \$\endgroup\$ – Andrew Russell Apr 6 '14 at 2:42
  • \$\begingroup\$ I don't understand the two for loops. Why are they using x = -4; x < 4; and y = -1; y < 1; ? How can I set the measures for a breakable body that should be 150 pixel broad and 50 pixel tall? \$\endgroup\$ – Homer_Simpson Apr 6 '14 at 10:24
  • \$\begingroup\$ In the original those loops are intended to set the center position of each of several rectangles to different values. But the original doesn't have valid parameters (as the writer indicated: "off the top of my head"). Like I said: check the overloads for CreateRectangle. There is a version that takes both half-sizes and a position. \$\endgroup\$ – Andrew Russell Apr 8 '14 at 12:51
  • \$\begingroup\$ I changed my code. In addition, I use now another body(no more rectangle). But it's not working. \$\endgroup\$ – Homer_Simpson Apr 8 '14 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.