Advertisement

About the problems after collision detection

Started by February 25, 2019 03:20 PM
6 comments, last by Gwenpi 5 years, 9 months ago

First of all, I'm using this for the first time. So please forgive me if I don't understand any rules.

Secondly, I am a Chinese with very poor English. Therefore, I used the translation software to translate the full text. Hopefully that makes sense to you.

image.thumb.png.c80e74b8f28e888fc1c50a6ea5e8bdd5.png

How to separate two colliding polygons. (two objects running through each other).

What code have you written to attempt to solve the problem you're having?

Programmer and 3D Artist

Advertisement

Within that time frame, they are processed recursively. Eventually they split at t1 and collide at t2. And t2 - t1 < precision. Then calculate the intersection point according to the linear velocity and the penetration point. Finally, the penetrating point of the object is moved to the position of the contact point.

45 minutes ago, Gwenpi said:

Within that time frame, they are processed recursively. Eventually they split at t1 and collide at t2. And t2 - t1 < precision. Then calculate the intersection point according to the linear velocity and the penetration point. Finally, the penetrating point of the object is moved to the position of the contact point.

I'm asking what 'code' you have written attempting to solve the problem. The solution is pretty straight forward, you never allow movement that would cause any overlapping to begin with by checking for collision on each tick. If movement would cause an overlap you can also offset the amount back to the edge so the object will be right up against the second object. Now if you have code written and you're still getting this problem we can look at that.

Is this for school?

Programmer and 3D Artist

10 hours ago, Rutin said:

I'm asking what 'code' you have written attempting to solve the problem. The solution is pretty straight forward, you never allow movement that would cause any overlapping to begin with by checking for collision on each tick. If movement would cause an overlap you can also offset the amount back to the edge so the object will be right up against the second object. Now if you have code written and you're still getting this problem we can look at that.

Is this for school?

It's just my personal interest. It has nothing to do with school. How can I show you my code?

Copy and paste, screenshot, or...

float backIsDivide(Polygon * poly1, Polygon * poly2, float t1, float t2)
    {
        float result = (t2 + t1) / 2.0;
        poly1->changeLocation(result - t2);//the object back
        poly2->changeLocation(result - t2);
        if (!CollisionDetection::detection(poly1,poly2)
            )
        {
            //forwardIsImpact is similar to backIsDivide
            return forwardIsImpact(poly1, poly2, result, t2);
        }
        else
        {
            if (t2 - t1 <= timePrecision)
            {
                return result;
            }
            return backIsDivide(poly1, poly2, t1, result);
        }
    }

    //After the object has moved...
    if (CollisionDetection::detection(poly1, poly2))
    {
        float collisionTime = duration;
        if (collisionTime > timePrecision)
            collisionTime = backIsDivide(poly1, poly2, 0, duration);

        //The two objects are now in an overlapping state...

        //I use the sum velocity (v+ ω× r) of the overlapping vertices as the slope to 
        //calculate the intersection point between the overlapping vertex and the edge of 
        //another object. The point of intersection is the point of contact. Finally, move 
        //objects so that their edges and vertices come into contact, and change the speed of the object.
    }

This seems to solve the problem of separation, but if two objects next to each other collide with another object at the same speed, because of recursion, the colliding object will collide with the object next to it... ..

image.thumb.png.52a4410786177a1baa37de3e0a0156c3.png

I hope you can give me a suggestion

I made five examples to illustrate what can happen.

If you move your two cubes together and only use Bounding Box Collision with real time positions you'll get this:

col.gif.d2a9d0306b210677a7c7d6b4f0955ae7.gif

If you add in an 'Offset' to correct the overlapping during the real time movement you'll get a bounce back effect:

colwithoffset.gif.bc6b8eefad326d8d9fa67ae08685c4f3.gif

What I normally do is use ghosting, however you'll get gaps because movement isn't possible if the future move will result in collision, so you'll see something like this:

colghosting.gif.89daf83ea6dcd6dc1aa5430f37da9a9a.gif

However, if you offset the difference it will come together:

colghostingwithoffset.gif.84a9aabdd22461b134c2efbf961037f2.gif

Side by side objects also work:

colghostingwithoffsetsidebyside.gif.406f62c76b715684b3a294c760d88976.gif

I essentially do this:

Every object that will be moving in the scene moves with a ghosted position, all objects are moved at the same time (I use a fixed update for this). Then an overall collision check is done on anything that moved, and depending on results the objects will either move, not move, or if a move is possible but an overlap will happen then an offset is done so the object moves up against the other one. For example if moving right with the green cube your offset for that cube when overlapping the blue cube is the difference between blue's x value and green's x value plus the object width. then you add that value to green's x to offset. (assuming top left cords are 0,0 of each object)

Try ghosting and moving everything first then doing your checks and applying the final result based on that.

Also, if for whatever reason your object is able to move enough per tick to by pass another object, you can do a sweep to see if has gone through objects and make your correction prior to finalizing the movement.

 

Programmer and 3D Artist

Advertisement

I seem to understand what you mean. But there are some terms I don't understand very well.

What does "ghosting" mean? Does this mean that the next frame will collide?

Fixed update is not like this:

double previous = getCurrentTime();
    double lag = 0.0f;
    while (true)
    {
        double current = getCurrentTime();
        double elapsed = current - previous;
        previous = current;
        lag += elapsed;
        if (lag >= MS_PER_UPDATE)
        {
            update();
            lag -= MS_PER_UPDATE;
        }
       .......
    }

 

 

This topic is closed to new replies.

Advertisement