0

I made my borders with this:

class Maze
    {
        private Body _agentBody;
        private Sprite _box;
        private GameplayScreen _screen;
        private float _offset;

        public Maze(World world, GameplayScreen screen, Vector2 position)
        {
            _agentBody = BodyFactory.CreateBody(world, position);
            _agentBody.BodyType = BodyType.Dynamic;
            _agentBody.IsStatic = true;
            _agentBody.Restitution = 0.2f;
            _agentBody.Friction = 0.2f;

            _offset = ConvertUnits.ToDisplayUnits(1f);
        // spodek
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(1f, 0.05f, new Vector2(0f, 1f), 0), 1f));
            // spodek
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(1f, 0.05f, new Vector2(0f, -1f), 0), 1f));
            // pravy bok
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(0.05f, 1f, new Vector2(1f, 0f), 0), 1f));
            // levy bok
            _agentBody.CreateFixture(new PolygonShape(PolygonTools.CreateRectangle(0.05f, 1f, new Vector2(-1f, 0f), 0), 1f));   
            _screen = screen;

            //GFX
            AssetCreator creator = _screen.ScreenManager.Assets;
            _box = new Sprite(creator.TextureFromVertices(PolygonTools.CreateRectangle(1f, 0.05f),
                                                           MaterialType.Blank, Color.White, 1f));
        }

        public Body Body
        {
            get { return _agentBody; }
        }

        public void Draw()
        {
            SpriteBatch batch = _screen.ScreenManager.SpriteBatch;
            batch.Draw(_box.Texture, ConvertUnits.ToDisplayUnits(_agentBody.Position), null,
                        Color.White, _agentBody.Rotation, _box.Origin + new Vector2(0f, _offset), 1f, SpriteEffects.None, 0f);
            batch.Draw(_box.Texture, ConvertUnits.ToDisplayUnits(_agentBody.Position), null,
                        Color.White, _agentBody.Rotation, _box.Origin + new Vector2(0f, -_offset), 1f, SpriteEffects.None, 0f);

            batch.Draw(_box.Texture, ConvertUnits.ToDisplayUnits(_agentBody.Position), null,
                        Color.White, _agentBody.Rotation + MathHelper.Pi / 2f, _box.Origin + new Vector2(0f, _offset), 1f, SpriteEffects.None, 0f);
            batch.Draw(_box.Texture, ConvertUnits.ToDisplayUnits(_agentBody.Position), null,
                        Color.White, _agentBody.Rotation + MathHelper.Pi / 2f, _box.Origin + new Vector2(0f, -_offset), 1f, SpriteEffects.None, 0f);
        }

    }

And these are my little particles:

for (int i = 0; i < 8; i++)
            {
                _sands[i] = BodyFactory.CreateRectangle(_world, 0.05f, 0.05f, 1f);
                _sands[i].IsStatic = false;
                _sands[i].Restitution = 0.1f;
                _sands[i].Friction = 0.1f;
                _sands[i].Position = new Vector2(1.8f + i * 0.2f, 2.2f);
            }

            _sand = new Sprite(ScreenManager.Assets.TextureFromShape(_sands[0].FixtureList[0].Shape,
                                                                        MaterialType.Dots,
                                                                        Color.SandyBrown, 0.8f));

I draw it this way:

foreach (Body sand in _sands)
            {
                spriteBatch.Draw(_sand.Texture, ConvertUnits.ToDisplayUnits(sand.Position), null, Color.SandyBrown, sand.Rotation, _sand.Origin, 1f, SpriteEffects.None, 0f);
            }
_maze.Draw();

But I can't figure out why if I rotate with borders then why partlicles are still in place. I tried change restitution of particles and when there is 1f they are restitute (bouncing) allright and I can rotate with borders and they restitute from new position of borders but when I have settings like above particles fall down, the ones which are inside of borders they stopped at bottom border and others fall down entirely. So after start I have first image and after I rotate with borders I get seccond image. What I am doing wrong? Why when I change restitution they are bouncing a with 0.2 they are not?

After start Try to rotate with borders

Edit: New lines in maze constructor:

agentBody = BodyFactory.CreateBody(world, position);
            _agentBody.BodyType = BodyType.Dynamic;
            _agentBody.IgnoreGravity = true;
            _agentBody.Restitution = 0.1f;
            _agentBody.Friction = 1f;

            _offset = ConvertUnits.ToDisplayUnits(1.5f);

            FixtureFactory.AttachRectangle(3f, 0.1f, 1f, new Vector2(0, 1.55f), _agentBody);
            FixtureFactory.AttachRectangle(3f, 0.1f, 1f, new Vector2(0f, -1.55f), _agentBody);
            FixtureFactory.AttachRectangle(width, 3f, 1f, new Vector2(-1.55f, 0f), _agentBody);
            FixtureFactory.AttachRectangle(width, 3f, 1f, new Vector2(1.55f, 0f), _agentBody);

This is how it looks with debug view:

Newest look with debug view

Rotating with body:

public override void HandleInput(GameTime gameTime, InputState input)
{
    if (input == null)
        throw new ArgumentNullException("input");

    // Read in our gestures
    foreach (GestureSample gesture in input.Gestures)
    {
        if (gesture.GestureType == GestureType.HorizontalDrag)
        {
            if (gesture.Delta.X < 0)
            {
                _maze.Body.Rotation += 0.02f;
            }
            else if (gesture.Delta.X > 0)
            {
                _maze.Body.Rotation -= 0.02f;
            }
        }
    }
Libor Zapletal
  • 13,752
  • 20
  • 95
  • 182

1 Answers1

1

The little particle bodies are probably sleeping once they stop moving. The frame around them is a static body so the engine does not expect it to ever move, but you are moving it. In this situation the little particle bodies will not wake up.

You will need to set the little particle bodies so that they cannot sleep, or make the frame around them a dynamic or kinematic body. As a general rule, if a body is gonna move, don't make it a static body.

iforce2d
  • 8,194
  • 3
  • 29
  • 40
  • You´ve right. Their were sleeping. So I made my maze dynamic and set IgnoreGravity to true but now I have problem that particles go thru bottom border but in debugview fixture looks allright. But you answered my question so thanks :) – Libor Zapletal Oct 15 '12 at 15:35
  • Are you rotating the frame using SetTransform? That is kinda like teleporting things around, and can lead to some un-realistic behavior. You should be moving things by setting the velocity or applying forces/impulses. – iforce2d Oct 16 '12 at 00:55
  • I am rotating by ´body.Rotation´ when there is user input. I think this is allright. But articles when they appear they fall down a go thru bottom border (I am not moving with maze at all). When I remove line with static to true I must changed code with fixtures but when I looked at debugview fixtures are allright, they are set to borders of maze. – Libor Zapletal Oct 16 '12 at 07:39
  • No problem, I added to question. – Libor Zapletal Oct 16 '12 at 12:22
  • Okay. I'm not familiar with this framework but it seems like changing that variable is reaching directly into the private parts of the body and fiddling with it, rather than the expected methods of setting the velocity or applying forces/impulses. Look into SetAngularVelocity. To get it rotating around its center you will probably have to pin it in place with a revolute joint, or make it a kinematic body so it ignores gravity. – iforce2d Oct 16 '12 at 15:05
  • But I think there is no problem with gravity (or rotation), the maze is still in its position. Problem is that my particles go thru fixture of maze (and I am not rotating with it). – Libor Zapletal Oct 16 '12 at 16:34