开发者

Simple ball paddle collision

开发者 https://www.devze.com 2023-02-22 00:56 出处:网络
There is a ball UIImage and Paddle UIImage. the collision is working fine when the ball hits the top and bottom sides, but if it hits the small sides of the paddle the ball passes through itand bounc

There is a ball UIImage and Paddle UIImage.

the collision is working fine when the ball hits the top and bottom sides, but if it hits the small sides of the paddle the ball passes through it and bounces within the paddle and comes out.

if (CGRec开发者_JS百科tIntersectsRect(ball.frame, racquet_green.frame))
{
      ballVelocity.y = -ballVelocity.y;
}   

Please tell me some suggestion or any other collision check methods. This is simple ball paddle problem

Note: this clearly happens when the ball hits the rectangle paddle at 45 degrees.


It probably works that way, because: when ball flies from side, one simulation step before collision it is under the top of the paddle, but id doesn't collide yet, because its X coordinate is too small (or too big).

Then next simulation step happens, and ball collides with paddle, so its y velocity is reversed, but x velocity is still the same, so ball goes further into paddle.

Then next simulation step happens, and ball is still inside paddle, so its y velocity is reversed, but x velocity is unchanged, so it goes further into paddle, but bouncing up and down along the way.

Solution - check for collision against both vertical and horizontal edges of the paddle.

0

精彩评论

暂无评论...
验证码 换一张
取 消