粒子不会根据边界移动

粒子不会根据边界移动

本文介绍了Farseer-粒子不会根据边界移动/弹跳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以此为界:

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);
        }

    }

这些是我的小粒子:

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));

我这样画:

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();

但是我不知道为什么如果我旋转边框,为什么微粒仍然存在。我尝试更改粒子的归还状态,当出现1f时,它们恢复原状(反弹),并且我可以随着边界旋转,并且它们从新的边界位置恢复原状,但是当我有类似上面的粒子的设置掉落时,那些位于边界内的粒子他们停在底部边界,其他人则完全倒下。因此,在开始之后,我得到了第一张图像,并在旋转了边界之后得到了第二张图像。我做错了什么?为什么当我更改归还金额时,它们以0.2反弹却不是?

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?


编辑
迷宫构造函数中的新行:

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);

这是调试视图的样子:

旋转正文:

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;
            }
        }
    }


推荐答案

这些小粒子体一旦停止移动就可能正在睡觉。它们周围的框架是静态的,因此引擎不希望它移动,而是在移动它。在这种情况下,小粒子体不会醒来。

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.

这篇关于Farseer-粒子不会根据边界移动/弹跳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:22