简体   繁体   English

检查两个物体是否发生碰撞和减速的最有效方法是什么?

[英]What is most efficient way to check if two objects are colliding and decellerate on impact?

Okay, so I am working on a simple Javascript platformer game.好的,我正在开发一个简单的 Javascript 平台游戏。 I want to check if a moving item has collided with a surface, and then decelerate the object if that is the case.我想检查移动的物体是否与表面发生碰撞,如果是,则使物体减速。

I feel like there must be a more efficient way to approach the problem than the solution I have created, and I also do not have a strong idea of how to tackle the deceleration aspect of the collision.我觉得必须有一种比我创建的解决方案更有效的方法来解决这个问题,而且我也不太清楚如何解决碰撞的减速方面。

Let me know what you think.让我知道你的想法。

This is what I have been trying, which does work successfully, but would be terrible when there are plenty of objects on the screen at any given point.这就是我一直在尝试的方法,它确实成功地工作了,但是当在任何给定点屏幕上有大量对象时会很糟糕。

const fps = 60;
const g = (9.81)*(1/fps);

...

function move(item) {
    item.x += Math.floor(item.vx);

    item.vy += g;
    item.y += Math.round(item.vy);

    for (let i=0; i<blocks.length; i++) {
        adjustBump(item, blocks[i]);
    }
}

function adjustBump (item, surface) {
    // Horizontal bumping
    if (item.x + item.w >= surface.x && item.vx > 0 && ((item.y >= surface.y && item.y + item.h <= surface.y + surface.h) || (item.y <= surface.y && item.y + item.h >= surface.y + surface.h))) {
        item.vx *= -g;
    } else if (item.x <= surface.x + surface.w && item.vx < 0 && ((item.y >= surface.y && item.y + item.h <= surface.y + surface.h) || (item.y <= surface.y && item.y + item.h >= surface.y + surface.h))) {
        item.vx *= -g;
    }
    // Vertical bumping
    if (item.y + item.h >= surface.y && item.vy > 0 && ((item.x >= surface.x && item.x + item.w <= surface.x + surface.w) || (item.x <= surface.x && item.x + item.w >= surface.x + surface.w))) {
        item.vy *= -g;
    } else if (item.y <= surface.y + surface.h && item.vy < 0 && ((item.x >= surface.x && item.x + item.w <= surface.x + surface.w) || (item.x <= surface.x && item.x + item.w >= surface.x + surface.w))) {
        item.vy *= -g;
    }
}

I would be happy to share more of my code if it would be helpful for you.如果对您有帮助,我很乐意分享更多我的代码。

Thanks in advance!提前致谢! Kelvin开尔文

I'm currently working on a C# console platformer game, so I think I can give you a few tips.我目前正在开发一款 C# 控制台平台游戏,所以我想我可以给你一些提示。

First of all, if you want an efficient collision detection algorithm, I don't recommend going for the complicated algorithms that no one understands.首先,如果你想要一个高效的碰撞检测算法,我不建议你去使用那些没人能理解的复杂算法。 Instead, I'd stick to checking all the collidable objects in a loop.相反,我会坚持检查循环中的所有可碰撞对象。

If it becomes a real performance problem for you, you can use a tree structure (sorted by position probably) of collidable objects instead of an array;如果它对您来说成为一个真正的性能问题,您可以使用可碰撞对象的树结构(可能按位置排序)而不是数组; which means you're going to check the closest (thus most probable to collide with) objects first.这意味着您将首先检查最近(因此最有可能与之发生碰撞)的物体。

What I did was a function that checks all the entities for collisions and if there is one return true .我所做的是一个检查所有实体是否发生碰撞的函数,如果有则return true Otherwise return false .否则return false That means you're not going to waste time checking for objects if you know that a collision happens.这意味着如果您知道会发生碰撞,就不会浪费时间检查对象。

That returned boolean basically means whether you collided or not.返回的布尔值基本上表示您是否发生碰撞。

So move the moving item, then check if there are any collisions using the function, and if there are any, adjust the position and stop the movement.所以移动移动中的物品,然后使用函数检查是否有碰撞,如果有则调整位置并停止移动。

I don't know if these techniques are going to work for you though because in my game the coordinates were mostly integers (except when jumping, I did some float rounding tricks there), but I think the overall concept might be similar.我不知道这些技术是否适合你,因为在我的游戏中坐标主要是整数(除了跳跃时,我在那里做了一些浮动舍入技巧),但我认为整体概念可能是相似的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM