将Point(2)或Vector2(2)连接到Point(1)或Vector2(1)

时间:2012-05-24 18:04:42

标签: c# xna-4.0

想象一下像蜈蚣这样的分段生物。通过控制头部区段,身体部分通过一个点连接到前一个身体部分。

当头部移动时(目前在8个基数/基本方向),一个点相对于其旋转移动。

public static Vector2 RotatePoint(Vector2 pointToRotate, Vector2 centerOfRotation, float angleOfRotation)
{
    Matrix rotationMatrix = Matrix.CreateRotationZ(angleOfRotation);
    return Vector2.Transform(pointToRotate - centerOfRotation, rotationMatrix);
}

这里要发布图表,但你知道......

 center(2)      point(2)                      center(1)    point(1)



                                                     point(1)                    

                point(2)    ^                                        |
                           / \                                       |
                            |                                        |
 center(2)                                           center(1)      \ /
                                                                     V

我想过为基本精灵使用矩形属性/字段,

private Rectangle bounds = new Rectangle(-16, 16, 32, 32);

并检查主体段内的预定义点是否仍在头部精灵的范围内 虽然我现在在做:

     private static void handleInput(GameTime gameTime)
    {
        Vector2 moveAngle = Vector2.Zero;

        moveAngle += handleKeyboardMovement(Keyboard.GetState()); // basic movement, combined to produce 8 angles
                                                                  // of movement

        if (moveAngle != Vector2.Zero)
        {
            moveAngle.Normalize();
            baseAngle = moveAngle;
        }

        BaseSprite.RotateTo(baseAngle);

        BaseSprite.LeftAnchor = RotatePoint(BaseSprite.LeftAnchor,
 BaseSprite.RelativeCenter, BaseSprite.Rotation); // call RotatePoint method

        BaseSprite.LeftRect = new Rectangle((int)BaseSprite.LeftAnchor.X - 1,
 (int)BaseSprite.LeftAnchor.Y - 1, 2, 2); 
 // All segments use a field/property that is a point which is suppose to rotate around the center
        // point of the sprite (left point is (-16,0) right is (16,0) initially
        // I then create a rectangle derived from that point to make use of the .Intersets method of the
        // Rectangle class

        BodySegmentOne.RightRect = BaseSprite.LeftRect; // make sure segments are connected?

        BaseSprite.Velocity = moveAngle * wormSpeed;

        //BodySegmentOne.RightAnchor = BaseSprite.LeftAnchor;

        if (BodySegmentOne.RightRect.Intersects(BaseSprite.LeftRect)) // as long as there two rects occupy the 
        {                                                             // same space move segment with head

            BodySegmentOne.Velocity = BaseSprite.Velocity;
        }

    }

现在看来,该片段以头部移动但以平行方式移动。我希望得到一个更细致的片段运动,因为它被头部拖动。

据我所知,这种运动的编码将比我在这里所涉及的更多。关于如何看待这个问题的一些提示或指示将不胜感激。

1 个答案:

答案 0 :(得分:0)

我将使用像Farseer这样的物理引擎来描述您需要做什么,但如果您想编写自己的物理引擎,则会同样如此。

  1. 为蜈蚣身体的每个关节点创建一个身体。
  2. 创建一个封装将附加到每个点的外壳的Shape。
  3. 使用夹具连接主体和形状。这会在您的蜈蚣中创建一个链接。
  4. 使用SliderJoint附加多个链接。
  5. 例如 - 假设每个链接的外壳都是圆形,这里是如何创建两个链接并将它们连接在一起。

        Fixture fix1 = FixtureFactory.CreateCircle(_world, 0.5f, 1, new Vector2(-5, 5));
        fix1.Body.BodyType = BodyType.Dynamic;
    
        Fixture fix2 = FixtureFactory.CreateCircle(_world, 0.5f, 1, new Vector2(5, 5));
        fix2.Body.BodyType = BodyType.Dynamic;
    
        JointFactory.CreateSliderJoint(_world, fix1.Body, fix2.Body, Vector2.Zero, Vector2.Zero, 10, 15);
    

    现在在形状上的任何实体或碰撞上施加力将拖动第二个关节 - 就像你想要的那样。

    这一切都只是坚持物理 - 所以你可以实现自己的,如果你真的想要。 ;)

相关问题