0
\$\begingroup\$

I have a ShipTexture and a Sail Texture. I need to draw the sail texture at a specific location on the shipTexture (call it the sailMountPoint) regardless of the location vector associated with the ship. I.e. the sail should be mounted to the ship and move with it.

I have tried various methods calculating the offset from the vector2 location coordinates of the shipTexture, but am not really coming close. I have also put a color indicator (in the rgba data) in the texture where the sail mount is on the ship texture. During some pre-processing, I then calculated the offset from this position.

Something like the following:

    public class SailMountTextureCoordinates
{
    public static Dictionary<string, Dictionary<int, Tuple<int, int>>> SailMountCords = new Dictionary<string, Dictionary<int, Tuple<int, int>>>();
}

Preprocessing:

public Tuple<int, int> GetSailMountCords(Texture2D texture, int rows, int cols, string key)
        {
            Color[,] Colors = TextureUtility.TextureTo2DArray(texture);

            for (int r = 0; r < rows; r++)
            {
                for (int c = 0; c < cols; c++)
                {
                    // target frame on the texture
                    int width = texture.Width / cols;
                    int height = texture.Height / rows;
                    int X = width * c;
                    int Y = height * r;

                    for (int a = X; a < X + width; a++)
                    {
                        for (int b = Y; b < Y + height; b++)
                        {
                            //If we find a the red sail mount color
                                if (Colors[a, b].R == 255 && Colors[a, b].G == 45 && Colors[a, b].B == 0 && Colors[a, b].A == 255)
                            {
                                if (!SailMountTextureCoordinates.SailMountCords.ContainsKey(key))
                                    SailMountTextureCoordinates.SailMountCords.Add(key, new Dictionary<int, Tuple<int, int>>());
                                SailMountTextureCoordinates.SailMountCords[key][r] = new Tuple<int, int>(a-X, b-Y);
                            }
                        }
                    }
                }
            }
            return null;
        }

Mounting the sail:

        // set the sail location here (equal to ship location plus the offset on the texture to hit the mount)
        float offsetX = (0.5f * (float)(_texture.Width / nColumns));
        float offsetY = (0.5f * (float)(_texture.Height / nRows));
        shipSail.location.X = location.X - offsetX + SailMountTextureCoordinates.SailMountCords[bbKey][currRowFrame].Item1;
        shipSail.location.Y = location.Y - offsetY + SailMountTextureCoordinates.SailMountCords[bbKey][currRowFrame].Item2;

It is worth noting that I am using a large sprite sheet for both textures, then setting the source rectangle for the correct ship and sail frame.

Are there any easier ways to do this in monogame/XNA? So far I've had no luck with this route.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.