Sign in to follow this  
Senor Beer

Movement in fighting game (XNA)

Recommended Posts

Hi, I'm having some trouble moving two of my characters in a multiplayer fighting game for XNA. I'll post the relevant code here:

public void UpdatePlayerOne(PlayerIndex thisPlayer, Vector3 opponentPosition, float distance)
        {
            EngineResources.game.Window.Title = distance.ToString();

            //Very smooth, works apart from strafing
            position.X = (float)(opponentPosition.X + (distance * Math.Cos(MathHelper.ToRadians(strafeAngle))));
            position.Z = (float)(opponentPosition.Z + (distance * Math.Sin(MathHelper.ToRadians(strafeAngle))));


            playerMatrix = Matrix.CreateTranslation(position);
        }


        public void UpdatePlayerTwo(PlayerIndex thisPlayer, Vector3 opponentPosition, float distance)
        {
            position.X -= GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.X;
            position.Z += GamePad.GetState(PlayerIndex.Two).ThumbSticks.Left.Y;

            playerMatrix = Matrix.CreateTranslation(position);
        }


Both of these methods are in the Update section, PlayerOne getting updated first and then player2. The problem being that when I comment out the 'UpdatePlayerTwo' method, everything moves fine (except for player two ofc), but when I include it they both fly off the screen to ridiculous positions. I think the problem is that they both use the same 'distance' variable to move (which is literally the distance between the two of them). So when player two tries to move closer to player one, player one also moves towards player two without me using his controls. This is all circle trigonometry that I dont fully understand. Can anybody shed some light on this problem? If you need me to post anymore code or clear anything up just please ask! Thanks in advance --Senor Beer

Share this post


Link to post
Share on other sites
Sorry for the double post but I've just noticed something.

The code I'm using to move player one is circle trigonometry (i.e. working out his position based on the center of a circle (the opponent), the distance and the angle he moves by), and because of this when I use player two's basic movement, player one essentially mirrors him to stay on the outside of this circle.

Don't know if that helps, but I essentially wanted him to sit tight when player two moves, not be dragged around the map :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this