How to bounce a Body within Loopshape?

Topics: Developer Forum, User Forum
Nov 24, 2010 at 5:53 AM

I have a Loopshape and I want some objects (anything to start with, png's in the end) to bounce around within a Loopshape indefinitely ignoring gravity and without increasing/decreasing speed when hitting another object in the Loopshape or the Loopshape itself.

Anyone have an idea on what to put in there?  I'm using the BodyFactory but not sure what all the Body attributes are and how to make this jive together.

Nov 24, 2010 at 9:16 AM
Edited Nov 24, 2010 at 9:17 AM

I suggest having a look at the testbed samples and the Box2D manual.

In short you have shapes and bodies and fixtures, which bind one or more shapes to a body. From the manual:

"A fixture binds a shape to a body and adds material properties such as density, friction, and restitution."

Restitution is what you are actually looking for, as it kind of specifies the "bounciness" of an object. Set this to one and you get perfectly elastic collision between objects. As Farseer uses an iterative solver im not so sure about the "bounce around within a Loopshape indefinitely" part though. Maybe you need to check your objects' velocities from time to time and apply some force manually to get them up to speed again ;)

Best regards


Nov 24, 2010 at 9:47 AM

Actually, I managed to get exactly what I wanted about 1hr ago.  I was setting my velocity before the body type was set, which causes the velocity to be 0.  Also, I wasnt setting IsStatic, and of course IgnoreGravity is needed.  Regardless, below gives me exactly what I was looking for.


            _obstacles[0] = FixtureFactory.CreateCircle(World, 10, 1);
            _obstacles[0].Body.Position = new Vector2(50, 80);
            _obstacles[0].CollidesWith = CollisionCategory.All;
            _obstacles[0].CollisionCategories = CollisionCategory.All;
            _obstacles[0].Friction = 0f;
            _obstacles[0].Restitution = 1;
            _obstacles[0].Body.Mass = 50f;
            _obstacles[0].Body.IsStatic = false;
            _obstacles[0].Body.BodyType = BodyType.Dynamic;
            _obstacles[0].Body.LinearVelocity = new Vector2(60, 60);            
            _obstacles[0].Body.IgnoreGravity = true;
            _obstacles[0].Body.ApplyForce(new Vector2(30000, 10000));