简体   繁体   中英

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. 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.

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 . Otherwise 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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