2012-05-24 41 views
0

想象一个分段生物,如蜈蚣。通过控制头部段,身体段通过一个点附着到先前的身体段。保留点(2)或Vector2(2)加入点(1)或Vector2(1)

随着头部移动(现在在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; 
     } 

    } 

目前的情况是,段移动头部,但以并行的方式。我想在头部被拖动时获得细分的更细微的移动。

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

+0

你想自己实现这个,还是使用像Farseer(http://farseerphysics.codeplex.com)这样的物理引擎可以吗?我问的原因是你想做的事情不是微不足道的,而是涉及物理引擎概念的知识。 – Ani

+0

标称的解决方案是写我自己的解决方案;但是,如果使用已建立的系统将加速我提出解决方案的能力,我将全力以赴,并将进一步深入Farseer。 – Dialock

回答

0

我将描述你需要做什么使用像Farseer物理引擎,但如果你想编写自己的物理引擎相同。

  1. 为蜈蚣身体的每个关节点创建一个身体。
  2. 创建一个封装将连接到每个点的外壳的Shape。
  3. 使用夹具附着身体和形状。这会在您的蜈蚣中创建一个链接。
  4. 使用SliderJoint附加多个链接。

例如 - 假设每个链接的外壳是一个圆圈,下面介绍如何创建两个链接并将它们连接在一起。

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

现在对任何物体施加作用力或形状上的碰撞将会拖动第二个关节 - 就像您想要的那样。

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