7-hard_constraints

Initially we’ll look at the most common hard constraint—collisions and contact between objects.All the engines we’re building in this book treat hard constraints different from force generators. At the end of the book,we’ll look briefly at alternative approaches that unify them all into one again.

SIMPLE COLLISION RESOLUTION

THE CLOSING VELOCITY

The laws governing the motion of colliding bodies depend on their closing velocity. The closing velocity is the total speed at which the two objects are moving together.This can be simplified to give

\[v_{c} = -(\dot{p}_{a} - \dot{p}_{b}) \cdot \widehat{p_{a} - p_{b}} \qquad \qquad [7.1] \]

where \(v_{c}\) is the closing velocity (a scalar quantity), $p_{a} $ and $p_{b} $ are the positions of objects a and b.Rather than a closing velocity, we have a separating velocity. The closing velocity is the velocity of one object relative to another, in the direction between the two objects.In this case two objects that are closing in on each other will have a negative relative velocity, and objects that are separating will have a positive velocity. Mathematically this is simply a matter of changing the sign of equation 7.1 to give

\[v_{s} = (\dot{p}_{a} - \dot{p}_{b}) \cdot \widehat{p_{a} - p_{b}} \qquad \qquad [7.2] \]

where \(v_{s}\) is the separating velocity.

THE COEFFICIENT OF RESTITUTION

In particular the spring model assumes that momentum is conserved during the collision:

\[m_{a}\dot{p_{a}} + m_{b}\dot{p_{b}} = m_{a}\dot{p_{a}'} + m_{b}\dot{p_{b}'} \qquad \qquad [7.3] \]

Equation 7.3 tells us about the total velocity before and after the collision, but it doesn’t tell us about the individual velocities of each object. The individual velocities are linked together using the closing velocity, according to the equation

\[v_{s}' = -cv_{s} \]

where \(v_{s}'\) is the separating velocity after the collision, \(v_{s}\) is the separating velocity before the collision, and c is a constant called the coefficient of restitution .Using the two equations, we can get values for \(\dot{p_{a}}'\) and \(\dot{p_{b}}'\).

THE COLLISION DIRECTION AND THE CONTACT NORMAL

\[\widehat{n} = (\widehat{p_{a} - p_{b}}) \]

With the correct contact normal, equation 7.2 becomes

\[v_{s} = (\dot{p}_{a} - \dot{p}_{b}) \cdot \widehat{n} \qquad \qquad [7.4] \]

impulses

Rather than a force, this is called an impulse: an instantaneous change in velocity.In the same way that we have

\[f = m \ddot{p} \]

for forces,we have

\[g = m \dot{p} \qquad \qquad \qquad [7.5] \]

for impulses, \(g\) Impulses are often written with the letter \(p\); I will use g to avoid confusion with the position of the object p.

There is a major difference, however, between force and impulse. An object has no acceleration unless it is being acted on by a force: we can work out the total acceleration by combining all the forces using D’Alembert’s principle. On the other hand, an object will continue to have a velocity even if no impulses (or forces) are acting on it. The impulse therefore changes the velocity; it is not completely responsible for the velocity. We can combine impulses using D’Alembert’s principle, but the result will be the total change in velocity, not the total velocity.

COLLISION PROCESSING

COLLISION DETECTION

The collision points will normally be found using a collision detector. So far in the physics engine, we’ve assumed we are dealing with particles, which lets us avoid taking geometry into account at all. we’ll implement a range of useful collision detection routines for full 3D objects in chapter 12.
Some collision detection algorithms can take into account the way objects are moving and try to predict likely collisions in the future. Most simply look through the set of objects and check to see whether any two objects are interpenetrating.

RESOLVING INTERPENETRATION

We expect the collision detector to tell us how far the objects have interpenetrated.

class ParticleContact
{
    // ... Other ParticleContact code as before ...
    /**
    * Holds the depth of penetration at the contact.
    */
    real penetration;
};

To resolve the interpenetration we check the interpenetration depth. If it is already zero or less, then we need take no action; otherwise, we canmove the two objects apart just far enough so that the penetration depth becomes zero.

The total motion of each object is equal to the depth of interpenetration:

\[\Delta p_{a} + \Delta p_{b} = d \]

where \(\Delta p_{a}\) is the scalar distance that object awill bemoved (we’ll return to the direction later). The two distances are related to each other according to the ratio of their masses:

\[m_{a}\Delta p_{a} = m_{b}\Delta p_{b} \]

which combined gives us
\(\Delta p_{a} = \frac{m_{b}}{m_{a} + m_{b}}d\) and \(\Delta p_{b} = \frac{m_{a}}{m_{a} + m_{b}}d\) Combining these with the direction from the contact normal, we get a total change in the vector position of

\[\Delta \mathbf{p}_{a} = \frac{m_{b}}{m_{a} + m_{b}}d \mathbf{n} \]

and

\[\Delta \mathbf{p}_{b} = \frac{m_{b}}{m_{a} + m_{b}}d \mathbf{n} \]

But objects resting (a particle resting on a table, for example)may appear to vibrate and may even leap into the air occasionally.

RESTING CONTACTS

THE CONTACT RESOLVER ALGORITHM

We have three bits of code for performing this update

  • The collision resolution (主要是速度的处理)function that applies impulses to objects to simulate their bouncing apart.
  • The interpenetration resolution function that moves objects apart so that they aren’t partially embedded in one another.
  • The resting contact code that sits inside the collision resolution function and keeps an eye out for contacts that might be resting rather than colliding.

The contact resolver we will use follows this algorithm:

  • Calculate the separating velocity of each contact, keeping track of the contact with the lowest (i.e., most negative) value.
  • If the lowest separating velocity is greater than or equal to zero, then we’re done: exit the algorithm.
  • Process the collision response algorithm for the contact with the lowest separating velocity.
  • If we have more iterations, then return to step 1.

posted on 2024-03-13 14:53  Ultraman_X  阅读(2)  评论(0编辑  收藏  举报

导航