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\$
0
\$\begingroup\$

Here is how I draw something (object B) at a specific position of something else (object A).

First, you need to have a method (or something) to get the X and Y position of object A:

public float getPosX()
{
    return pos.X;
}

Then you simply set the position of your object B in Update method as below:

objectB.setPos(objectA.getposX() + offsetX, objectA.getposY() + offsetY);

I would recommend you draw your object A in a specific scale to your screen's width and height (for example, objectA.width = screenWidth / 10). After doing this, if your object A is moving, your object B will always follow object A's position.

Edit: The image below is the result of my code: Moving along with another object

\$\endgroup\$
  • \$\begingroup\$ Thanks for the answer. I'm actually looking to set B's position somewhere on (or within) A's texture, not according to the viewport. \$\endgroup\$ – Lamar May 21 at 21:39
  • \$\begingroup\$ I edited my code to show an image of some moving cases. Are they what you want or you want a different thing? \$\endgroup\$ – Silver May 23 at 16:22

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.