简体   繁体   English

3D 刚体球体碰撞响应 (C++)

[英]3D Rigid body sphere collision response (C++)

I am currently programming a 3D physics engine in C++ and have two spheres in my scene.我目前正在 C++ 中编写 3D 物理引擎,并且在我的场景中有两个球体。 I can detect when said spheres are colliding and they have some basic collision as a response but the spheres go inside each other a lot and act minimally upon each other so I'm looking to change this to be a RigidBody collision.我可以检测到所述球体何时发生碰撞并且它们具有一些基本碰撞作为响应,但是球体 go 彼此内部很多并且彼此之间的作用最小,因此我希望将其更改为刚体碰撞。 I've looked online and many tutorials are on 2D collisions and use Elastic collision which is already hard enough to translate into 2D.我在网上看过很多教程都是关于 2D 碰撞的,并且使用弹性碰撞已经很难转化为 2D。 Any advice on where to look for Rigidbody sphere collision would be a huge help and I'll put below the code I am currently using for my collision.任何关于在哪里寻找刚体球体碰撞的建议都会有很大的帮助,我将把我目前用于碰撞的代码放在下面。 Thanks in advance!提前致谢!

for (size_t i = 0; i < pool.size(); i++)
        {
            if (pool.at(i) == this)
            {
                continue;
            }
            DynamicObjects* other = pool.at(i);
            SScollision = PFG::SphereToSphereCollision(pos, other->GetPosition(), rad, other->GetRadious(), contactPoint);
            if (SScollision)
            {
                glm::vec3 relativeVel = this->GetVelocity() - other->GetVelocity();

                float eCof = -(1.0f + 1.0f) * glm::dot(relativeVel, contactPoint);

                float jLin = eCof / (1.0f / this->GetMass() + 1.0f / other->GetMass());

                glm::vec3 collision_impulse_force = jLin * contactPoint / deltaTs;

                //ClearForces();
                AddForce(1.0f / this->GetMass() * collision_impulse_force);

                //pool.at(otherI)->ClearForces();
                other->AddForce(-(1.0f / other->GetMass() * collision_impulse_force));
            }
        }

When dealing with forces (as opposed to pressure, stress, or potentials), you will need to use a mass-spring-damper model.在处理力(与压力、应力或势能相反)时,您需要使用质量弹簧阻尼器 model。 You can look into the discrete particle method for a technique that is used in many fields.您可以研究离散粒子法,了解一种在许多领域中使用的技术。

To learn for yourself, try to understand your state variables and your system.要自学,请尝试了解您的 state 变量和您的系统。 Take the acceleration you are seeing and divide the sphere's velocity (or velocity difference) by that acceleration.取您看到的加速度并将球体的速度(或速度差)除以该加速度。 This will give you an idea of the time scale that the sphere needs to slow down on impact.这将使您了解球体在撞击时需要减速的时间尺度。 Multiply by the velocity and you get a displacement, you can get an idea of how far that sphere will travel before slowing down.乘以速度,你会得到一个位移,你可以知道球体在减速之前会行进多远。

Also, take a look at your time step.另外,看看你的时间步长。 If the velocity multiplied by the time step allows the sphere to travel further than your threshold for detecting contact, then the sphere's will pass through each other.如果速度乘以时间步长使球体移动的距离超过检测接触的阈值,那么球体将相互穿过。 The same is true if your contact force doesn't increase as the sphere's begin to intersect.如果你的接触力在球体开始相交时没有增加,情况也是如此。

If you want to take a deeper look at collision mechanics, look for topics under "dynamic behavior of materials".如果您想更深入地了解碰撞力学,请在“材料的动态行为”下查找主题。 There you will see concepts for wave mechanics and elastic behavior.在那里,您将看到波力学和弹性行为的概念。 Most numerical methods transition to energy potential models rather than mass-spring-damper models, you can check out methods such as molecular dynamics, smoothed particle hydrodynamics, and peri-dynamics.大多数数值方法过渡到能量势模型而不是质量-弹簧-阻尼器模型,您可以查看分子动力学、平滑粒子流体动力学和周边动力学等方法。

The main point is that these numerical methods operate on governing equations that don't allow particles (or your spheres) to pass through each other.要点是这些数值方法在不允许粒子(或您的球体)相互穿过的控制方程上运行。 The spring models, or potentials often have very (very) stiff responses when objects begin to intersect, as compared to the normal contact between objects.与对象之间的正常接触相比,当对象开始相交时,spring 模型或电位通常具有非常(非常)僵硬的响应。

A couple of points to take away:有几点要带走:

  1. Your integrator (ie time step) is likely too coarse or your collision detection is too fine, allowing the spheres to intersect.您的积分器(即时间步长)可能太粗糙或您的碰撞检测太精细,从而使球体相交。

  2. Look up the discrete particle method (sometimes called the discrete element method), it is very popular and has many, many people using, studying, and publishing.查找离散粒子法(有时称为离散元法),它非常流行,有很多人在使用、研究和发表。 You will find code, mass-spring-damper models, parameters, and other methods to help you reach your goal.您将找到代码、质量弹簧阻尼器模型、参数和其他方法来帮助您实现目标。

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

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