Small problems with game..

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Hello all,

Hope you all had a enjoyable Christmas and New Year!

Making some good progress on my Air Hockey game, which you've all
heard so much about! :)

Apologies for the vagueness of the following:

Having some little problems not... got my hit-testing working but am
having problems with it at the bottom of the table (typically) for
some reason. The puck seems to react a couple of times at the bottom.
Here's a link to my Table form (along with the Table and puck - put in
Debug folder to work!)

Could ye please have a look at it and suggest why this could be
happening?

PS - Also, i'm about to begin to implement user interaction into it.
Such as the puck reacting when the mouse enters it? How could i go
about implementing this?
 
Brian,

You haven't posted any code, or attached a project. It would be helpful
=)
 
I think that what is happening is that your puck sometimes winds up
below the bottom of the table, so far that it is two velocity-intervals
off, so that it senses it is off, reverses direction, then in the next
cycle sees it is _still_ off, reverses direction again, and - somehow;
this isn't really clear to me - finds its way back onto the table this
time. I think the root cause of this may be the discrepancy between
your "pth" region, with a boundary at 596, and your lower bound limit
of 595. Another source of problem is the diagonals of the pth
trapezoid - with respect to x position, it will be common to see it
"straddle" the boundary because the x coordinate of the boundary
changes as it moves in the y direction.

HTH,
--Carl
 
Hello Brian. The game is coming along nicely since the last time I saw it.

In your game-draw routine, where you check for the ball position, you need
to allow the top-bottom left-right test to adjust the velocity in both
planes X and Y in a single cycle. The ball is getting stuck on the edges and
wobbling in certain circumstances. I think this is because the puck velocity
isn't 1 so the puck can travel past the edge far enough that it gets locked
in a zone where it's always off the edge and bouncing with every go round
the loop.


--
Bob Powell [MVP]
Visual C#, System.Drawing

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Also note that to have a smooth looking bouce action, you need to account
for
the specific position of the puck with respect to the edges when it bounces.
If
the X or Y speeds are never larger than 1.0, this isn't an issue because the
puck
will never move more than one pixel per iteration. But, as in your case,
when
the puck can be moving faster than that, you can get a jerky movement effect
if
you don't take care with how the puck interacts with the edges.

For instance, imagine that the puck is moving at a velocity of 5
pixels/cycle in the
downward direction. Imagine that the puck is one pixel away from the bottom
border. On the next cycle, your hit-testing will determine that the puck
wants to go
4 pixels below the bottom border. Of course, you can't actually draw the
puck
there, so the temptation is to do something like (greatly simplified):

Puck.YPosition += Puck.YVelocity; // move the puck
if(Puck.YPosition <= Table.BottomBorder) // puck has hit the bottom
{
Puck.YPosition = Table.BottomBorder; // don't let the puck be too low
Puck.YVelocity *= -1; // switch directions
}

However, this will result in a non-smooth motion. People are very good at
spotting
small discontinuities in how fast things seem to be moving, and what this
code will
do, in our example, is create one cycle in which the otherwise fast-moving
puck only
moves 1 pixel, after which it changes direction and speeds up again.
Visually, this
looks like the puck gets briefly "stuck" to the edge it bounced off of.

Instead, what you want to do is take account of how much past the border the
puck
would have gone, and give those pixels back, as it were, during the bounce.
In our
example, the puck is only 1 pixel from the border, but should still look to
the user like
it moved a total of 5 pixels in accordance with its speed. That is, in one
cycle of
your animation the puck should go that last pixel down, and then 4 more back
up,
leaving it 3 pixels higher than it started. It only moved 3 pixels on the
screen, and
it's true that the puck will never appear to actually touch the edge, but to
the user
the motion will look correct:

Puck.YPosition += Puck.YVelocity; // move the puck
if(Puck.YPosition <= Table.BottomBorder) // puck has hit the bottom
{
int Excess = Table.BottomBorder - Puck.YPosition; // how far past did we
go?
Puck.YPosition = Table.BottomBorder - Excess; // compensate
Puck.YVelocity *= -1; // switch directions
}
 
Back
Top