Wednesday, November 30, 2022
HomeGame Developmentc++ - 2D physics engine collision points

c++ – 2D physics engine collision points


I am at the moment caught creating my very own easy physics engine. I am getting a problem the place objects wont come to relaxation. As a substitute, they jitter and finally are shot off into the gap, and a few even fully disappear.

I am utilizing AABB’s for slim part detection, and a quad tree for broad part. I additionally use an impulse and place solver for collision decision

This is my physics step:

        void Step(float _dt)
        {
            Containers::BodyQuadTree tree {
                    {{0.f, 0.f}, {800.f / 32.f, 600.f / 32.f}},
                    0
            };

            for (auto const& physique : m_bodies)
            {
                ApplyForceToCM(physique.get(), m_gravity * body->mass);
                IntegrateBody(physique.get(), _dt);

                tree.Insert(physique.get());
            }

            BroadPhaseList broadList;
            tree.GetPairs(broadList);

            NarrowPhaseList narrowList;
            for (auto const& [A, B] : broadList)
            {
                if (auto intersection { Collision::AABBvsAABB(A->collider, B->collider) };
                    intersection)
                {
                    auto const& contact { narrowList.emplace_back(std::make_unique<BodyContact>()) };

                    contact->our bodies[0] = A;
                    contact->our bodies[1] = B;

                    if (intersection->x < intersection->y)
                    {
                        if (A->place.x < B->place.x)
                        {
                            contact->regular = {-1.f, 0.f};
                        }
                        else
                        {
                            contact->regular = {1.f, 0.f};
                        }
                        contact->intersection = intersection->x;
                    }
                    else if (intersection->x > intersection->y)
                    {
                        if (A->place.y < B->place.y)
                        {
                            contact->regular = {0.f, -1.f};
                        }
                        else
                        {
                            contact->regular = {0.f, 1.f};
                        }
                        contact->intersection = intersection->y;
                    }
                    else
                    {
                        contact->regular = intersection->Regular();
                        contact->intersection = intersection->Magnitude();
                    }

                    contact->restitution = std::min(A->restitution, B->restitution);
                }
            }

            for (std::size_t itr {0}; itr < narrowList.dimension() * 2; itr++)
            {
                float max {0};
                std::size_t maxI { narrowList.dimension() - 1 };
                for (std::size_t i {0}; i < narrowList.dimension(); i++)
                {
                    float sepVelocity { Dynamics::SeparatingVelocity(narrowList[i].get()) };
                    if (sepVelocity < max)
                    {
                        max = sepVelocity;
                        maxI = i;
                    }
                }

                SolveImpulse(narrowList[maxI].get());
                SolvePositionSmooth(narrowList[maxI].get());
            }

            for (auto const& physique : m_bodies)
            {
                ClearForces(physique.get());
            }

        }

Is there a sure method used that would assist clear up this challenge? I’ve heard of Swept AABBs, however I wasn’t certain find out how to implement it.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments