我正在制作乒乓球,并且在其他物体内部玩家身体隧道存在问题 使用SetLinearVelocity移动时。
除了隧道之外,我的碰撞工作得很好。玩家在里面或甚至通过隧道 顶墙和底墙。玩家是一个充满活力的身体,墙壁是静止的身体。
这是我的播放器鼠标移动代码:
Vec2 playerVelocity = new Vec2();
float mousePosY = Mouse.GetPosition(window).Y / PixelsToMeter;
float playerPosY = this.Body.GetPosition().Y;
// Player collides with wall
if (this.collisionWithWall)
{
playerVelocity.Y = 0;
}
else
{
if (mousePosY - playerPosY > 0.01f || mousePosY - playerPosY < -0.01f)
{
// I have a fixed timestep of 1 / 60, so
// multiplying by 60 makes the movement smooth.
// This also means that the velocity is very high.
playerVelocity.Y = (mousePosY - playerPosY) * 60;
}
else if (mousePosY - playerPosY == 0)
{
playerVelocity.Y = 0;
}
}
this.playerBody.SetLinearVelocity(playerVelocity);
播放器由3个形状定义组成,如下所示:
// Define new shape definitions
this.rightShape = new PolygonDef() { UserData = "side", IsSensor = true, Density = 1.0f };
// (4px, 124px) fixture at (player.body: 22, 2)
this.rightShape.SetAsBox(2f / PixelsToMeter, (HalfHeight - 2) / PixelsToMeter, new Vec2(10 / PixelsToMeter, 2 / PixelsToMeter), 0);
this.topShape = new PolygonDef() { UserData = "top", IsSensor = true, Density = 1.0f };
// (24px, 4px) fixture at (player.body: 0, 0)
this.topShape.SetAsBox(HalfWidth / PixelsToMeter, 2 / PixelsToMeter, new Vec2(-12 / PixelsToMeter, -HalfHeight / PixelsToMeter), 0);
this.bottomShape = new PolygonDef() { UserData = "bottom", IsSensor = true, Density = 1.0f };
// (24px, 4px) fixture at (player.body: 0, 128)
this.bottomShape.SetAsBox(HalfWidth / PixelsToMeter, 2f / PixelsToMeter, new Vec2(-12 / PixelsToMeter, HalfHeight / PixelsToMeter), 0);
我已经将玩家身体设置为子弹,用于连续碰撞检测。
this.playerBody.SetBullet(true);
墙面形状定义:
// Define a new shape definition
this.wallPoly = new PolygonDef();
this.wallPoly.IsSensor = true;
this.wallPoly.SetAsBox(HalfWidth / PixelsToMeter, HalfHeight / PixelsToMeter);
我确认在移动身体时问题确实是高速度 速度较慢时,碰撞效果很好,但逐渐设定速度后 隧道越高越明显。在我移动鼠标的那一刻 非常快速上升或下降,玩家穿过墙壁到达另一侧, 没有停下来。
如何防止高速隧道掘进?