1
\$\begingroup\$

I'm trying to learn Monogames(Old XNA) and how to develop a 2d game.

I'm in the process of animating my sprites. It works but there are somethings that are weird to me.

For some reason when animating my sprites it "changes" the position when being animated WITHOUT changing the origin. Like so

knight

When I have not changed my origin my character "shifts" his body upwards. Something he should not do.

Now if I change the origin to be the width and height of my frame this does not happend. Like so

Knight

Ass you can see the rectangle is still changing but my character does NOT shift in its position. I can't really understand why he shifts and DOES NOT shift.

The origin is at the top-left position of the rectangle.

Why does my character position shifts? I think it's something about offset with the different heights of the frames I am unsure. I don't understand how to work with offset.

EDIT.

//I have sheets with more animations (IDLE,WALK etc) This is how I get correct ones
List<CustomTexture2D.Frames> currentsheet = cts.frames.FindAll(x => x.filename.Contains(_currentstate.ToString()));
//Gets the information of the current frame (width, height, position on spritesheet)
var t = currentsheet.Where(x => x.filename.Contains(_currentframe.ToString())).FirstOrDefault();
//Without changing origin
_spriteBatch.Draw(cts.Texture, position, new Rectangle(new Point(t.frame.x, t.frame.y), new Point(t.frame.w, t.frame.h)), Color.White, rotate, new Vector2(0, 0), 1, SpriteEffects.None, 1);
//With changed origin
_spriteBatch.Draw(cts.Texture, position, new Rectangle(t.frame.x, t.frame.y, t.frame.w, t.frame.h), Color.White, rotate, new Vector2(t.frame.w, t.frame.h), 1, SpriteEffects.None, 1);
\$\endgroup\$
  • \$\begingroup\$ Can you show us the code for each example? \$\endgroup\$ – DMGregory May 17 at 11:42
  • \$\begingroup\$ The code for drawing the sprite or how much of the code do you want? \$\endgroup\$ – tirri May 17 at 11:44
  • \$\begingroup\$ How much is required to reproduce the problem? That's usually a good metric to show enough for others to be able to offer good answers. \$\endgroup\$ – DMGregory May 17 at 11:45
  • \$\begingroup\$ @DMGregory added code. This is how I'm drawing the sprite. \$\endgroup\$ – tirri May 17 at 12:00
  • \$\begingroup\$ I don't have time to answer this now but I answered a very similar problem before. Basically, you need to set the origin point when you call the Draw method \$\endgroup\$ – craftworkgames May 17 at 22:43
1
\$\begingroup\$

If you look carefully at the first image the top left does not change position.

XNA does not know what is drawn in the sprite. So when you tell it to draw a sprite at (x,y) then it will draw that sprite at (x,y).

It's up to you to make sure the visuals actually match up. Either by making sure the character is always in the same spot relative to the top left, or by adding an offset to the (x,y) based on where the character is in the sprite.

\$\endgroup\$
  • \$\begingroup\$ Yeah I know that top left does not change position. But how would I calculate the offset? English is not my main language so unsure what offset means. \$\endgroup\$ – tirri May 17 at 12:41
  • \$\begingroup\$ @tirri, for every sprite you will need to store that offset. Those would need to be determined manually. \$\endgroup\$ – ratchet freak May 17 at 13:04
  • \$\begingroup\$ A common way to handle this offset that ratchet freak describes is to have the artist define a registration point for each frame, also called a "hotpoint" or "pivot", annotated in metadata for the animation, often made with a visual sprite editor tool. By subtracting the respective registration point when drawing each frame, you move them to a common coordinate space where the registration point is fixed at the origin, and the animation moves as needed around this controllable anchor. You can then add your desired sprite coordinates to place this anchored animation wherever you want on-screen. \$\endgroup\$ – DMGregory May 18 at 8:20

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.