我正在 XNA 中进行乒乓球比赛,但遇到了球拍碰撞的问题。桨的侧面工作正常,但顶部和底部表现得很奇怪。我已经尝试了很多东西,但它仍然不起作用。这是我当前的代码:
protected override void Update(GameTime gameTime)
{
//Intersection
if (bPos.Intersects(p1Pos))
{
//top
//bottom
//middle
if (bPos.X <= p1Pos.X + p1Pos.Width)
bSpd.X = -bSpd.X;
}
if (bPos.Intersects(p2Pos))
{
//top
//bottom
//middle
if (bPos.X + bPos.Width >= p2Pos.X)
bSpd.X = -bSpd.X;
}
bPos.X = bPos.X + (int)bSpd.X;
bPos.Y = bPos.Y + (int)bSpd.Y;
if (bPos.Intersects(p1Pos))
{
//top
//bottom
//middle
if (bPos.X <= p1Pos.X + p1Pos.Width)
bSpd.X = -bSpd.X;
}
if (bPos.Intersects(p2Pos))
{
//top
//bottom
//middle
if (bPos.X + bPos.Width >= p2Pos.X)
bSpd.X = -bSpd.X;
}
base.Update(gameTime);
}