XNA Creators Club Online
Page 1 of 1 (4 items)
Sort Posts: Previous Next

Top-Down Shooter Help With Character Movement.

Last post 06/11/2009 14:16 by Storm Kiernan. 3 replies.
  • 06/11/2009 4:54

    Top-Down Shooter Help With Character Movement.

    I'm developing a top-down shooter game, but i stucked with moving my player, i have Character class which implements ICharacter interface and inherits DrawableGameComponent, it has Position property (Vector2) now, when i move my mouse my character turns my mouse. but when i pressed W (Forward) i decrease my Position.Y, and normally my character is Going Upward, but i want it to move to my mouse (in this case Right).

    My TurnTo(Vector2) Method:
            public void TurnTo(Vector2 turnTo) 
            { 
                Vector2 m_Difference = turnTo - m_CurrentPosition; 
                double m_NewRotation = 0; 
                float m_NDirection = 0.5f; 
                if (m_Difference.X != 0 && m_Difference.Y != 0) 
                    m_NewRotation = Math.Atan(m_Difference.Y / m_Difference.X); 
                else if (m_Difference.X == 0 && m_Difference.Y > 0) 
                    m_NDirection = 0.0f; 
                if (m_Difference.X < 0) 
                    m_NewRotation -= (Math.PI * m_NDirection); 
                else 
                    m_NewRotation += (Math.PI * m_NDirection); 
                if (m_NewRotation < 0) 
                    m_NewRotation += (Math.PI * 2); 
                m_Rotation = (float)m_NewRotation; 
            } 

    Please help.
    Regards.
    Orhan "Sehlor" KALAYCI
  • 06/11/2009 5:58 In reply to

    Re: Top-Down Shooter Help With Character Movement.

    First, you want to use Atan2() instead of Atan(), because Atan() has singularity and inversion problems.
    Second, you already have the direction you want: it's called m_Difference. Simply normalize m_Difference, and add that to your position, instead of adding/subtracting Y.
    Btw: In classic Hungarian notation, "m_" means a member variable (field). Using m_ for a local variable is likely to confuse people who come from that tradition. I'm just saying!
    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 06/11/2009 6:37 In reply to

    Re: Top-Down Shooter Help With Character Movement.

    Thanks for reply, I Changed my code like:
            public void TurnTo(Vector2 turnTo) 
            { 
                m_Direction = turnTo - m_CurrentPosition; 
                double dNewRotation = 0; 
                float fDirection = 0.5f; 
                if (m_Direction.X != 0 && m_Direction.Y != 0) 
                    dNewRotation = Math.Atan2(m_Direction.Y / m_Direction.X, turnTo.Y / turnTo.X); 
                else if (m_Direction.X == 0 && m_Direction.Y > 0) 
                    fDirection = 0.0f; 
                if (m_Direction.X < 0) 
                    dNewRotation -= (Math.PI * fDirection); 
                else 
                    dNewRotation += (Math.PI * fDirection); 
                if (dNewRotation < 0) 
                    dNewRotation += (Math.PI * 2); 
                m_Rotation = (float)dNewRotation; 
            } 

    And

    m_CurrentPosition -= Vector2.Normalize(m_Direction); 

    Thanks for reply, It solved my problem :)

    But somehow, i think i'm doing something wrong with Right and Left actions, i'm tryin:

                if (m_CurrentKeyboardState.IsKeyDown(Keys.D)) 
                { 
                    m_CurrentPosition.X += Vector2.Normalize(m_Direction).Y; 
                    m_CurrentPosition.Y -= Vector2.Normalize(m_Direction).X; 
                    m_CurrentAnimationIndex = 2; 
                    m_CurrentState = CharacterState.Walking; 
                } 
                if (m_CurrentKeyboardState.IsKeyDown(Keys.A)) 
                { 
                    m_CurrentPosition.X -= Vector2.Normalize(m_Direction).Y; 
                    m_CurrentPosition.Y += Vector2.Normalize(m_Direction).X; 
                    m_CurrentAnimationIndex = 2; 
                    m_CurrentState = CharacterState.Walking; 
                } 

    Am i doing something wrong ?

    D is Right, and A is Left Key of my game.
  • 06/11/2009 14:16 In reply to

    Re: Top-Down Shooter Help With Character Movement.

    Orhan KALAYCI:
    Thanks for reply, I Changed my code like:
            public void TurnTo(Vector2 turnTo) 
            { 
                m_Direction = turnTo - m_CurrentPosition; 
                double dNewRotation = 0; 
                float fDirection = 0.5f; 
                if (m_Direction.X != 0 && m_Direction.Y != 0) 
                    dNewRotation = Math.Atan2(m_Direction.Y / m_Direction.X, turnTo.Y / turnTo.X); 
                else if (m_Direction.X == 0 && m_Direction.Y > 0) 
                    fDirection = 0.0f; 
                if (m_Direction.X < 0) 
                    dNewRotation -= (Math.PI * fDirection); 
                else 
                    dNewRotation += (Math.PI * fDirection); 
                if (dNewRotation < 0) 
                    dNewRotation += (Math.PI * 2); 
                m_Rotation = (float)dNewRotation; 
            } 

    And

    m_CurrentPosition -= Vector2.Normalize(m_Direction); 

    Thanks for reply, It solved my problem :)

    But somehow, i think i'm doing something wrong with Right and Left actions, i'm tryin:

                if (m_CurrentKeyboardState.IsKeyDown(Keys.D)) 
                { 
                    m_CurrentPosition.X += Vector2.Normalize(m_Direction).Y; 
                    m_CurrentPosition.Y -= Vector2.Normalize(m_Direction).X; 
                    m_CurrentAnimationIndex = 2; 
                    m_CurrentState = CharacterState.Walking; 
                } 
                if (m_CurrentKeyboardState.IsKeyDown(Keys.A)) 
                { 
                    m_CurrentPosition.X -= Vector2.Normalize(m_Direction).Y; 
                    m_CurrentPosition.Y += Vector2.Normalize(m_Direction).X; 
                    m_CurrentAnimationIndex = 2; 
                    m_CurrentState = CharacterState.Walking; 
                } 

    Am i doing something wrong ?

    D is Right, and A is Left Key of my game.

    So reverse the directions. Moving right is positive, left is negative. Moving down is positive, up is negative.
Page 1 of 1 (4 items) Previous Next