mreinstein / collision-2d

2d collision routines
MIT License
120 stars 0 forks source link

Cone -> Sphere and segment overlap #14

Open dungeon2567 opened 9 months ago

dungeon2567 commented 9 months ago

Is there any way to implement cone -> sphere and segment overlap system?

mreinstein commented 9 months ago

I'm not sure I understand the ask. Are you saying you want to overlap a cone against a set of spheres and segments, or do you want separate checks like coneSphere(...), coneSegment(...) ?

dungeon2567 commented 9 months ago

I want to overlap a cone against a set of spheres and segments

But both works

dungeon2567 commented 9 months ago

this is for a topdown game, for visibility checks

mreinstein commented 9 months ago

We would need to add coneSphereOverlap and coneSegmentOverlaptests. I don't have any links to existing implementations for these, but I'm open to adding them.

this is for a topdown game, for visibility checks

Cool!

dungeon2567 commented 9 months ago
bool overlapsWith(const Cone& cone) const {
    if (isCircle()) {
        float dist = std::sqrt(Math::sqr(position.x - cone.position.x) + Math::sqr(position.y - cone.position.y));

        if (dist + radius() < cone.minDistance || dist - radius() > cone.maxDistance) {
            return false;
        }

        // Calculate the direction vector from the cone's position to the circle center
        Vector2 toCircle = { position.x - cone.position.x, position.y - cone.position.y };

        // Calculate the angle between the direction vector and the cone's rotation vector
        float dotProduct = toCircle.x * cone.rotation.x + toCircle.y * cone.rotation.y;
        float angle = std::acos(dotProduct / (dist * 1.0));

        return angle <= cone.fieldOfView / 2.0f;
    }

    return false;
}
I have generated this using ChatGPT for cone and circle but for segment i have no idea (and chatgpt can`t do it)
mreinstein commented 9 months ago

and chatgpt can`t do it

Why not?

mreinstein commented 9 months ago

I suspect that chatgpt generated function may not catching all intersection cases, because it only seems to compare the angle of the cone against the vector going through the center point of the sphere.

It would be nice to generate visual test for this, like we have for the other collision functions. That would give me some confidence that this code is actually working.

dungeon2567 commented 9 months ago

image It is not working, only range check is working

dungeon2567 commented 9 months ago

and chatgpt can`t do it

Why not?

I have tried but all codes he generate is broken.

mreinstein commented 9 months ago

I would imagine somewhere on the web are high quality cone-sphere and cone-segment intersection tests. If you find any I'm happy to integrate them into this library. :)

mreinstein commented 9 months ago

PRs also welcome!

dungeon2567 commented 9 months ago

image

Im thing about doing that for cone sphere (reducing it cone-circle). but im not sure how yet.

mreinstein commented 9 months ago

This might be good. It looks like it may be robust: https://github.com/mosra/magnum/blob/53c1549e69af552d28d5da8fc4dcdd0e9a1cad60/src/Magnum/Math/Intersection.h#L529-L556

mreinstein commented 9 months ago

Let me know how this works for you, if the routine seems robust I'm happy to include it in this module.

dungeon2567 commented 9 months ago

Ok, i think it should work. As my game is using a BVH and i still don`t have a code to calculate AABB of frustrum i have not tested it visually (except some values)

mreinstein commented 9 months ago

just out of curiosity what are you using for a renderer?

dungeon2567 commented 9 months ago

Nothing, it is a mmorpg backend. But for the frontend i`m using unreal engine 5

mreinstein commented 9 months ago

that's a really interesting architecture, have yet to encounter that. Are you keeping a devlog anywhere?

dungeon2567 commented 9 months ago

No, i have just started. But i will do it soon.

https://streamable.com/7vo4vd

This is my mmorpg on unity using same architecture but ... unity is dead