Sign in to follow this  
Machaira

Sprite rotation problem using XNA

Recommended Posts

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

[img]http://machxgames.com/files/screenshots/rotationproblem.png[/img]

[color="#1C2837"][size="2"][left]The first tile isn't rotated and renders correctly. The other 3 are rotated to each direction other than north. Here's the code:[/left][/size][/color]

[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;
}
[/code]

_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
[quote name='Machaira' timestamp='1318265098' post='4871123']
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 />[code]<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 />
[/code]<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 />
[/quote]

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
[quote name='EJH' timestamp='1318279836' post='4871206']
Without reading into code, is your rotation angle in radians (not degrees)?

[/quote]
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this