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
where is the closing velocity (a scalar quantity), and 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
where is the separating velocity.
THE COEFFICIENT OF RESTITUTION
In particular the spring model assumes that momentum is conserved during the collision:
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
where is the separating velocity after the collision, 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 and .
THE COLLISION DIRECTION AND THE CONTACT NORMAL
With the correct contact normal, equation 7.2 becomes
impulses
Rather than a force, this is called an impulse: an instantaneous change in velocity.In the same way that we have
for forces,we have
for impulses, Impulses are often written with the letter ; 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:
where 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:
which combined gives us
and Combining these with the direction from the contact normal, we get a total change in the vector position of
and
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 阅读(18) 评论(0) 编辑 收藏 举报
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南