Jump to content
  • Advertisement
Sign in to follow this  

[HELP] Sprite rotation towards mouse

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

#include <SDL/SDL.h>

const int WINDOW_WIDTH = 480;
const int WINDOW_HEIGHT = 272;
const char* WINDOW_TITLE = "Swarm";

int main(int argc, char **argv)

SDL_Surface * screen = SDL_SetVideoMode( WINDOW_WIDTH, WINDOW_HEIGHT, 0,
SDL_WM_SetCaption( WINDOW_TITLE, 0 );

SDL_Surface * background = SDL_LoadBMP("images/background.bmp");

SDL_Rect source_background;
source_background.x = 0;
source_background.y = 0;
source_background.w = 480;
source_background.h = 272;

SDL_Rect destination_background;
destination_background.x = 0;
destination_background.y = 0;
destination_background.w = 480;
destination_background.h = 272;

SDL_Surface * player = SDL_LoadBMP("images/player.bmp");

SDL_Rect source_player;
source_player.x = 0;
source_player.y = 0;
source_player.w = 6;
source_player.h = 12;

SDL_Rect destination_player;
destination_player.x = 240;
destination_player.y = 136;
destination_player.w = 6;
destination_player.h = 12;

SDL_Surface * enemy = SDL_LoadBMP("images/enemy.bmp");

SDL_Rect source_enemy;
source_enemy.x = 0;
source_enemy.y = 0;
source_enemy.w = 6;
source_enemy.h = 12;

SDL_Rect destination_enemy;
destination_enemy.x = 0;
destination_enemy.y = 0;
destination_enemy.w = 6;
destination_enemy.h = 12;

SDL_Event event;
bool gameRunning = true;

while (gameRunning)
if (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
gameRunning = false;
int MouseX, MouseY;
float angle = atan2(MouseY - destination_player.y, MouseX - destination_player.x);
SDL_GetMouseState (&MouseX,&MouseY);
destination_player.x = MouseX;
destination_player.y = MouseY;

SDL_BlitSurface(background, &source_background, screen, &destination_background);
SDL_BlitSurface(enemy, &source_enemy, screen, &destination_enemy);
SDL_BlitSurface(player, &source_player, screen, &destination_player);




return 0;

So far I have my background,enemy (currently does nothing, just there), and player rendered in the game. The player moves using the coordinates from the SDL_GetMouseState function. I just need it so the player can also rotate towards the mouse. Also maybe slow down the time it takes for the player to get to the mouse. I've heard about SDL_rotozoom but do not know how to use it.

Share this post

Link to post
Share on other sites

const int WINDOW_WIDTH = 480;
const int WINDOW_HEIGHT = 272;

those dimensions bring back memories, this wouldn't happen to be for the psp would it?

float angle = atan2(MouseY - destination_player.y, MouseX - destination_player.x);

is what you need to get the player to face the mouse, but you are setting the player position to the mouse position immediately so Mouse# - destination_player.# will always = 0
if you want it to move towards the mouse (rather than move right on it each frame) then you should change it a bit like this,

direction.x = MouseX - player.x;
direction.y = MouseY - player.y;

float speed = 3; //Arbitrary number, change depending how fast you want your player to move
player += direction * speed;

That would be the gist of it, You might have to go research what Normalize() is to be able to implement it in SDL, your angle should already work, you just need to apply it to the player image you are blitting, I dont know enough of SDL to tell you
If you have any other issues, let us know

Share this post

Link to post
Share on other sites
Actually I'm recreating a PSP homebrew game and thank you very much for your help.

Share this post

Link to post
Share on other sites
Rotating sprites in SDL is not the best idea. Unless you're specifically integrating SDL with openGL, it's going to be slow, as the rotation will be done in softare.

I would suggest using SFML, as it inherently uses OpenGL under the covers, @d rotation is done in hardware, and it's easy. I spent just a little bit of time with SFML and chipmunk physics, and I got a really cool little test app that i just bat a ball around, and it spins all over the place.

Share this post

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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!