Sprite Movings are buzzing

Hi. I’m making Shoot’em up game.

so there are many bullets. (average over 100)

however, bullets are buzzing.
bullet moving is not smooth.

here is pseudo-code of my bullet moving logic.

in addition, framerate is regulated by FPS Regulation Util in SDL_GFX.
Game Screen Surface is made to SDL_SWSURFACE.
and sdl_putenv(“SDL_VIDEODRIVER=directx”); is also applied.
SDL Version is SDL 1.2
My Development environment is MS Visual Studio 2010, Windows 7.

Thank you for reading my question.

double x;
double y;
double Speed;

//speed(angle) addition per frame.
double speed_acc;
double angle_acc;

double Radian = abs(Angle - 360.0) * (PI / 180.0)
x = x + (Speed * Cos (Radian));
y = y + (Speed * Sin(Radian));

Speed = Speed + speed_acc;
Angle = Angle + angle_acc;

however, bullets are buzzing.

bullet moving is not smooth.

here is pseudo-code of my bullet moving logic.

in addition, framerate is regulated by FPS Regulation Util in SDL_GFX.

Game Screen Surface is made to SDL_SWSURFACE.

and sdl_putenv(“SDL_VIDEODRIVER=directx”); is also applied.

SDL Version is SDL 1.2

My Development environment is MS Visual Studio 2010, Windows 7.

Thank you for reading my question.

double x;

double y;

double Speed;

//speed(angle) addition per frame.

double speed_acc;

double angle_acc;

double Radian = abs(Angle - 360.0) * (PI / 180.0)

x = x + (Speed * Cos (Radian));

y = y + (Speed * Sin(Radian));

Speed = Speed + speed_acc;

Angle = Angle + angle_acc;

This question might be better directed to a gamedev or general programming
forum, since it doesn’t look like the problem lies with SDL. For one thing,
in this pseudo-code snippet you use abs() where you mean fabs(), which
would mean you’re working in integers. Also, you could save the calls to
cos() and sin() every frame (which might be accumulating precision errors)
if you store velocity as its x and y components and just work out the angle
on the initial throw of the bullet.

Good luck :)On Tue, Dec 4, 2012 at 1:24 AM, axt32 wrote:

Chris is right - bullets are moving in a straight line, so you need to
calculate the velocity only once.

Since your timestep is probably quite small, your velocity will often be
a fraction of 1 pixel so you need to either calculate the position
update as doubles (kills performance) or switched to scaled integers for
all calculations. This is a common practise, where one multiplies
integers by a fixed power-of-two factor (i.e. factor = 2^8 or a use a
bit-shift-left of 8) for all calculations and the divide by that factor
again before drawing.

i.e. the bullet would maintain these states:
screen_x, screen y
scaled_x, scaled_y
scaled_velocity_x, scaled_velocity_y

where you’d initialize as:
scaled_x = screen_x << 8;
scaled_y = screen_y << 8;
scaled_velocity_x = speed * cos(angle) * (double)(1 << 8)
scaled_velocity_y = speed * sin(angle) * (double)(1 << 8)

on each iteration you’d updated
scaled_x += scaled_velocity_x
scaled_y += scaled_velocity_y
screen_x = scaled_x >> 8
screen_y = scaled_y >> 8

and draw using the newly calculated screen location.

Hope that helps.
–AndreasOn 12/3/2012 10:47 PM, Chris Bush wrote:

On Tue, Dec 4, 2012 at 1:24 AM, axt32 <axt32 at naver.com <mailto:axt32 at naver.com>> wrote:

however, bullets are buzzing.

bullet moving is not smooth.


here is pseudo-code of my bullet moving logic.


in addition, framerate is regulated by FPS Regulation Util in SDL_GFX.

Game Screen Surface is made to SDL_SWSURFACE.

and sdl_putenv("SDL_VIDEODRIVER=directx"); is also applied.

SDL Version is SDL 1.2

My Development environment is MS Visual Studio 2010, Windows 7.


Thank you for reading my question.


double x;

double y;

double Speed;


//speed(angle) addition per frame.

double speed_acc;

double angle_acc;


double Radian = abs(Angle - 360.0) * (PI / 180.0)

x = x + (Speed * Cos (Radian));

y = y + (Speed * Sin(Radian));


Speed = Speed + speed_acc;

Angle = Angle + angle_acc;

This question might be better directed to a gamedev or general
programming forum, since it doesn’t look like the problem lies with
SDL. For one thing, in this pseudo-code snippet you use abs() where
you mean fabs(), which would mean you’re working in integers. Also,
you could save the calls to cos() and sin() every frame (which might
be accumulating precision errors) if you store velocity as its x and y
components and just work out the angle on the initial throw of the bullet.

Good luck :slight_smile:


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

…or just calculate position as single-precision, which maintains more than enough precision at this scope, doesn’t require scaled integers, and doesn’t kill performance.

Mason________________________________
From: Andreas Schiffler
To: SDL Development List
Cc: axt32
Sent: Tuesday, December 4, 2012 7:16 PM
Subject: Re: [SDL] Sprite Movings are buzzing.

Chris is right - bullets are moving in a straight line, so you need to calculate the velocity only once.

Since your timestep is probably quite small, your velocity will
often be a fraction of 1 pixel so you need to either calculate the
position update as doubles (kills performance) or switched to
scaled integers for all calculations. This is a common practise,
where one multiplies integers by a fixed power-of-two factor (i.e.
factor = 2^8 or a use a bit-shift-left of 8) for all calculations
and the divide by that factor again before drawing.

i.e. the bullet would maintain these states:
screen_x, screen y
scaled_x, scaled_y
scaled_velocity_x, scaled_velocity_y

where you’d initialize as:
scaled_x = screen_x << 8;
scaled_y = screen_y << 8;
scaled_velocity_x = speed * cos(angle) * (double)(1 << 8)
scaled_velocity_y = speed * sin(angle) * (double)(1 << 8)

on each iteration you’d updated
?scaled_x += scaled_velocity_x
scaled_y += scaled_velocity_y
screen_x = scaled_x >> 8
screen_y = scaled_y >> 8

and draw using the newly calculated screen location.

Hope that helps.
–Andreas

On 12/3/2012 10:47 PM, Chris Bush wrote:

On Tue, Dec 4, 2012 at 1:24 AM, axt32 wrote:

however, bullets are buzzing.

bullet moving is not smooth.

here is pseudo-code of my bullet moving logic.

in addition, framerate is regulated by FPS Regulation Util in SDL_GFX.
Game Screen Surface is made to SDL_SWSURFACE.
and sdl_putenv(“SDL_VIDEODRIVER=directx”); is also applied.
SDL Version is SDL 1.2
My Development environment is MS Visual Studio 2010, Windows 7.

Thank you for reading my question.

double x;
double y;
double Speed;

//speed(angle) addition per frame.
double speed_acc;
double angle_acc;

double Radian = abs(Angle - 360.0) * (PI / 180.0)
x = x + (Speed * Cos (Radian));
y = y + (Speed * Sin(Radian));

Speed = Speed + speed_acc;
Angle = Angle + angle_acc;
This question might be better directed to a gamedev or general
programming forum, since it doesn’t look like the problem lies
with SDL. For one thing, in this pseudo-code snippet you use
abs() where you mean fabs(), which would mean you’re working
in integers. Also, you could save the calls to cos() and sin()
every frame (which might be accumulating precision errors) if you
store velocity as its x and y components and just work out the
angle on the initial throw of the bullet.

Good luck :slight_smile:


SDL mailing list SDL at lists.libsdl.org http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org