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

Shallow impact

Dec 3, 2010 at 7:56 AM

I have a box that has a restituion of 1 and as I bounce particles (very small spheres) off it they come out perfectly reflected - as long as the angle isn't too shallow. If it is very shallow, the particles follow the wall instead of bouncing off it. I've tried turning of rotation for the particles but that didn't help. Any ideas what's going on?

I'll create a test-bed example if no explanation springs to mind.

(Is it possible to include images in this forum?)

Dec 3, 2010 at 11:07 PM

I dont have an explanation springing to my mind. But i was wondering if you are "the" Mattias Fagerlund that used to be active in GLScene and ODE for Delphi ? If so, what a small world lol

Dec 4, 2010 at 2:14 AM

It would be great if you could upload a Testbed sample somewhere.

Dec 4, 2010 at 7:57 AM

I certainly am that Mattias Fagerlund - the road that led me to GLScene and importing OpenODE to Delphi also led me here ;) Good to see some familiar faces.

Dec 4, 2010 at 9:02 AM

Here's a test that demonstrates the problem - and I figured out the problem while creating the test - it's a scaling issue. 
In the test below, when the scale is 1 or 1.5 the problem occurs 100% of the time (though it's hard to see because the rendering is tiny). If I
set the scale to 2 it only happens every few particles as in my other open issue, , so likely the two issues are related.

I'll try to increase the scale of my physics world and see if the problem resolves itself...


public class ShallowImpactTest : Test { private Random _random = new Random(); private List<Fixture> _particles = new List<Fixture>(); private static float scale = 1; private static float xoffset = -2f * scale; private ShallowImpactTest() { World.Gravity = new Vector2(0, 0); Fixture rectangle = FixtureFactory.CreateRectangle(World, scale * 2f, scale * 0.4f, 1f); // <RectangleCollider Position="469,366" Size="200,40" /> // rectangle.Body.Rotation = 0.65f; rectangle.Body.Position = new Vector2(scale * 4.69f + xoffset, scale * 3.66f); rectangle.Body.IsStatic = true; rectangle.Body.BodyType = BodyType.Static; rectangle.Restitution = 1; } internal static ShallowImpactTest Create() { return new ShallowImpactTest(); } public override void Update(GameSettings settings, GameTime gameTime) { base.Update(settings, gameTime); Fixture circle = GenerateParticle(); while (_particles.Count() > 300) { World.RemoveBody(_particles.First().Body); _particles.RemoveAt(0); } } private Fixture GenerateParticle() { double angl = 7 * Math.PI / 180f; Vector2 dir = new Vector2((float)Math.Cos(angl), (float)Math.Sin(angl)); dir.Normalize(); Vector2 pos = new Vector2(scale * 2.45f + xoffset, scale * 3.14f); // <ParticleProducer Position="245,314" Radius="3" Angle="7" /> // Random position float angle = (float)(_random.NextDouble() * 2 * Math.PI); float radius = (float)(_random.NextDouble() * scale * 0.03); Vector2 position = new Vector2( (float)(pos.X + (Math.Cos(angle) * radius)), (float)(pos.Y + (Math.Sin(angle) * radius))); float speed = (float)scale * 5; Vector2 linearVelocity = Vector2.Multiply(dir, speed); Fixture circle = FixtureFactory.CreateCircle(World, scale * 0.01f, 1); circle.Body.IsStatic = false; circle.Body.LinearVelocity = linearVelocity; circle.Body.Position = position; circle.Restitution = 1; circle.CollisionCategories = CollisionCategory.Cat31; circle.CollidesWith = CollisionCategory.All & ~CollisionCategory.Cat31; _particles.Add(circle); return circle; } }

Dec 5, 2010 at 12:03 PM
Edited Dec 5, 2010 at 12:20 PM

I've ported the code to Box2D and it seems like it performs like it should. I've also run the code in Box2D.XNA and the bugs seems to come from there. Could you create a test that fails every time?

Edit: A test that fires a single particle at an angle that causes the particle to have the wrong direction after collision.

Dec 5, 2010 at 12:10 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.