Jcorretjer
Jcorretjer

Reputation: 497

Positioning situation in XNA

I've been trying to fix this problem, but I have no idea. I know before I've asked about origin, but I don't really know how to fix this.

enter image description here

red dot = coordinates x-y

yellow dot = origin used in spritebatch.draw

origin = new vector(img.width/2, img.height/2);

position = new rectangle(400, 200, img.width, img.height);

center_x = position.center.x center_Y= position.center.y

I noticed that even though I placed the origin when using spritebatch.draw, the positioning is still done from the 0,0 origin instead of the Yellow dot. How can I change this? I noticed it when I check both the center for Y and X on the position rectangle and compared it with the actual coordinates. The center was higher than the coordinates its self. I want it so that the center coordinates are the same from the position's X & Y. Example, I assign 200 for X and 200 fro Y in the position rectangle. When I go check the center of that rectangle, I want it to be 200 on both X and Y. Also, I'm using a rectangle for positioning cause I'm also testing out so collision stuff.

An example would be nice, thanks in advance

Upvotes: 0

Views: 258

Answers (3)

Blau
Blau

Reputation: 5762

You are passing to positioning a rectangle, use a Vector2 to positioning instead:

  Vector2 origin = new Vector2(img.Width, img.Height) * 0.5f;
  Vector2 pos = new Vector2(400,200) + origin;
  spriteBatch.Draw(img, pos, null, Color.White, 0, origin, SpriteEffects.None, 0)

This works for sure.

EDIT: if you want to work with rectangles be sure that origin is related to the texture size.

  Rectangle bounds = new (400,200, img.Width, img.Height);
  Vector2 origin = new Vector2(img.Width, img.Height) * 0.5f;
  spriteBatch.Draw(img, bounds, null, Color.White, 0, origin, SpriteEffects.None, 0)

  This works too. 

  The position of the yellow dot will be (400,200)
  if you don't use origin, the red dot position will be (400,200)

Upvotes: 1

pinckerman
pinckerman

Reputation: 4213

If you're trying to draw a sprite setting its Origin in its center you're doing it right. Your yellow dot is at position (400, 200) and the dot in the screenshot is reasonably in that position, considering the screen dimension of 800x480. But if you want that the upperleft corner of your sprite has that position you need to change your Draw call.

When specifying the Origin, try using Vector2 position instead of Rectangle for the second parameter of Draw to set the positin of your sprite.

Specify Vector2 Position and Origin:

origin = newVector2(img.Width, img.Heigth) / 2;
pos = new Vector2(400, 200) + origin;
spriteBatch.Draw(img, pos, null, Color.White, 0, origin, SpriteEffects.None, 0);

Or use only the position Rectangle:

posRect = new Rectangle(400, 200, img.Width, img.Heigth);
spriteBatch.Draw(img, posRect, Color.White);

Upvotes: 1

Davor Mlinaric
Davor Mlinaric

Reputation: 2017

i don't see position on our example of spriteBatch. try using it with all properties.

SpriteBatch.Draw (Texture2D, ImgPosition, ImgRect, ImgColor, ImgRotate, ImgOrigin, Scale, SpriteEffects, Layer)

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.spritebatch.draw.aspx

Upvotes: 1

Related Questions