2012-05-24 1 views
0

지네와 같은 조각 된 생물을 상상해보십시오. 헤드 세그먼트의 제어로, 바디 세그먼트는 이전 바디 세그먼트에 포인트로 첨부됩니다.Point (1) 또는 Vector2 (1)에 Point (2) 또는 Vector2 (2)를 유지하십시오.

머리가 움직이면서 (지금은 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); 

지금 주위의 제 2 접합부를 드래그합니다 모양의 기관 또는 충돌의에 힘을 적용 - 당신이 원하는 것처럼.

이것은 모두 물리적 인 문제입니다. 정말 원한다면 직접 구현할 수 있습니다. ;)