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

Posts I've Made

In Topic: Sprite Movings are Buzzing.

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.

In Topic: Sprite Movings are Buzzing.

05 December 2012 - 12:50 PM

Posted Image

In Topic: Sprite Movings are Buzzing.

05 December 2012 - 09:41 AM

Posted Image

In Topic: 2D Tile based water using cellular automata.

20 November 2012 - 12:08 PM

I fixed the issues myself(took me a while) here is the final video of it working.

In Topic: UDP Multiplayer with SDL_net

09 August 2011 - 02:50 PM

HAHAHA!!! i did it! :3 i made an echo server ^_^. I actually took what you said quite literally and simply did this !
Basicaly i just did exactly that "p" is my recv() udp packet and send is well the send packet.


send->address = p->address;
send->channel = -1;
char *dude = (char*)p->data;
send->data = (Uint8*)dude;
send->len = strlen((char *)send->data) + 1;
send->status = NULL;
SDLNet_UDP_Send(sd, -1, send);


i read your reply and then went into the documentation for UDPpacket and found out that the address holds both the destination and the source ip address and port. then i just set it all equal to each other and send it on through!
High-five? lol

PARTNERS