# 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.

## 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 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 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 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 on other sites
Na, I've been meaning to learn OpenGL anyways.

I'm not going to get anywhere in the professional world using SDL, lol.

Thanks for the idea though.

##### 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 Velocitiesif(_accelerate){    _vx += sin(_angle) * 0.1;    _vy += cos(_angle) * 0.1;}    //Add Velocities_x += (int)_vx;_y += (int)_vy;

And the rotation code:
//Rotate Leftif(_rotateLeft){    _angle += 5.0;}    //Rotate Rightif(_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.

##### 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 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.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628728
• Total Posts
2984419

• 25
• 11
• 10
• 16
• 14