Search code examples
c#xnamousexna-4.0

click mouse fire direction and speed


I make a 2d video games. When i fired i want the bullet go in the direction of the mouse and it's ok but more far of my hero the mouse is and more fast the bullet go. It's logic but i doesn t want that. Here my programme.

posSouris = new Vector2(mouseState.X, mouseState.Y);
coeffDirecteurOrientationBalleX = (pos.X - posSouris.X);
coeffDirecteurOrientationBalleY = (pos.Y - posSouris.Y);
// pos is the position of my character

so now in my udpates i make that :

public void Update(GameTime gt)
{
     positionBalle.X = positionBalle.X - coeffDirecteurOrientationBalleX;
     positionBalle.Y = positionBalle.Y - coeffDirecteurOrientationBalleY;  
}

The direction is good but not the speed. If some one has an idea. Sorry for my very bad english and thanks you for the time you spend for me


Solution

  • It's been a while since I used XNA, or any video game framework for the matter, but I'll try to be as clear as possible.

    When you want an object to move, the typical process as far as I remember is:

    yourObject.Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds;

    Basically, it is the position of the object (Vector2) + the coordinates of the mouse (direction) + the speed + the elapsed game time (so that it moves more than once, progressively).

    Vector2 already contains a Distance(Vector2, Vector2) method which returns a float representing the distance between the passed values. You can calculate the distance between your object and the Vector2 representing the mouse coordinates at the time of the click by using that method.

    http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.vector2.distance.aspx

    UPDATE:

    You may want to check this link which is a simple 2D XNA game: http://msdn.microsoft.com/en-us/library/bb203893.aspx

    Specially step 5, which explains how to move objects with speed (and how to set the speed accordingly at run time:

    void UpdateSprite(GameTime gameTime)
    {
        // Move the sprite by speed, scaled by elapsed time.
        spritePosition +=
            spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
    
        int MaxX =
            graphics.GraphicsDevice.Viewport.Width - myTexture.Width;
        int MinX = 0;
        int MaxY =
            graphics.GraphicsDevice.Viewport.Height - myTexture.Height;
        int MinY = 0;
    
        // Check for bounce.
        if (spritePosition.X > MaxX)
        {
            spriteSpeed.X *= -1;
            spritePosition.X = MaxX;
        }
    
        else if (spritePosition.X < MinX)
        {
            spriteSpeed.X *= -1;
            spritePosition.X = MinX;
        }
    
        if (spritePosition.Y > MaxY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MaxY;
        }
    
        else if (spritePosition.Y < MinY)
        {
            spriteSpeed.Y *= -1;
            spritePosition.Y = MinY;
        }
    }