Verlet integration 韋爾萊積分 (Part. 1)
Verlet integration:
第一次瞭解到 Verlet integration 像是接觸到了新大陸,原來dynamic sim可以變得如此簡單。
Introduction
When simulating a particle movement, I naturally think of Euler integration, where the position of the particle can be expressed as such: (free falling motion)
x
=
0.5
∗
g
∗
t
2
x = 0.5*g*t^2
x=0.5∗g∗t2
This is an extremly easy use case. The position of the particle can be represented using a function of time. But in real life, a particle can be affected by multiple forces (and even unstabled force caused non-constant acceleration) and Euler integration also has inaccurate estimation when time step is very large.
In my understanding, the use of Verlet is good for complex particle movement. What it essentially does is relate force with position rather than velocity. The process first calculate the current particle position and the position one step back:
v
e
l
o
c
i
t
y
=
X
c
u
r
r
e
n
t
−
X
p
r
e
v
i
o
u
s
;
velocity = Xcurrent-Xprevious;
velocity=Xcurrent−Xprevious;
X
p
r
e
v
i
o
u
s
=
X
c
u
r
r
e
n
t
;
X
c
u
r
r
e
n
t
=
X
c
u
r
r
e
n
t
+
v
e
l
o
c
i
t
y
Xprevious = Xcurrent; Xcurrent = Xcurrent+velocity
Xprevious=Xcurrent;Xcurrent=Xcurrent+velocity
This works magically, here’s some sample code to help understand it.
// without other influence, the following updates the particle position
velocity = posNow - posOld;
posOld = posNow;
posNow += velocity;
Calculate Force
What if we want to add gravity to the particle like the free falling above? Just add the acceleration to the current position, and the change will add-up on the next integration. The following also applies to forces like friction.
posNow += GRAVITY; // not sure if we should multipy Time.deltatime
Constraint
Constraint are usually in the form of constraining particles to a specific distance, like spring. When mulitple constraints acts on single particle, one constraint will affect the other. To solve this, loop the constraint multiple time for each time step.
for (int count = 0; count < ITERATION; count++){
Constriants();
}
// constraint on rope to make fixed distance between segments
void Constriants()
{
for(int index = 0; index < segment-1; index++)
{
float distance = (currentSeg.posNow - nextSeg.posNow).magnitude;
float error = Mathf.Abs(distance - ropeDist);
Vector2 changeDir = Vector2.zero;
if (distance > ropeDist)
changeDir = (currentSeg.posNow - nextSeg.posNow).normalized;
else if (distance < ropeDist)
changeDir = (nextSeg.posNow - currentSeg.posNow).normalized;
Vector2 changeAmount = changeDir * error;
if (index == 0)
nextSeg.posNow += changeAmount;
else{
currentSeg.posNow -= changeAmount * 0.5f;
nextSeg.posNow += changeAmount * 0.5f;
}
}
}
Collision
Penalty based system or projection collision reaction
// example for bounding box collision
Vector2 velocity = posNow - posOld;
if (posNow.x > screenWidth){
posNow.x = screenWidth;
posOld.x = posNow.x + velocity.x;
}
else if (posNow.x < 0){
posNow.x = 0;
posOld.x = posNow.x + velocity.x;
}
if (posNow.y > screenWidth){
posNow.y = screenWidth;
posOld.y = posNow.y + velocity.y;
}
else if (posNow.y < 0){
posNow.y = 0;
posOld.y = posNow.y + velocity.y;
}
Demo
https://sheldonlei.itch.io/rope-simulation
Resources
https://youtu.be/3HjO_RGIjCU
https://www.youtube.com/watch?v=FcnvwtyxLds
https://en.wikipedia.org/wiki/Verlet_integration#Velocity_Verlet
相關文章
- 重修微積分1
- 玻爾茲曼能量分佈律及麥克斯韋速度分佈推導
- 伯克希爾・哈撒韋財報:2023年Q1伯克希爾・哈撒韋淨利潤355億美元 同比大漲540%
- §1. 第一型曲面積分
- 【微積分】不定積分
- 積分
- 積分商城_積分系統_積分業務邏輯與管理_OctShop
- 微積分小感——1.導數與微分
- 特殊分佈律篇6——萊斯分佈
- 1*1卷積卷積
- 半球積分
- 不定積分
- 微積分小感——3.簡單積分
- 德國4-0提前出線,克羅斯2射1傳諾伊爾撲點,德國隊6勝1負積18分!
- 韋爾股份2021年年度董事會經營評述
- 韋萊韜悅:調查顯示2020年中國薪酬預期漲幅為6.5%
- matlab對不定積分和定積分的計算Matlab
- 有限微積分
- 基本積分表
- 英特爾退位,臺積電稱王
- 17 積體電路與摩爾定律
- 庫茲韋爾:超越人腦,AI尚需跨越的三大里程碑AI
- 1002,概統,積分
- 反常積分例題
- 定積分例題
- 積分商品兌換
- 獲取CSDN積分
- $Simpson$積分入門
- 【物理】回首路徑積分
- 二重積分方法
- 【高等數學】不定積分
- Venn韋恩圖
- 羅克韋爾AB PVPlus觸控式螢幕使用者自動登出的方法
- 如何實現羅克韋爾PLC的模擬量採集和遠端上下載?
- Integration of SAP WM with barcode scanners
- 笛卡爾積的應用——商品 SKU 計算
- Python如何從列表中獲取笛卡爾積Python
- 二維陣列笛卡爾積js實現陣列JS