Search code examples
c#xnarotation

rotation and origins building a path. XNA


I'm trying to build a path system involving the use of paths with waypoints. At the moment I'm trying to render the path to the screen. I'm getting some success.

But, I have a problem being, between these checkpoints I need to make "lines". The logic is below.

    for (int i = 0; i < paths.Count; i++)
        {
            for (int j = 0; j < paths[i].wayPoints.Count; j++)
            {
                if ((j + 1) < paths[i].wayPoints.Count)
                {
                    Vector2 point1 = paths[i].wayPoints[j];
                    Vector2 point2 = paths[i].wayPoints[j + 1];
                    Vector2 origin = new Vector2();


                    float angle = (float)Math.Atan2(point2.Y - point1.Y, point2.X - point1.X);
                    float length = Vector2.Distance(point1, point2);

                    //batch.Draw(tileTexture, new Rectangle((int)point1.X, (int)point1.Y, cellSize, cellSize), Color.White);

                    batch.Draw(tileTexture, new Vector2((int)point1.X, (int)point1.Y), null, Color.White,
                        angle, origin, new Vector2(length/40, 1),
                        SpriteEffects.None, 0);
                }
           }
      }

The problem I'm having is that I can't get my enemies to follow this path. Dependent on the rotation, they can go on the wrong side of the path. e.g. they go one cll too far to the right when travelling towards the bottom of the screen but when going back up, they're in the right cell, where the path is drawing. I had this working with a map and drawing using rectangles if that's any help.


Solution

  • Are you talking about pixel perfect accuracy? Yeah I've had this problem too, and it's down how rotation works, it rotates everything around the corner of the pixel rather than the middle of a pixel.. so no pixel remains in the same spot after a rotation. I gave up experimenting because it's a very small thing that I can fix when I'm ironing out all the little bugs when I'm done.

    Here's what my game looks like:

    enter image description here

    You can see that they are 1 pixel off, I've just come to live with it as I'm only making a prototype anyway, and as I said, it's a small nitpicky thing to fix and I chose to add more functionality instead!

    Basically, you need to have a dynamic origin based on the rotation, the exact angle ranges to get it looking correct is something you'll just have to experiment with.

    I think, if I'm reading your path drawing code correctly, you can only support none-diagonal lines. It may be worth saying that it might be your line drawing that is causing the error and not your drawing of the objects that follow the path! Here's the code I use to draw lines:

    public void DrawLine(SpriteBatch spriteBatch, Vector2 lineStart, Vector2 lineEnd)
    {
        Vector2 d = lineEnd - lineStart;
    
        float angle = (float)Math.Atan2(d.Y, d.X);
    
        float distance = d.Length() + 1;
    
        if (Math.Abs(angle - MathHelper.Pi) < 0.01 || 
            Math.Abs(angle + MathHelper.PiOver2) < 0.01)
        {
            distance--;
        }
    
        spriteBatch.Draw(ScreenManager.BlankTexture, 
            new Vector2(lineStart.X + 0.5f, lineStart.Y + 0.5f), 
            null, nodes[i].Item2, angle, new Vector2(0f, 0.5f),
            new Vector2(distance, level.GridLineThickness), 
            SpriteEffects.None, 0);
    }
    

    This works very well for me, nodes[i].Item2 is just a colour reference. As far as I can tell this works for all cases... but then again it works for all cases for my work, I'm sure you can tweak it for your needs, if you decide to use it.