Sign in to follow this  

SOLVED SDL_gfx Rotation

This topic is 4020 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

For an Asteroids clone, I need my player's ship image to have several angles, both for moving and drawing. I figured the best way to do this in SDL would be with SDL_gfx's rotozoomer. With vector-based graphics, like textured quads, rotation is an easy thing to do on the fly. I'm assuming that SDL_gfx's rotozoomer isn't fast enough for real-time rotation. I figured I'd just rotate it several times when loading at the game's startup, and store them in an array. So: 1. Should I do this, or is real-time rotation fast enough with SDL_gfx? 2. If I should pre-rotate, how many degrees do you think I should jump each time? I don't really think I should store 360 images for each degree. [Edited by - Uphoreum on December 14, 2006 7:52:59 PM]

Share this post


Link to post
Share on other sites
SDL_gfx is fast enought to rotate in real time but last time i looked, when the image rotated it turned the transparent background of an image to black. Not sure, You might want to try it out to see if its what you want. Another alternative is to use OpenGL with SDL and use OpenGL's functions to rotate the images for you.

Share this post


Link to post
Share on other sites
Oh, that's true. I forgot about OpenGL with SDL.

Well, I suppose I'll try the rotozoomer, and if it works, I'll just use that. Otherwise, I guess I'll have to learn OGL+SDL.

Also, how can real-time SDL rotation be fast? Whenever I've used 2D rotation that wasn't vector-based, I've been told to store the pre-rotated images.
(Okay, maybe it was like, one API)

Share this post


Link to post
Share on other sites
If you don't want to learn OpenGL but want to use the realtime hardware rotation of OpenGL, you might consider a wrapper like hxRender.

To use it, just set up the OpenGL context in SDL and then replace all of your blits with hxRender's blitting functions. It allows non-power-of-two textures and is fast when working with 32-bit color depth since the alpha-blending features of the hardware are used. This generally speeds up blits by 15% for straight unmasked 32-bit blits (using the alpha-channel for transparency) and rotation is speeded up much more.

Share this post


Link to post
Share on other sites
Well, SDL_gfx isn't distorting or anything, so I'm trying to rotozoom the image.

It's all working fine, except for the corresponding movement.

I have variables for my player:

x,y

angle

vx, vy

Usually, this works (i.e in XNA and BlitzBasic)

vx += sin(angle) * someSpeed;
vy += cos(angle) * someSpeed;

The image rotates fine, but the velocities are all over the place. They don't seem to correspond to any sides of the image, like they should. It's not like, it goes up, then I rotate 180 degrees and it goes down, it's like, I rotate 180 degrees and it starts going left.

It's really strange.

I don't really know what else to say, but if you have any information that might help, that'd be great.

Here's the code that does this:


//Compute Velocities
if(_accelerate)
{
_vx += sin(_angle) * 0.1;
_vy += cos(_angle) * 0.1;
}

//Add Velocities
_x += (int)_vx;
_y += (int)_vy;



And the rotation code:

//Rotate Left
if(_rotateLeft)
{
_angle += 5.0;
}

//Rotate Right
if(_rotateRight)
{
_angle -= 5.0;
}



I wish I could show you the behavior so you would have a better idea, but I don't have anyplace to host the files.

Thanks in advance.

Share this post


Link to post
Share on other sites
For anyone who finds this thread while searching in the future, the problem was I had to manually convert the angle to radians.

I did this with:

_vx = sin(_angle*PI/180);
_vy = cos(_angle*PI/180);

I'm not old enough to take a calculus class, so if anyone could explain to me why this works? lol

Share this post


Link to post
Share on other sites

This topic is 4020 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.

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