f8b21af4be
10_Wiki/Topics 대규모 정리: - 오류 캡처/미완성 stub 문서 227개 제거 - 교차폴더 중복 43클러스터 병합 (63파일 → redirect) - 링크명 정규화: 깨진 링크 수정·redirect 직결·개념 매핑 ~2,400건 - 카테고리 MOC 6개 신규 생성 - Graph 섹션 미해결 related-keyword 링크 10,058건 제거 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
194 lines
6.8 KiB
Markdown
194 lines
6.8 KiB
Markdown
---
|
|
id: wiki-2026-0508-physics
|
|
title: Physics
|
|
category: 10_Wiki/Topics
|
|
status: verified
|
|
canonical_id: self
|
|
aliases: [Game Physics, Physics Engine, Rigid Body Simulation]
|
|
duplicate_of: none
|
|
source_trust_level: A
|
|
confidence_score: 0.95
|
|
verification_status: applied
|
|
tags: [game-design, physics, simulation, rigid-body, collision]
|
|
raw_sources: []
|
|
last_reinforced: 2026-05-10
|
|
github_commit: pending
|
|
tech_stack:
|
|
language: cpp
|
|
framework: rigid-body-physics
|
|
---
|
|
|
|
# Physics
|
|
|
|
## 매 한 줄
|
|
> **"매 Newtonian dynamics 의 discrete-time integration + collision detection + constraint solving 의 trinity"**. Game physics 매 (1) integrator (Euler, Verlet, RK4), (2) broadphase + narrowphase collision, (3) iterative constraint solver (Sequential Impulses, PGS, XPBD) 의 stack. 2026 매 Jolt (Horizon Forbidden West), Rapier (Rust ecosystem), PhysX 5, Bullet 매 dominant.
|
|
|
|
## 매 핵심
|
|
|
|
### 매 Three Pillars
|
|
- **Integration**: 매 Δt 의 over forces → velocity → position.
|
|
- **Collision detection**: 매 broadphase (BVH, sweep-and-prune) → narrowphase (GJK, SAT, MPR).
|
|
- **Constraint resolution**: 매 contact, joints, friction 의 iterative solve.
|
|
|
|
### 매 Integrators
|
|
- **Explicit Euler**: 매 simple, unstable, energy gain.
|
|
- **Semi-implicit Euler**: 매 game default, stable for most cases.
|
|
- **Verlet**: 매 position-based, energy-stable, cloth-friendly.
|
|
- **RK4**: 매 accurate, expensive — 매 specialized sims.
|
|
|
|
### 매 Constraint solvers
|
|
- **Sequential Impulses (Erin Catto)**: 매 Box2D / Bullet 의 standard.
|
|
- **Projected Gauss-Seidel (PGS)**: 매 PhysX, ODE.
|
|
- **XPBD (Extended Position-Based Dynamics)**: 매 Jolt, modern soft-body.
|
|
|
|
### 매 응용
|
|
1. Action games — Jolt + Havok physics for combat impact.
|
|
2. Driving sims — multi-body vehicle constraints.
|
|
3. Cloth / soft-body — XPBD + Verlet for hair, capes, organic deformation.
|
|
|
|
## 💻 패턴
|
|
|
|
### Semi-implicit Euler
|
|
```cpp
|
|
struct RigidBody {
|
|
Vec3 position, velocity;
|
|
Quat orientation; Vec3 angularVelocity;
|
|
float invMass; Mat3 invInertia;
|
|
Vec3 force, torque;
|
|
};
|
|
|
|
void integrate(RigidBody& b, float dt) {
|
|
b.velocity += (b.force * b.invMass) * dt; // 매 v_{n+1} 의 first
|
|
b.position += b.velocity * dt; // 매 x_{n+1} 의 use new v
|
|
b.angularVelocity += (b.invInertia * b.torque) * dt;
|
|
Quat dq = 0.5f * Quat(0, b.angularVelocity) * b.orientation;
|
|
b.orientation = normalize(b.orientation + dq * dt);
|
|
b.force = b.torque = Vec3::Zero;
|
|
}
|
|
```
|
|
|
|
### AABB broadphase (sweep-and-prune)
|
|
```cpp
|
|
struct AABB { Vec3 min, max; int bodyId; };
|
|
|
|
std::vector<std::pair<int,int>> sap(std::vector<AABB>& boxes, int axis) {
|
|
std::sort(boxes.begin(), boxes.end(),
|
|
[&](auto& a, auto& b){ return a.min[axis] < b.min[axis]; });
|
|
std::vector<std::pair<int,int>> pairs;
|
|
for (size_t i = 0; i < boxes.size(); i++) {
|
|
for (size_t j = i + 1; j < boxes.size(); j++) {
|
|
if (boxes[j].min[axis] > boxes[i].max[axis]) break;
|
|
if (overlap(boxes[i], boxes[j])) pairs.emplace_back(boxes[i].bodyId, boxes[j].bodyId);
|
|
}
|
|
}
|
|
return pairs;
|
|
}
|
|
```
|
|
|
|
### GJK narrowphase (convex overlap test)
|
|
```cpp
|
|
bool gjk(const ConvexShape& A, const ConvexShape& B) {
|
|
Vec3 d = {1, 0, 0};
|
|
std::vector<Vec3> simplex = { support(A, B, d) };
|
|
d = -simplex[0];
|
|
for (int i = 0; i < 64; i++) {
|
|
Vec3 p = support(A, B, d);
|
|
if (dot(p, d) < 0) return false; // 매 origin 의 not contained
|
|
simplex.push_back(p);
|
|
if (doSimplex(simplex, d)) return true;
|
|
}
|
|
return false;
|
|
}
|
|
```
|
|
|
|
### Sequential impulse contact resolution
|
|
```cpp
|
|
void resolveContact(RigidBody& a, RigidBody& b, const Contact& c, float restitution, float friction) {
|
|
Vec3 ra = c.point - a.position;
|
|
Vec3 rb = c.point - b.position;
|
|
Vec3 relV = (b.velocity + cross(b.angularVelocity, rb))
|
|
- (a.velocity + cross(a.angularVelocity, ra));
|
|
float vn = dot(relV, c.normal);
|
|
if (vn > 0) return; // 매 separating
|
|
|
|
float effMass = a.invMass + b.invMass
|
|
+ dot(c.normal, cross(a.invInertia * cross(ra, c.normal), ra))
|
|
+ dot(c.normal, cross(b.invInertia * cross(rb, c.normal), rb));
|
|
|
|
float j = -(1 + restitution) * vn / effMass;
|
|
Vec3 impulse = j * c.normal;
|
|
a.velocity -= impulse * a.invMass;
|
|
b.velocity += impulse * b.invMass;
|
|
a.angularVelocity -= a.invInertia * cross(ra, impulse);
|
|
b.angularVelocity += b.invInertia * cross(rb, impulse);
|
|
}
|
|
```
|
|
|
|
### XPBD distance constraint
|
|
```cpp
|
|
void solveDistance(RigidBody& a, RigidBody& b, float restLen, float compliance, float dt) {
|
|
Vec3 d = b.position - a.position;
|
|
float len = length(d);
|
|
Vec3 n = d / len;
|
|
float C = len - restLen;
|
|
float wSum = a.invMass + b.invMass;
|
|
float alpha = compliance / (dt * dt);
|
|
float dLambda = -C / (wSum + alpha);
|
|
a.position -= n * (dLambda * a.invMass);
|
|
b.position += n * (dLambda * b.invMass);
|
|
}
|
|
```
|
|
|
|
### Continuous collision (CCD)
|
|
```cpp
|
|
// 매 high-velocity tunneling 의 prevent
|
|
float ccdSphereSphere(Vec3 pa, Vec3 va, float ra, Vec3 pb, Vec3 vb, float rb, float dt) {
|
|
Vec3 dp = pb - pa;
|
|
Vec3 dv = vb - va;
|
|
float r = ra + rb;
|
|
float a = dot(dv, dv);
|
|
float b = 2 * dot(dp, dv);
|
|
float c = dot(dp, dp) - r * r;
|
|
float disc = b*b - 4*a*c;
|
|
if (disc < 0 || a == 0) return -1;
|
|
float t = (-b - std::sqrt(disc)) / (2 * a);
|
|
return (t >= 0 && t <= dt) ? t : -1;
|
|
}
|
|
```
|
|
|
|
## 매 결정 기준
|
|
| 상황 | Approach |
|
|
|---|---|
|
|
| Action / shooter | Jolt + semi-implicit Euler + sequential impulses |
|
|
| Cloth / soft-body | XPBD + Verlet |
|
|
| Driving sim | Multi-body + RK4 (or sub-stepped semi-implicit) |
|
|
| Casual mobile | Box2D / cocos2d-x physics — minimal overhead |
|
|
| Multiplayer rollback | Deterministic fixed-point physics (Photon Quantum) |
|
|
|
|
**기본값**: 매 semi-implicit Euler + sequential impulses + AABB broadphase + GJK narrowphase.
|
|
|
|
## 🔗 Graph
|
|
- 응용: [[Fixed Time Step vs Variable Time Step]] · [[Beat Saber]] · [[가상현실(VR) 자전거 시뮬레이터]]
|
|
- Adjacent: [[BVH]]
|
|
|
|
## 🤖 LLM 활용
|
|
**언제**: Solver boilerplate, integrator selection, debugging stuck constraint diagnosis.
|
|
**언제 X**: Numerical correctness verification (deterministic test 의 require), shipping-grade tuning.
|
|
|
|
## ❌ 안티패턴
|
|
- **Explicit Euler 의 production**: 매 energy drift → instability.
|
|
- **No CCD on bullets**: 매 tunneling 의 inevitable.
|
|
- **Single-axis SAP**: 매 worst-case O(N²) 의 degrade.
|
|
- **Float-based deterministic netcode**: 매 cross-platform desync.
|
|
- **Constraint solver 의 too-few iterations**: 매 stack jitter.
|
|
|
|
## 🧪 검증 / 중복
|
|
- Verified (Erin Catto GDC talks 2006-2024, Jolt physics docs 2024, "Real-Time Collision Detection" Christer Ericson, XPBD paper Macklin et al.).
|
|
- 신뢰도 A+.
|
|
|
|
## 🕓 Changelog
|
|
| 날짜 | 변경 |
|
|
|---|---|
|
|
| 2026-05-08 | Phase 1 |
|
|
| 2026-05-10 | Manual cleanup — integration, collision, constraint trinity + canonical solvers |
|