# Snake Game 2D Sprite Rotation

Hi everyone!
I’m making a snake clone and I’m having trouble rotating the snake’s sprite. As you know it moves 90 degrees each time a movement key is pressed, if it is not in the opposite direction, that is if it goes down it cannot change and go up, if it goes to the right it cannot change to the left , etc … My purpose is to rotate the snake sprite, which I easily get using the “angle” variable of the function “SDL_RenderCopyEx (…” but I also need to rotate an imaginary line that is in, say, the muzzle of the snake that is where I will calculate if it collides with the “prey.” This line will always be above the snout, if the snake goes to the right or left, it will be upright and if it goes up or down in horizontal position, but always just above the head. Well, if I calculate the rotation at a 90 degree angle of that line with the “classic” shape with sines and cosines :
l.x = l.x * cosine (angle) - l.y * sine (angle);
l.y = l.x * sine (angle) + l.y * cosine (angle); where l is the line
I can represent the positive values ​​on the screen, but since they only register positive values ​​on the X and Y axes, how do I represent those that are negative?. I have tried to evaluate only the absolute values ​​but I get the same results. I apologize in advance if it is a very basic or even stupid question but I can’t find a way to do it. Here is the code I am using:

``````struct Line
{
int direction;
float x1, y1, x2, y2, angle;
Line(int dir, float _x1, float _y1, float _x2, float _y2, float ang):
direction(dir), x1(_x1), y1(_y1), x2(_x2), y2(_y2), angle(ang){}
};
{
float pi = 3.14159f;
return degrees * pi / 180.0f;
}

void rotateLine(float degrees, Line& l)
{

auto rotx1 = l.x1 * cosine - l.y1 * sine;
auto roty1 = l.x1 * sine + l.y1 * cosine;
auto rotx2 = l.x2 * cosine - l.y2 * sine;
auto roty2 = l.x2 * sine + l.y2 * cosine;

/*l.x1 = fabs(rotx1);
l.y1 = fabs(roty1);
l.x2 = fabs(rotx2);
l.y2 = fabs(roty2);*/

l.x1 = rotx1;
l.y1 = roty1;
l.x2 = rotx2;
l.y2 = roty2;
}
void Game::handleEvents()
{
if (Input::get().update())
{
if (Input::get().isKeyDown(SDL_SCANCODE_ESCAPE)) mbIsRunning = false;
if (Input::get().isKeyDown(SDL_SCANCODE_UP))
{
if (line.direction == RIGHT || line.direction == LEFT) line.direction = UP;
}
if (Input::get().isKeyDown(SDL_SCANCODE_DOWN))
{
if (line.direction == RIGHT || line.direction == LEFT) line.direction = DOWN;
}
if (Input::get().isKeyDown(SDL_SCANCODE_RIGHT))
{
if (line.direction == UP || line.direction == DOWN) line.direction = RIGHT;
}
if (Input::get().isKeyDown(SDL_SCANCODE_LEFT))
{
if (line.direction == UP || line.direction == DOWN) line.direction = LEFT;
}
}
}

void Game::update()
{
if (line.direction == UP)
{
if (line.angle == 90) rotateLine(-90, line);
else rotateLine(90, line);
line.angle = 0;
}
else if (line.direction == DOWN)
{
if (line.angle == 90) rotateLine(90, line);
else rotateLine(-90, line);
line.angle = 180;
}
else if (line.direction == RIGHT)
{
if (line.angle == 0) rotateLine(90, line);
else rotateLine(-90, line);
line.angle = 90;
}
else if (line.direction == LEFT)
{
if (line.angle == 0) rotateLine(-90, line);
else if (line.angle == 180) rotateLine(90, line);
line.angle = 270;
}
}
``````

The angles of the snake according to the direction are: UP 0, RIGHT 90, DOWN 180, LEFT 270, and I have assumed that the angle changes are positive if the direction of the snake changes according to the clockwise (if the snake is going RIGHT, his angle is 90, and changes to UP, the angle change will be -90) and negative if it is the opposite.

Any help is more than welcome and thaks very much for your time!!.

Can you provide an image to show of whats happening ?

If you’re only moving in 4 directions I don’t see why you need to rotate your collision detection line as you know what it’ll be anyway!

For the right motion use an offset of something like (+10,-5) for the snout offset, left (-10,-5). Up you said was central so (0,-10) and down is (0,+10). 10 is the radius of the snake head, -5 is the snout offset

I’m not sure why you’re making a line for the collision detection rather than just a point, but you could work out the other end of your line for each direction with maybe offsets of right (+100,-5) left (-100,-5) up (0,-100) down (0,+100), where 100 is your line length or whatever you want and -5 is the snout offset.