2011-09-05 3 views
1

당신이 farseer에 익숙한 분들을 위해, 나는 충돌 감지에 관한 질문이 있습니다. 나는 많은 것을 읽었으며 Joran Omark의 XNAtutorial에 대한 훌륭한 비디오 튜토리얼을 보았습니다. 불행하게도 그는 구식 버전의 farseer (2006 버전)를 사용했기 때문에 광산이 작동하려면 약간 조정해야했습니다. 이제는 내 충돌 감지를 제외하고는 모두 작동합니다. 나는 그 사람들이 일하도록 할 수없는 것 같습니다. 나는 screenmanager 접근 방식에 가서 좋은 별도의 클래스를 만들기로 결정했습니다.farseer 물리 엔진에서 충돌 감지 3.3

내 GameplayScreen이

public class GamePlayScreen : GameScreen 
{ 
    Texture2D stewieTexture; 
    Texture2D floorTexture; 
    Sprite stewie; 
    Sprite floor; 
    World world; 

    public GamePlayScreen() 
    { 
     world = new World(new Vector2(0, 9.81F)); 
     EnabledGestures = GestureType.Flick; 
    } 

    public override void LoadContent() 
    { 
     stewieTexture = ScreenManager.Game.Content.Load<Texture2D>("playerStewie"); 
     floorTexture = ScreenManager.Game.Content.Load<Texture2D>("Floor"); 

     this.stewie = new Sprite(); 
     this.floor = new Sprite(); 
     this.stewie.LoadGraphicsContent(ScreenManager.SpriteBatch, this.stewieTexture, world); 
     this.floor.LoadGraphicsContent(ScreenManager.SpriteBatch, this.floorTexture, world); 
     // very ugly unfortunately for now like this.. We need Textures for our sprite that's why 
     this.stewie.Initialize(world, BodyType.Dynamic ,new Vector2(16000,1500)); 
     this.floor.Initialize(world, BodyType.Static, new Vector2(1500, 16000)); 
    } 

    public override void HandleInput(InputState input) 
    { 
     foreach (GestureSample gesture in input.Gestures) 
     { 
      if (gesture.GestureType == GestureType.Flick) 
      { 
       stewie.PhysicsBody.ApplyForce(Vector2.Divide(gesture.Delta, 0.5f)); 
      } 
     } 
    } 

    public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen) 
    { 
     world.Step((float)gameTime.ElapsedGameTime.TotalSeconds); 
     this.stewie.Update(gameTime, world); 
     this.floor.Update(gameTime, world); 
     base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen); 
    } 

    public override void Draw(GameTime gameTime) 
    { 
     // TODO: Add your drawing code here 
     ScreenManager.SpriteBatch.Begin(); 

     this.stewie.Draw(gameTime); 
     this.floor.Draw(gameTime); 
     ScreenManager.SpriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

그리고 내 스프라이트 클래스

public class Sprite 
{ 
    private Animation graphicsImage; 
    private Body physicsBody; 
    //Fixture test 
    //private Fixture fixtureBody; 
    private Vector2 _screenCenter; 

    private float pixelsPerMeter = 50; //needed to convert pixels per meter , because the gesture.position is in meters 
    public Body PhysicsBody { get { return physicsBody; } } 

    public Sprite() 
    { 
     this.graphicsImage = new Animation(); 
    } 
    /// <summary> 
    /// Initialize the sprite sets up position , creates rectangle from bodyfactory 
    /// </summary> 
    /// <param name="world"> takes a reference to the worldobject</param> 
    public void Initialize(World world, BodyType bodyType,Vector2 position) 
    { 
     this.graphicsImage.Initialize(); 

     this.physicsBody = BodyFactory.CreateRectangle(world, this.graphicsImage.Size.X/pixelsPerMeter, this.graphicsImage.Size.Y/pixelsPerMeter, 1f); 
     this.graphicsImage.Position = position/pixelsPerMeter; 
     this.physicsBody.Position = this.graphicsImage.Position; 
     this.physicsBody.BodyType = bodyType; 
    // this.fixtureBody = FixtureFactory.AttachRectangle(this.physicsBody.Position.X/pixelsPerMeter, this.physicsBody.Position.Y/pixelsPerMeter, 3f, new Vector2(100, 100), this.physicsBody); 

     //this.physicsBody.Restitution = 0.7f; 
     Debug.WriteLine("Initialize Sprite"); 
     Debug.WriteLine(this.graphicsImage.Position); 
    } 

    /// <summary> 
    /// LoadGraphicsContent for the sprite call this function in the LoadContent (initialize also works) 
    /// </summary> 
    /// <param name="spriteBatch">takes a reference to a spritebatch</param> 
    /// <param name="texture">takes a reference to texture2D</param> 
    public void LoadGraphicsContent(SpriteBatch spriteBatch, Texture2D texture, World world) 
    { 
     this.graphicsImage.LoadGraphicsContent(spriteBatch, texture); 
    } 


    /// <summary> 
    /// Update sprite for graphicsImage position , and rotation needs to be called in the main Update function of the game 
    /// </summary> 
    /// <param name="gameTime">reference to gameTime</param> 
    /// <param name="world">reference to worldobject</param> 
    public void Update(GameTime gameTime, World world) 
    { 
     this.graphicsImage.Update(gameTime); 
     this.graphicsImage.Position = this.physicsBody.Position; 
     this.graphicsImage.Rotation = this.physicsBody.Rotation; 

    } 

    /// <summary> 
    /// Draw sprite method needs to be called in the main Draw function of the game 
    /// </summary> 
    /// <param name="gameTime"></param> 
    public void Draw(GameTime gameTime) 
    { 
     this.graphicsImage.Draw(gameTime); 
    } 
} 

}

