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?