Advertisement

Bullet Path Using bresenham Line Algorithm For Shooting Game

Started by May 17, 2020 08:39 AM
0 comments, last by tppramod 4 years, 6 months ago

Hi Friends,

Could you please help modify the below line algorithm to plot only one pixel at a time to have a bullet path moving from a x1, y1 to x2, y2. Here x1,y1 is the source and x2, y2 is the target and there values are predefined. No sin / cos calculation here.

Bullet need not travel all the points calculated by the algorithm but jumping few x and y pixels for smooth movement.

void DrawLine(int x1, int y1, int x2, int y2, unsigned char color)
{
int i,dx,dy,sdx,sdy,dxabs,dyabs,x,y,px,py;

dx=x2-x1; /* the horizontal distance of the line */
dy=y2-y1; /* the vertical distance of the line */
dxabs=abs(dx); dyabs=abs(dy);
sdx=sgn(dx); sdy=sgn(dy);
x=dyabs>>1; y=dxabs>>1;
px=x1; py=y1;
if (dxabs>=dyabs) /* the line is more horizontal than vertical */
{
for(i=0;i<dxabs;i++)
{
y+=dyabs;
if (y>=dxabs)
{ y-=dxabs; py+=sdy; }
px+=sdx;
PlotPixel(px,py,color);
}
}
else /* the line is more vertical than horizontal */
{
for(i=0;i<dyabs;i++)
{
x+=dxabs;
if (x>=dyabs)
{ x-=dyabs; px+=sdx; }
py+=sdy;
PlotPixel(px,py,color);
}
}
}

This topic is closed to new replies.

Advertisement