Jump to content

  • Log In with Google      Sign In   
  • Create Account


Aaru

Member Since 08 Aug 2011
Offline Last Active Nov 28 2013 03:47 PM
-----

#5007820 Sprite Movings are Buzzing.

Posted by Aaru on 06 December 2012 - 11:33 AM

I wrote a simple program in SDL that shows a way of implementing Projectile Motion without any "buzzing".
Controls : ( R) : resets the balls position, "hold" (Space) : plays with the angle in a cool way
Hope It helps.

#include <SDL.h>
#include <SDL_image.h>
#include <cmath>

#define PI 3.14159265358979323846
#define WWIDTH 640
#define WHEIGHT 480
#define SPRITESIZE 48

bool Running = true;
float DeltaTime = 0.0f;

int   X = WWIDTH/2;
int   Y = WHEIGHT/2;
float  SpeedX = 0.0f;
float  SpeedY = 0.0f;
float  MaxSpeedX = 2.01f;
float  MaxSpeedY = 2.01f;
float  AccelX = 5.0f;
float  AccelY = 5.0f;

int   Angle = 45;

SDL_Surface* SurfDisplay = NULL;
SDL_Surface* SurfBack = IMG_Load("./Background.jpg");
SDL_Surface* SurfProj = NULL;

void LoadData()
{
SDL_Surface* SurfTemp = IMG_Load("./Projectile.png");
SurfProj = SDL_DisplayFormatAlpha(SurfTemp);

SDL_FreeSurface(SurfTemp);
}

bool Init()
{
// Init
if(SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
  return false;
}

// Vidio Init
if((SurfDisplay = SDL_SetVideoMode(WWIDTH, WHEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL)
{
  return false;
}

//Set Window Title
SDL_WM_SetCaption("Projectile Motion","");
// Hide the Mouse Cursor
SDL_ShowCursor(0);
// Key Repeat
SDL_EnableKeyRepeat(1, SDL_DEFAULT_REPEAT_INTERVAL / 3);

//Load Projectile.png
LoadData();
}

bool Draw(SDL_Surface* SurfDest, SDL_Surface* SurfSrc, int X, int Y)
{
if(SurfDest == NULL || SurfSrc == NULL)
{
  return false;
}

SDL_Rect DestR;
DestR.x = X;
DestR.y = Y;
SDL_BlitSurface(SurfSrc, NULL, SurfDest, &DestR);
return true;
}
void OnEvent(SDL_Event* AnEvent)
{
switch(AnEvent->type)
{
  case SDL_QUIT:
   Running = false;
   break;
  case SDL_KEYDOWN:
   switch(AnEvent->key.keysym.sym)
   {
	case SDLK_ESCAPE:
	 Running = false;
	 break;
	case SDLK_r:
	 //Reset Projectile
	 X = WWIDTH/2;
	 Y = WHEIGHT/2;
	 Angle = 45;
	 break;
	case SDLK_SPACE:
	 //Cool Projectile Motion Path
	 Angle += tan(1* acos(1* asin(.1)));
	 break;
   }
   break;
  case SDL_MOUSEBUTTONDOWN:
   break;
}
}

void OnLoop()
{
SpeedX += AccelX * cos(Angle*PI/180) * DeltaTime;
SpeedY += AccelY * sin(-Angle*PI/180) * DeltaTime;

if(SpeedX > MaxSpeedX)  SpeedX =  MaxSpeedX;
if(SpeedX < -MaxSpeedX) SpeedX = -MaxSpeedX;
if(SpeedY > MaxSpeedY)  SpeedY =  MaxSpeedY;
if(SpeedY < -MaxSpeedY) SpeedY = -MaxSpeedY;

X += SpeedX;
Y += SpeedY;

//Keep Projectile In Window
if(Y < 0){SpeedY *= -1; Angle = -Angle-360;}
if(Y > WHEIGHT - SPRITESIZE){SpeedY *= -1; Angle = -Angle+360;}
if(X < 0){SpeedX *= -1; Angle = -Angle+180;}
if(X > WWIDTH - SPRITESIZE){SpeedX *= -1; Angle = -Angle-180;}

//Keep Angle Within The Range of +/- 360
if(Angle >= 360){Angle -= 360;};
if(Angle <= -360){Angle += 360;};

char Buffer[255];
sprintf(Buffer, "Projectile Motion. Current Angle: %d", Angle);
SDL_WM_SetCaption(Buffer, Buffer);
}

void OnDraw(SDL_Surface* screen)
{
//Draw The Background
Draw(screen,SurfBack,0,0);

//Draw The Projectile
Draw(screen,SurfProj,X,Y);

//Flip the Display Buffer
SDL_Flip(SurfDisplay);

}
void Cleanup()
{
SDL_FreeSurface(SurfDisplay);

SurfDisplay = NULL;

SDL_Quit();

exit(0);
}

int main(int argc, char* argv[])
{
SDL_Event Event;
Uint32 LastTime = NULL;

Init();

while(Running)
{
  DeltaTime = ((SDL_GetTicks() - LastTime) / 1000.0f) * 32.0f;
  while(SDL_PollEvent(&Event))
  {
   OnEvent(&Event);
  }
  OnLoop();
  OnDraw(SurfDisplay);
  LastTime = SDL_GetTicks();
  SDL_Delay(1);
}
Cleanup();
return 0;
}


Attached is the .exe to try it out.

Attached Files




#5007476 Sprite Movings are Buzzing.

Posted by Aaru on 05 December 2012 - 12:50 PM

Posted Image


#5007420 Sprite Movings are Buzzing.

Posted by Aaru on 05 December 2012 - 09:41 AM

Posted Image


PARTNERS