Thực hiện lực hấp dẫn trong trò chơi, nhân vật tiếp tục nảy lên xuống một chút khi anh ta chạm đất, khi gỡ lỗi nhân vật về cơ bản là nảy lên và xuống khỏi mặt đất. Không hoàn toàn chắc chắn làm thế nào để giải quyết vấn đề bất kỳ trợ giúp ở đây sẽ được đánh giá rất cao.
Giá trị không đổi
private float gravity = 0.09f;
thêm vào vận tốc nếu nhân vật ở trên mặt đất
if (!isOnGround)
velocity.Y += gravity;
thêm các ký tự vận tốc tổng thể vào vị trí tổng thể.
position += velocity * (float)gameTime.ElapsedGameTime.TotalSeconds;
Một phần của phương pháp va chạm xác định xem nhân vật có ở trên mặt đất hay không.
for (int y = topTile; y <= bottomTile; ++y)
{
for (int x = leftTile; x <= rightTile; ++x)
{
// If this tile is collidable,
TileCollision collision = Level.GetCollision(x, y, tileMap);
if (collision != TileCollision.Passable)
{
// Determine collision depth (with direction) and magnitude.
Rectangle tileBounds = Level.GetBounds(x, y);
Vector2 depth = RectangleExtensions.GetIntersectionDepth(bounds, tileBounds);
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
if (depth != Vector2.Zero)
{
float absDepthX = Math.Abs(depth.X);
float absDepthY = Math.Abs(depth.Y);
// Resolve the collision along the shallow axis.
if (absDepthY <= absDepthX || collision == TileCollision.Platform)
{
// If we crossed the top of a tile, we are on the ground.
if (previousBottom <= tileBounds.Top)
isOnGround = true;
1
Tại sao không sử dụng một động cơ vật lý hiện có? Farseer mất khoảng một ngày để cảm thấy thoải mái và có thể làm bất cứ điều gì bạn cần.
—
ClassicThunder