이미 언급 한 바와 같이

, 내 충돌이 작업을 가까이 가도처럼 보인다. 필자는 Farseer 3.3을 사용하는 현재 샘플을 살펴 보았습니다. 예를 들어 여기 http://farseerphysics.codeplex.com/releases/view/64108 그리고 HelloWorld 예제. 이전 버전의 충돌에 사용 된 GeomFactory에 대한 내용을 읽었습니다. 그러나 Farseer의 새로운 버전에서는 Bodyfactory가 모든 것을 다룰 수있는 곳으로 바뀌 었습니다. 내 충돌이 작동하지 않는 이유는 누구나 알 수 있습니까?

+0

코드에 문제가 있음을 분명히 알 수는 없지만 모든 표준 사항이 제대로 작동하는 것 같습니다. 올바른 장소에서 그림을 그리는 것이 확실합니까? 당신이 게시 한 것으로부터 Sim 유닛에서 스크린 유닛으로 올바르게 변환하는 것을 볼 수 없습니다. 항상 첫 번째 인스턴스에서 DebugviewXNA를 연결하는 것이 좋습니다. – Nick

+0

늦게까지 답변을 드려 죄송합니다. 의견을 보지 못했지만 조언을 주셔서 감사합니다. 실제로 단위 변환이 올바르게 진행되지 않는다는 느낌이 들었습니다. Daven은 DebugViewXNA를 시도했습니다. 그러나 그것은 계획처럼 들립니다. 만약 내가 어떤 식 으로든 해결한다면 나는 대답을 게시 할 것이다. – Shinji

답변

0

네가 엔진으로 장난 치면서 얻은 것에서 Meters의 화면 좌표를 사용하기 때문에 네가 위치를 잡아 내고있다. 즉, 화면 해상도가 10 분의 16 (ish) 미터 (480x800 픽셀) 밖에 될 수 없기 때문입니다. 그래서 16000, 1500 픽셀의 위치를 ​​전달하고 50으로 나눕니다. 시체는 320m와 30m에 몸을 두었습니다. 몸체의 화면에서 벗어나기는하지만 Draw 함수의 화면에서는 그 값을 생각하기 때문에 픽셀 단위입니다.

또한 자신의 사이트에있는 HelloWorld 예제에서 보았을 때 64를 사용하여 50으로 나누지 않아야합니다. 정확히 그 이유는 모르겠지만 64는 2의 요소이기 때문에 생각할 수 있습니다. 컴퓨터는 2 LOL의 요소를 좋아하기 때문에 50을 사용하면 충돌이 일어날 수 있습니다.

+0

귀하의 책임에 대해 감사드립니다. 그러나 질문? 다른 충돌 본체 (FixtureFactory 사용법)가 있음을 의미합니까? 나는 여전히 내 몸을 끌고 마우스 스 와이프로 부스트 할 수 있기 때문에 내 몸이 길을 잃은 경우에는 이것이 가능하지 않을 것이라고 생각합니다. 또한 나는 64f를 사용하여 이전 테스트 부분을 복사/붙여 넣기 만했었다. – Shinji