Rotated image moving with SDL

I am making an asteroids kind of game with SDL. For some reason whenever I rotate a ship it moves in a weird pattern. You can see what it looks like in this quick video I made https://www.youtube.com/watch?v=X0nSGvQEZfo . I've tried everything I can think of to fix it but can't fix it. here is my code for drawing ships:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
int i;
	for (i = 0; i < 2; i++) {
		SDL_Rect dest;
		dest.x = players[i].x;
		dest.y = players[i].y;
		
		SDL_Rect size;
		size.x = 16 * players[i].moving;
		size.y = 0;
		size.w = 16;
		size.h = 16;
		
		SDL_Rect place;
		place.x = 4;
		place.y = 4;
		
		SDL_Surface *tmp1 = SDL_CreateRGBSurface(0, 16, 16, 16, 0, 0, 0, 0);
		SDL_BlitSurface(ship, &size, tmp1, NULL);
		
		SDL_Surface *tmp2 = rotozoomSurface(tmp1, 360 - players[i].dir, 1.0, 0);
		SDL_BlitSurface(tmp2, NULL, screen, &dest);
		SDL_FreeSurface(tmp1);
		SDL_FreeSurface(tmp2);

	}
Last edited on
Try setting the dest values to the middle of the sprite not the x/y positions.

1
2
    dest.x = players[i].x + (tmp1->width  / 2);
    dest.y = players[i].y + (tmp1->height / 2);
I tried that, and I think it makes it worse
Note that the rotated surface will often be larger than the original surface. If you know where you want the center of the ship you can easily calculate where the top left corner should be by simply subtracting half the width/height from the center position.

1
2
dest.x = centerOfShipX - tmp2->w / 2;
dest.y = centerOfShipY - tmp2->h / 2;
Last edited on
Awesome! It works now. Thanks for the help
Topic archived. No new replies allowed.