Hello all,
Can someone please give me some advice specifically in the hit-lines cos
i've been at them for for a while now and i still can't figure out a way to
solve the trouble the puck has around the border of the table. It basically
stutters around some areas of the border. But i think i figured out why this
is occuring:
(bear with me..)
If the puck is moving at a speed of 3, it will redraw the puck 3 pixels
(both x and y) further ahead each time. Basic motion!
If the puck is 1 pixel away from the border, it checks and sees it is not
outside the border and moves ahead 3 pixels. It is now outside the region.
It then checks if the puck is visible outside the region.. and it is, it
reverses it's direction. But when it checks again, it is still those 3
pixels outside the region. So that's how i'm getting those stutters in
motion around the border.
I'm trying to fix this by:
- checking the next puck position before i move the puck
- if the next puck position is not outside the table, move the puck as
normal
- if it is due to go outside the table, first check if the next puck
position is higher or lower than the current one.
- If higher, increase the x and y position of the point of the puck each
time until it is visible outside the border. If lower, decrease the x and y
position of the point of the puck each time until it is visible outside the
border.
- Then, take this position of the puck away from the original position and
that should be the distance from the puck to the border.
- increase the puck by this amount to get it touching the border. But it's
not working!
Please look at the game (address in above post) to understand more about
this problem.
Any help would be much appreciated as i'm at my wit's end!!
Brian
PS - Below is the code i've been working at:
private void GetNextPuckPosition()
{
flNextPuckXPos = puck_x + x_vel;
flNextPuckYPos = puck_y + y_vel;
intNextPuckXPos = Convert.ToInt32(flNextPuckXPos);
intNextPuckYPos = Convert.ToInt32(flNextPuckYPos);
nextPuckPos = new Point(intNextPuckXPos, intNextPuckYPos);
nextPuckPosOutsideTable = formRgn.IsVisible(nextPuckPos);
}
private void gameLoop(Object sender, EventArgs e)
{
CheckPlayer1HitPuck();
CheckPlayer2HitPuck();
GetNextPuckPosition();
if(nextPuckPosOutsideTable != true)
{
puck_x += x_vel;
puck_y += y_vel;
}
else
{
if(intPuck_x < intNextPuckXPos)
{
for(int i = intPuck_x; i <= intNextPuckXPos; i++)
{
for(int j = intPuck_y; j <= intNextPuckYPos; j++)
{
Point puckPosPt = new Point(i, j);
if(formRgn.IsVisible(puckPosPt))
{
int distXToBorder = intPuck_x - i;
int distYToBorder = intPuck_y - j;
puck_x+=distXToBorder;
puck_y+=distYToBorder;
break;
}
}
}
}
else
{
foundBorder = false;
for(int i = intPuck_x; i >= intNextPuckXPos; i--)
{
for(int j = intPuck_y; j >= intNextPuckYPos; j--)
{
Point puckPosPt = new Point(i, j);
if(formRgn.IsVisible(puckPosPt))
{
int distXToBorder = intPuck_x - i;
int distYToBorder = intPuck_y - j;
puck_x += distXToBorder;
puck_y += distYToBorder;
foundBorder=true;
break;
}
if(foundBorder==true)
break;
}
if(foundBorder==true)
break;
}
}
}
nextPuckPosOutsideTable = false;
//Called so the form will repaint itself.
Invalidate();
}