PONG stop the ball from shaking? AI player. Suggestions

Started by
5 comments, last by FirstStep 8 years, 11 months ago

Im almost finish on my gameplay on my pong project.

I cant seem to make the AI player move smoothly.

This is my AI movement that is called on Update() always

AI class


            float moveDistance = speed * (float)gameTime.ElapsedGameTime.TotalSeconds;
            float ballDistance = Math.Abs((position.Y + texture.Height / 2) - (ball.position.Y + ball.getTextureSize().Y / 2));    // Explanation about math.abs http://xboxforums.create.msdn.com/forums/p/34583/199261.aspx


            if (moveDistance > ballDistance)
            {
                moveDistance = ballDistance / 2;
            }

            float aY = position.Y + texture.Height/2 - (ball.getTextureSize().Y / 2);   // get the center of paddle
            
            if (ball.position.X > viewportBounds.Width/2)   // when the ball reach the center of the screen
            {
                if (ball.position.Y > aY    && ball.direction.Y == 1)
                {
                    direction = new Vector2(0, 1)*moveDistance;

                }
                else if (ball.position.Y < aY && ball.direction.Y == -1)
                {
                    direction = new Vector2(0, -1)*moveDistance;
                }
                
                // Bounds collision
                bounds = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
                Vector2 tmpPos = position + ((direction * (float)gameTime.ElapsedGameTime.TotalSeconds) * speed); ;
                if (collision.BoundsCollide(ref tmpPos, texture, ref viewportBounds))
                {
                    return;
                }
                position = tmpPos;
            }
            else
            {
                direction.Y = 0; // stop the AI from moving when the ball is on the other half side of screen
            }

Ball class

Initially at load content the speed is set to 300f;

So when the ball hit one the paddle I instantly increase by 100f


if (bounds.Intersects(player.bounds) || bounds.Intersects(Ai.bounds))
            {
                speed = 400f;
                direction.X *= -1;  
            }

Then slowly decrease the speed overtime


 if (speed < 300f)
            {
                speed = 300f;
            }

The problem here is that the paddle of AI paddle is always shaking when the ball is getting close to it.

Any suggestion about how to stop the paddle from shaking?

Advertisement

I think this is your problem.


if (moveDistance > ballDistance)
{
   moveDistance = ballDistance / 2;
}

Splitting the distance in half is quite arbitrary. How about: moveDistance = Math.Abs(ball.direction.Y)
That way you if you are too close, you match the speed of the ball and you never go over/under.

Hope that helps.

Then slowly decrease the speed overtime

 if (speed < 300f)
            {
                speed = 300f;
            }

This section of code increases the speed to 300 if it's less than 300 (instantly).

I think this is your problem.


if (moveDistance > ballDistance)
{
   moveDistance = ballDistance / 2;
}

Splitting the distance in half is quite arbitrary. How about: moveDistance = Math.Abs(ball.direction.Y)
That way you if you are too close, you match the speed of the ball and you never go over/under.

Hope that helps.

Cool. I think it works.. Thanks

Then slowly decrease the speed overtime

 if (speed < 300f)
            {
                speed = 300f;
            }

This section of code increases the speed to 300 if it's less than 300 (instantly).

Im not really sure how it affects the paddle though. Its on the ball class.

Are you saying that I base my AI paddle speed on my ball speed?

If thats so, Combining this with desdemian solution gives me a better of effect biggrin.png

Something you need to look into for problems like this is interpolation. This is the act of "smoothing out" change over time. There are lots of ways to do this but at its heart, it is a mathematically simple beast.

You should read up on it if you are not sure on it yet as it has tons of uses outside of this one problem and is one of the core tricks you will need to learn to handle lots of game development problems.

Good luck!

I see. Thanks for the tips. I found a youtube video explaining about linear interpolation.

This topic is closed to new replies.

Advertisement