C# 碰撞检测弹跳问题
所以我的碰撞检测工作正常,但由于某种原因,玩家似乎进入地面一帧,然后在下一帧中跳出地面,然后按预期跌倒。
我用于碰撞检测的代码
void DoCollisions()
{
onGround = false;
Position.Y += Velocity.Y;
Vector2 tileCollision = GetTileCollision();
if (tileCollision.X != -1 || tileCollision.Y != -1)
{
onGround = true;
Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
new Rectangle(
tileCollision.X * World.tileEngine.TileWidth,
tileCollision.Y * World.tileEngine.TileHeight,
World.tileEngine.TileWidth,
World.tileEngine.TileHeight
)
);
Position.Y -= Velocity.Y;
Velocity.Y = 0;
Position.Y += collisionDepth.Y;
}
Position.X += Velocity.X;
tileCollision = GetTileCollision();
if (tileCollision.X != -1 || tileCollision.Y != -1)
{
Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
new Rectangle(
tileCollision.X * World.tileEngine.TileWidth,
tileCollision.Y * World.tileEngine.TileHeight,
World.tileEngine.TileWidth,
World.tileEngine.TileHeight
)
);
Position.X -= Velocity.X;
Velocity.X = 0;
Position.X += collisionDepth.X;
}
}
Vector2 GetTileCollision()
{
int topLeftTileX = (int)(CollisionRectangle.TopLeft.X / World.tileEngine.TileWidth);
int topLeftTileY = (int)(CollisionRectangle.TopLeft.Y / World.tileEngine.TileHeight);
int BottomRightTileX = (int)(CollisionRectangle.DownRight.X / World.tileEngine.TileWidth);
int BottomRightTileY = (int)(CollisionRectangle.DownRight.Y / World.tileEngine.TileHeight);
for (int i = topLeftTileX; i <= BottomRightTileX; i++)
{
for (int j = topLeftTileY; j <= BottomRightTileY; j++)
{
if (World.tileEngine.TileIsSolid(i, j))
{
return new Vector2(i,j);
}
}
}
return new Vector2(-1,-1);
}
So I got my collision detection working, but for some reason the player seems to go intro the ground 1 frame then jump out of it in the next frame and then fall down as its supposed to.
Code I use for the collision detection
void DoCollisions()
{
onGround = false;
Position.Y += Velocity.Y;
Vector2 tileCollision = GetTileCollision();
if (tileCollision.X != -1 || tileCollision.Y != -1)
{
onGround = true;
Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
new Rectangle(
tileCollision.X * World.tileEngine.TileWidth,
tileCollision.Y * World.tileEngine.TileHeight,
World.tileEngine.TileWidth,
World.tileEngine.TileHeight
)
);
Position.Y -= Velocity.Y;
Velocity.Y = 0;
Position.Y += collisionDepth.Y;
}
Position.X += Velocity.X;
tileCollision = GetTileCollision();
if (tileCollision.X != -1 || tileCollision.Y != -1)
{
Vector2 collisionDepth = CollisionRectangle.DepthIntersection(
new Rectangle(
tileCollision.X * World.tileEngine.TileWidth,
tileCollision.Y * World.tileEngine.TileHeight,
World.tileEngine.TileWidth,
World.tileEngine.TileHeight
)
);
Position.X -= Velocity.X;
Velocity.X = 0;
Position.X += collisionDepth.X;
}
}
Vector2 GetTileCollision()
{
int topLeftTileX = (int)(CollisionRectangle.TopLeft.X / World.tileEngine.TileWidth);
int topLeftTileY = (int)(CollisionRectangle.TopLeft.Y / World.tileEngine.TileHeight);
int BottomRightTileX = (int)(CollisionRectangle.DownRight.X / World.tileEngine.TileWidth);
int BottomRightTileY = (int)(CollisionRectangle.DownRight.Y / World.tileEngine.TileHeight);
for (int i = topLeftTileX; i <= BottomRightTileX; i++)
{
for (int j = topLeftTileY; j <= BottomRightTileY; j++)
{
if (World.tileEngine.TileIsSolid(i, j))
{
return new Vector2(i,j);
}
}
}
return new Vector2(-1,-1);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅以
Y
方向为例,乍一看,您似乎调整了Y
两次,一次是在碰撞检查之前,然后当您发生碰撞时,您将恢复Y
并将对象推出collisionDepth
如果碰撞深度.Y 表明您正在与地面相交,您可能不应该恢复
Y
您应该将物体向后推一定的深度,这将使您与地面齐平。Using just the
Y
direction as an example, at first glance it seems that you are adjustingY
twice, once before the collision check, then when you get a collision you revert theY
and push the object out bycollisionDepth
You should probably not be reverting the
Y
, if the collisionDepth.Y indicates that you are intersecting the ground you should just push your object back by the depth amount, which would put you flush against the ground.