B
Brian
[EDIT: Sorry for posting this initially as a new thread but i'm
posting via Google (terribly buggy!) as my account for reading
NewsGroups doesn't support this particular C-Sharp newsgroup]
Just to let you all know, got the issues with the puck straddling the
the hit-line's (jerking).
The error was that i was forgetting to add on the width of the image.
Let me elaborate... the puck is an image (puck.png) drawn and is set
at 35 pixels (height and width).
When the puck bounced at the top, it just read the puck_x which was
fine as that was the beginning of the image (the 0,0 in height and
width, in other words).
But when the puck wanted to bounce at the bottom, puck_y had to react
at the opposite end of the image (the 35,35 of height and width) and
was bouncing too late. It only reacted when it senses that puck_x was
at the bottom of the table, as opposed to puck_y.
So, i added on the dimension of the radius of the image (35) to the
puck_y to enable it actually takes the dimensions of the image into
account when bouncing at the bottom.
Probably could've worded that better but quite tough to explain.
Again, thanks all for the advice and suggestions.
PS - Cheers Jason for your advice regarding the smooth bouncing. Will
be starting into user interaction next week, so your useful advice
will come into it then.
PPS - Here's the entire GameDraw() method's updated code, for those of
you interested:
private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);
// create a rectangle from the image of the puck
puckRect = new Rectangle(puck_x, puck_y, 35, 35);
// create a region out of the form (and exclude the table-top)
formRect = new Rectangle(0, 0, 983, 683);
formRgn = new Region(formRect);
formRgn.Exclude(pth);
// intersect the two regions (table-top and the puck)
intersection = new Region(pth);
intersection.Intersect(puckRect);
// if the puck is visible outside the table-top region, return true!
boundaryHit = formRgn.IsVisible(puckRect);
// if boundary hit, check whether it was hit in a goal
if(boundaryHit == true)
{
// if puck hits either player's goal, increment the opponent's score
if(puck_y <= 76 && (puck_x >= 341 && puck_x <= 454))
{
IncrementPlayer1Score();
}
else if(puck_y + 35 >= 595 && (puck_x >= 333 && puck_x <= 461))
{
IncrementPlayer2Score();
}
// if puck hits the top or bottom of the table, bounce the puck back
in opposite direction!
if(puck_y <= 76 || puck_y + 35 >= 595)
{
y_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
else
{
x_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
}
}
posting via Google (terribly buggy!) as my account for reading
NewsGroups doesn't support this particular C-Sharp newsgroup]
Just to let you all know, got the issues with the puck straddling the
the hit-line's (jerking).
The error was that i was forgetting to add on the width of the image.
Let me elaborate... the puck is an image (puck.png) drawn and is set
at 35 pixels (height and width).
When the puck bounced at the top, it just read the puck_x which was
fine as that was the beginning of the image (the 0,0 in height and
width, in other words).
But when the puck wanted to bounce at the bottom, puck_y had to react
at the opposite end of the image (the 35,35 of height and width) and
was bouncing too late. It only reacted when it senses that puck_x was
at the bottom of the table, as opposed to puck_y.
So, i added on the dimension of the radius of the image (35) to the
puck_y to enable it actually takes the dimensions of the image into
account when bouncing at the bottom.
Probably could've worded that better but quite tough to explain.
Again, thanks all for the advice and suggestions.
PS - Cheers Jason for your advice regarding the smooth bouncing. Will
be starting into user interaction next week, so your useful advice
will come into it then.
PPS - Here's the entire GameDraw() method's updated code, for those of
you interested:
private void gameDraw(Graphics g)
{
// image of puck is loaded and drawn
Image image = Image.FromFile("Puck.png");
g.DrawImage(image, puck_x, puck_y, 35, 35);
// create a rectangle from the image of the puck
puckRect = new Rectangle(puck_x, puck_y, 35, 35);
// create a region out of the form (and exclude the table-top)
formRect = new Rectangle(0, 0, 983, 683);
formRgn = new Region(formRect);
formRgn.Exclude(pth);
// intersect the two regions (table-top and the puck)
intersection = new Region(pth);
intersection.Intersect(puckRect);
// if the puck is visible outside the table-top region, return true!
boundaryHit = formRgn.IsVisible(puckRect);
// if boundary hit, check whether it was hit in a goal
if(boundaryHit == true)
{
// if puck hits either player's goal, increment the opponent's score
if(puck_y <= 76 && (puck_x >= 341 && puck_x <= 454))
{
IncrementPlayer1Score();
}
else if(puck_y + 35 >= 595 && (puck_x >= 333 && puck_x <= 461))
{
IncrementPlayer2Score();
}
// if puck hits the top or bottom of the table, bounce the puck back
in opposite direction!
if(puck_y <= 76 || puck_y + 35 >= 595)
{
y_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
else
{
x_vel*=-1;
Sound.PlaySoundContinue(@"bounce.wav");
}
}
}