Advertisement
thecplusplusguy

move rect on vector

Feb 24th, 2013
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.41 KB | None | 0 0
  1. //http://youtube.com/user/thecplusplusguy
  2. //move rect on vector
  3. #include <iostream>
  4. #include <SDL/SDL.h>
  5. #include <cmath>
  6.  
  7.  
  8. void calculateVector(float sx,float sy,float ex,float ey,float& vecx,float& vecy)
  9. {
  10.     //a direction vector: end_point-start_point
  11.     vecx=ex-sx;
  12.     vecy=ey-sy;
  13.     //calculate the length with the help of pythagorean theorem
  14.     float length=sqrt(vecx*vecx+vecy*vecy); //might be std::sqrt
  15.     if(length!=0.0)
  16.     {
  17.         //and normalize the vector
  18.         vecx/=length;
  19.         vecy/=length;
  20.     }
  21. }
  22.  
  23.  
  24. int main(int argc,char** argv)
  25. {
  26.     float curX=30,curY=40;  //The current position of the rect. I use float, so we don't loose precision, for example imagine
  27.     //if the speed is 0.5 So I add do curX+=0.5  which is just curX, because it casted to an int, with float, we don't have such a problem.
  28.     int endX=30,endY=40;    //this can stay an int, because this is just the end coordinate
  29.     float vecx=0.0,vecy=0.0;    //The vector, because it will be between 0.0 and 1.0, definitely have to be a float, this is the 2 separate
  30.     //component of a 2D vector, if you want to use vectors often, create a class for them
  31.     float speed=3.0; //3 pixel/s
  32.     calculateVector(curX,curY,endX,endY,vecx,vecy); //calculate the direction vector
  33.     SDL_Surface* screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
  34.     SDL_Event event;
  35.     Uint32 start;
  36.     bool running=true;
  37.     while(running)
  38.     {
  39.         start=SDL_GetTicks();
  40.         while(SDL_PollEvent(&event))
  41.         {
  42.             if(event.type==SDL_QUIT)
  43.                 running=false;
  44.             else if(event.type==SDL_MOUSEBUTTONDOWN)
  45.             {
  46.                 //if we clicked somewhere, calculate the new direction vector with the new end coordinate
  47.                 endX=event.button.x;
  48.                 endY=event.button.y;
  49.                 calculateVector(curX,curY,endX,endY,vecx,vecy);
  50.             }
  51.         }
  52.        
  53.         //if we are close enough, just place make the current coordinate to the end coordinate (so we don't have to add lengthed vectors)
  54.         if(abs(curX-(float)endX)<5 && abs(curY-(float)endY)<5)  //might be std::abs
  55.         {
  56.             curX=endX;
  57.             curY=endY;
  58.         }else{
  59.             //else do the vector operation, and move on that direction
  60.             curX+=speed*vecx;
  61.             curY+=speed*vecy;
  62.         }
  63.         SDL_Rect rec;
  64.         rec.x=curX;
  65.         rec.y=curY;
  66.         rec.w=20;
  67.         rec.h=20;
  68.        
  69.         SDL_FillRect(screen,0,0); //clear screen
  70.        
  71.         SDL_FillRect(screen,&rec,SDL_MapRGB(screen->format,255,0,0));   //draw the rect on the screen
  72.  
  73.         SDL_Flip(screen);
  74.         if(1000.0/30.0>(SDL_GetTicks()-start))
  75.             SDL_Delay(1000.0/30.0-(SDL_GetTicks()-start));
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement