Search code examples
cmovesdl-2rect

slow down rectangle in SDL2/C


Hallo I try to have a real gravity in my program so the smaller rectangle should be slow down when I dont press a key. This is the code I tried out, but nothing happened(in this aspect):

 #include <stdio.h>
#include <SDL2/SDL.h>

int main()
{

  //Request successful execute
  if(SDL_Init(SDL_INIT_EVERYTHING)!=0)
  {
    printf("fehler");
    return 1;
  }

  //create window 800x600
  SDL_Window *win;
  win = SDL_CreateWindow("test",100,100,800,600,SDL_WINDOW_SHOWN);

  //create renderwindow with vysnc enabled
  SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);

  if(ren==NULL)
  {
    printf("Fehler");
    SDL_Quit();
    return 1;
  }


  //boolean data type not supported
  int gameRunning = 1;

 SDL_Event event;
 const Uint8 *keystate=SDL_GetKeyboardState(NULL);

//create rectangle which is the whole window
 SDL_Rect rect={
    0,0,800,600
  };
  //smaller rectangle 
  SDL_Rect rect2={
    10,10,50,50
  };

  float v_x = 0;
  float v_y = 0;
  float speedup = 1.f;

//while loop to hold the window opened
  while (gameRunning)
  {
    if (SDL_PollEvent(&event))//close the window 
    {
      if (event.type == SDL_QUIT)
      {
          gameRunning = 0;
      }
    }
    //color of the first rectangle(red)
    SDL_SetRenderDrawColor(ren, 255,0,0,255);
    SDL_RenderClear(ren);
    SDL_RenderFillRect( ren, &rect );

    //second rectangle (blue)
    SDL_SetRenderDrawColor(ren, 0,0,255,255);
    SDL_RenderFillRect( ren, &rect2 );
    //change/update the render
    SDL_RenderPresent(ren);


    if(keystate[SDL_SCANCODE_LSHIFT])
    {
      if (keystate[SDL_SCANCODE_A] ) 
      {
        v_x -= speedup;
      }
      if (keystate[SDL_SCANCODE_D] ) 
      {
        v_x += speedup;
      }
      if (keystate[SDL_SCANCODE_S] ) 
      {
        v_y += speedup;
      }
      if (keystate[SDL_SCANCODE_W] ) 
      {
        v_y -= speedup;
      }
    }
    else if(SDL_KEYDOWN)
    {
      if (keystate[SDL_SCANCODE_A] ) 
      {
        v_x -= 0.1f;
      }

      if (keystate[SDL_SCANCODE_D] ) 
      {
        v_x += 0.1f;
      }

      if (keystate[SDL_SCANCODE_S] ) 
      {
        v_y += 0.1f;
      }

      if (keystate[SDL_SCANCODE_W] ) 
      {
        v_y -= 0.1f;
      }
    }
     else
     {  //make slower(doesnt work)


           v_x= -v_x * 1.f;
           v_y= -v_y * 1.f;
            if(v_x<0.005f || v_y<0.005f)
            {
              v_x=0;
              v_y=0;
            }

     }

    rect2.x += v_x;
    rect2.y += v_y;

    if(rect2.x<0)
      v_x= -v_x * 0.8f;
    else if(rect2.x>=(800-50))
      v_x= -v_x * 0.8f;

    if(rect2.y<0)
      v_y= -v_y * 0.8f;
    else if(rect2.y>=(600-50))
      v_y= -v_y * 0.8f;


  }
  //give heap free
  SDL_DestroyRenderer(ren);
  SDL_DestroyWindow(win);

  SDL_Quit();
    return 0;
}

My idea was to overwrite the gravity v_x and v_y every frame. Whats the problem? When the rect hit a border v_x/v_y becomes slower and this is the same code. PS:I use C and as lib SDL2.

current codepart:

else if(event.type==SDL_KEYDOWN)
{
  if (keystate[SDL_SCANCODE_A] ) 
  {
    v_x -= 0.1f;
  }

  if (keystate[SDL_SCANCODE_D] ) 
  {
    v_x += 0.1f;
  }

  if (keystate[SDL_SCANCODE_S] ) 
  {
    v_y += 0.1f;
  }

  if (keystate[SDL_SCANCODE_W] ) 
  {
    v_y -= 0.1f;
  }
}

 //make slower(doesnt work)

     else
     {
       v_x-=0.0000005;
       v_y-=0.0000005;
        if(v_x<0.000005f || v_y<0.000005f)
        {
          v_x=0;
          v_y=0;
        }
     }

Solution

  • Your first problem was you had:

    else if (SDL_KEYDOWN)
    

    which is always true so the else branch was never reached. This should probably be:

    else if (event.type == SDL_KEYDOWN)
    

    The next problem is your velocity slowing code (note: spaces are free and makes it easier to read):

    v_x -= 0.0000005;
    v_y -= 0.0000005;
    
    if (v_x < 0.000005f || v_y < 0.000005f)
    {
        v_x = 0;
        v_y = 0;
    }
    

    This only works if your initial velocity is positive. If either velocity is negative it immediately sets both to 0. There are a few ways to do it correctly, but the simplest is just explicitly checking:

    const float MIN_SPEED = 0.000005f; // Add to top of function or as a global
    
    ...
    
    if (v_x > MIN_SPEED)
       v_x -= MIN_SPEED;
    else if (v_x < -MIN_SPEED)
       v_x += MIN_SPEED;
    else
       v_x = 0;
    

    and do the same check for v_y.