• Advertisement
Sign in to follow this  

Sprite rotation problem using XNA

This topic is 2324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

[color="#1C2837"]This is probably an obvious thing that's staring me in the face, but I'm probably too close to it to see it. sad.gif
[color="#1C2837"]
So I'm doing some rotation of tiles and they're not rendering to the correct location:

rotationproblem.png

[color="#1C2837"]

The first tile isn't rotated and renders correctly. The other 3 are rotated to each direction other than north. Here's the code:






public override void Draw(GameTime gameTime)
{
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0);

SpriteBatch sb = ScreenManager.SpriteBatch;

sb.Begin();

_defaultViewport = ScreenManager.GraphicsDevice.Viewport;
ScreenManager.GraphicsDevice.Viewport = _dungeonRenderArea;

int x2 = GetTilesShown("width");
int y2 = GetTilesShown("height");

float rotation;

Rectangle rect;

for (int x = _startX; x < x2; x++)
{
for (int y = _startY; y < y2; y++)
{
//TODO: need to fix rotation
if (_rooms[x, y] != null && _rooms[x, y].Type != RoomType.None)
{
rotation = GetRotation(_rooms[x, y].Rotation);
rect = new Rectangle((int)((x - _startX) * (TileSize * _zoomFactor)), (int)((y - _startY) * (TileSize * _zoomFactor)), (int)(TileSize * _zoomFactor), (int)(TileSize * _zoomFactor));

sb.Draw(_roomTiles[(int)_rooms[x, y].Type],
rect,
null,
Color.White,
rotation,
_rooms[x, y].Rotation != Direction.North ? _vecOrigin : Vector2.Zero,
SpriteEffects.None, 1.0f);

sb.DrawString(_debugFont, rect.ToString(), new Vector2(rect.X, rect.Y), Color.White);

}
}
}


ScreenManager.GraphicsDevice.Viewport = _defaultViewport;

sb.End();

}

private float GetRotation(Direction dir)
{
float ret = 0.0f;

switch (dir)
{
case Direction.North:
{
ret = 0.0f;
break;
}
case Direction.East:
{
ret = MathHelper.PiOver2;
break;
}
case Direction.South:
{
ret = MathHelper.Pi;
break;
}
case Direction.West:
{
ret = MathHelper.PiOver2 * 3;
break;
}
}

return ret;
}


_vecOrigin is set to the middle of the sprite. The text is rendering in the correct locations for the tiles, but I can't figure out why the tiles aren't.

Any ideas?

Share this post


Link to post
Share on other sites
Advertisement

This is probably an obvious thing that's staring me in the face, but I'm probably too close to it to see it. <img src='http://public.gamedev.net/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' /><br /><br />So I'm doing some rotation of tiles and they're not rendering to the correct location:<br /><br />&lt;img height=&quot;50%&quot; width=&quot;50%&quot; src=&quot;http://machxgames.com/files/screenshots/rotationproblem.png&quot; /&gt;<br /><br />The first tile isn't rotated and renders correctly. The other 3 are rotated to each direction other than north. Here's the code:<br /><br /><br />
public override void Draw(GameTime gameTime)<br />
{<br />
ScreenManager.GraphicsDevice.Clear(ClearOptions.Target, Color.Black, 0, 0);<br />
<br />
SpriteBatch sb = ScreenManager.SpriteBatch;<br />
<br />
sb.Begin();<br />
<br />
_defaultViewport = ScreenManager.GraphicsDevice.Viewport;<br />
ScreenManager.GraphicsDevice.Viewport = _dungeonRenderArea;<br />
<br />
int x2 = GetTilesShown(&quot;width&quot;);<br />
int y2 = GetTilesShown(&quot;height&quot;);<br />
<br />
float rotation;<br />
<br />
Rectangle rect;<br />
<br />
for (int x = _startX; x &lt; x2; x++)<br />
{<br />
for (int y = _startY; y &lt; y2; y++)<br />
{<br />
//TODO: need to fix rotation <br />
if (_rooms[x, y] != null &amp;&amp; _rooms[x, y].Type != RoomType.None)<br />
{<br />
rotation = GetRotation(_rooms[x, y].Rotation);<br />
rect = new Rectangle((int)((x - _startX) * (TileSize * _zoomFactor)), (int)((y - _startY) * (TileSize * _zoomFactor)), (int)(TileSize * _zoomFactor), (int)(TileSize * _zoomFactor));<br />
<br />
sb.Draw(_roomTiles[(int)_rooms[x, y].Type],<br />
rect,<br />
null,<br />
Color.White,<br />
rotation,<br />
_rooms[x, y].Rotation != Direction.North ? _vecOrigin : Vector2.Zero,<br />
SpriteEffects.None, 1.0f);<br />
<br />
sb.DrawString(_debugFont, rect.ToString(), new Vector2(rect.X, rect.Y), Color.White);<br />
<br />
}<br />
}<br />
}<br />
<br />
<br />
ScreenManager.GraphicsDevice.Viewport = _defaultViewport;<br />
<br />
sb.End();<br />
<br />
}<br />
<br />
private float GetRotation(Direction dir)<br />
{<br />
float ret = 0.0f;<br />
<br />
switch (dir)<br />
{<br />
case Direction.North:<br />
{<br />
ret = 0.0f;<br />
break;<br />
}<br />
case Direction.East:<br />
{<br />
ret = MathHelper.PiOver2;<br />
break;<br />
}<br />
case Direction.South:<br />
{<br />
ret = MathHelper.Pi;<br />
break;<br />
}<br />
case Direction.West:<br />
{<br />
ret = MathHelper.PiOver2 * 3;<br />
break;<br />
}<br />
}<br />
<br />
return ret;<br />
}<br />
<br /><br />_vecOrigin is set to the middle of the sprite. The text is rendering in the correct locations for the tiles, but I can't figure out why the tiles aren't.<br /><br /><br /><br />Any ideas?<br /><br /><br /><br />


I'm seeing the code all mangled with HTML tags. Might wanna fix that. Without reading into code, is your rotation angle in radians (not degrees)?

BTW, are those the actual Dungeon Quest board game tiles?


Share this post


Link to post
Share on other sites

Without reading into code, is your rotation angle in radians (not degrees)?


The MathHelper class constants return floats, so yes, they're in radians. They're rotated correctly, just not rendered in the correct location.

Share this post


Link to post
Share on other sites
So, insofar as rotation is concerned, it starts at 0 radians facing right (due to the unit circle). Now, as for position... when a tile that is 32x32 with an origin in the middle is drawn to position (0, 0) the top left corner is actually at (-16, -16). So, in order to draw tiles based on their top left hand corner, you need to subtract TIleSize / 2 from both rect.X and rect.Y.

Share this post


Link to post
Share on other sites
Got it answered over on the XNA forums. It's actually that I needed to add the origin to the dest rect on the rotated sprites.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement