【问题标题】:Farseer - Particles doesn´t move/bounce accord to bordersFarseer - 粒子不会根据边界移动/反弹
【发布时间】:2012-10-11 06:46:56
【问题描述】:

我以此为界:

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 反弹而不是?

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

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

【问题讨论】:

    标签: windows-phone-7 windows-phone-7.1 box2d farseer


    【解决方案1】:

    一旦停止运动,小粒子体可能正在睡觉。它们周围的框架是一个静态的物体,所以引擎不希望它移动,但你正在移动它。在这种情况下,小粒子体不会醒来。

    您需要将小粒子体设置为无法休眠,或者将它们周围的框架设置为动态或运动体。作为一般规则,如果一个物体要移动,不要让它成为一个静止的物体。

    【讨论】:

    • 你说得对。他们在睡觉。所以我让我的迷宫动态并将 IgnoreGravity 设置为 true 但现在我遇到了粒子穿过底部边框但在 debugview 夹具中看起来没问题的问题。但是你回答了我的问题,所以谢谢:)
    • 您是否使用 SetTransform 旋转框架?这有点像传送周围的东西,并可能导致一些不切实际的行为。您应该通过设置速度或施加力/脉冲来移动物体。
    • 当有用户输入时,我正在旋转“body.Rotation”。我认为这没关系。但是当它们出现时,它们会从底部边界掉下来(我根本没有在迷宫中移动)。当我将带有 static 的行删除为 true 时,我必须使用固定装置更改代码,但是当我查看 debugview 固定装置没问题时,它们被设置为迷宫的边界。
    • 没问题,我加了问题。
    • 好的。我不熟悉这个框架,但似乎改变这个变量是直接进入身体的私人部位并摆弄它,而不是设置速度或施加力/脉冲的预期方法。查看 SetAngularVelocity。要让它围绕中心旋转,您可能需要使用旋转接头将其固定到位,或者使其成为运动体,以便忽略重力。
    猜你喜欢
    • 2018-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    • 2018-04-24
    相关资源
    最近更新 更多