0

i am learning how to make a 2d game using sdl and c++ and am currently trying to rotate the sprite so that is always facing the cursor, my game is set up with an entity component system, i have a keyboardcontroller component where the game will use atan2 to work out the angle of the mouse to the sprite and rotate it to match when whenever the mouse is moved. i managed to get it to work but only on the sprites starting coordinates for some reason. all the things to do with drawing the sprite and animating it are stored in the spritecomponent, inside there is a draw() override function that is called for every sprite of every entity for every frame of the game, inside that there is member function of my texturemanager class, when it is called it uses sdl_rendercopyex to draw a sprite onto the renderer in the window. i then try to pass the angle to sdl_rendercopyex so that it changes every time the mouse is moved.

as stated it only seems to work around the starting position of the sprite, after moving away the rotation no longer works, just rotates very slightly but only in the sameish direction. to try and troubleshoot i made it print to the console the player sprite coords, the mouse cursor coords and the angle calculated from this, this all seemed correct, however i then had it print the angle that was passed into the spritecomponent draw() function and for some reason it was a couple hundred degrees more than the angle it should be and does change, remains the same no matter the change in mouse or player position. this is the code i wrote for the rotation angle, anglerot is the angle parameter of the texturemanager::draw() function passed to the sprite:

if(Game::event.type == SDL_MOUSEMOTION){
    Vector2D playerPos = entity->getComponent<TransformComponent>().position;
    int x1;
    int y1;
    int x = playerPos.x;
    int y = playerPos.y;
    SDL_GetMouseState(&x1 , &y1);
    int DeltaX;
    int DeltaY;
    double* result;
    DeltaX = playerPos.x -  x1 ;
    DeltaY = playerPos.y - y1;
    *result = ((atan2(-DeltaX, DeltaY) * 180.00000) / 3.141592);
    sprite->anglerot = static_cast<int> (*result);

    std::cout << "mouse cursor: " << x1 << " , " << y1 << '\n' << "player position: "         << playerPos.x << " " << playerPos.y << '\n' << "angle of rotation: " << result << std::endl;
  • sorry where i said does change in the second paragraph, i meant doesnt change – user21746705 Apr 26 '23 at 17:01
  • You may want to post to [gamedev.se]. – Thomas Matthews Apr 26 '23 at 17:03
  • Your `result` is a pointer, but it's not pointing at any allocated memory, so when you write to it, you get undefined behavior. – Jerry Coffin Apr 26 '23 at 17:19
  • thanks for the suggestion, unfortunately i have to wait 40 minutes tho lol – user21746705 Apr 26 '23 at 17:19
  • @JerryCoffin i was fiddling around with using pointers instead of the actual number but it made no difference, the issue in particular i have doesnt invlove pointers, however when using pointers the pointer printed in the console was the same regardless of position similar to how the number itself didnt change. – user21746705 Apr 26 '23 at 17:24
  • Trying to fix other things when you know you have undefined behavior is mostly a good way to waste a lot of time. – Jerry Coffin Apr 26 '23 at 17:29
  • Works perfectly on my side, though Jerry is right about the ``double`` pointer, use a normal ``double`` instead. From what you described, it seems that the printed angle is correct but it becomes incorrect in your sprite's draw function, so it's quite likely that the problem is from one of the functions that get executed between that mouse motion event and the sprite's draw function. – RedStoneMatt Jul 05 '23 at 09:20

0 Answers0