一个简单易用的绳索物理仿真库,基于 Position-Based Dynamics (PBD) 和 Verlet 积分实现,使用 MuJoCo 作为可视化后端。
A simple and easy-to-use rope physics simulation library, implemented with Position-Based Dynamics (PBD) and Verlet integration, using MuJoCo as the visualization backend.
- Python 3.8+
- MuJoCo 物理引擎(>=3.0.0)
- NumPy
- GLFW(用于图形窗口)
# 使用 pip 安装依赖
pip install mujoco numpy glfw
# 克隆项目
git clone <repository-url>
cd rope-sim
# 安装包(可选)
pip install -e .import numpy as np
from rope_sim import RopePhysics, RopeVisualizer
# 创建物理引擎
initial_anchor = np.array([0.0, 0.0, 10.0])
rope = RopePhysics(anchor=initial_anchor, length=10.0, segment_length=0.1)
# 创建可视化器
visualizer = RopeVisualizer(rope, title="Simple Rope")
# 仿真循环
while visualizer.is_running():
anchor = np.array([0.0, 0.0, 10.0])
rope.set_anchor(anchor)
rope.step(0.01)
visualizer.update(anchor)
visualizer.render()
visualizer.shutdown()python main.py控制方式:
A / D控制轨道 Y 轴W / S控制小车 X 轴Q / E控制绳索伸长/缩短R / T控制锚点升降- 鼠标左键拖动 → 旋转视角
- 鼠标右键拖动 → 平移视角
- 滚轮 → 缩放
ESC→ 退出
class RopePhysics:
"""绳索物理引擎类。"""
def __init__(
self,
anchor,
length: float = 10.0,
segment_length: float = 0.1,
gravity: tuple = (0.0, 0.0, -9.81),
iterations: int = 8
):
pass
def set_anchor(self, anchor):
"""设置绳索起点锚点。"""
pass
def step(self, dt: float):
"""物理引擎步进。"""
pass
@property
def end_point(self):
"""获取绳索终点位置。"""
pass
@property
def positions(self):
"""获取所有点的位置。"""
passclass RopeVisualizer:
"""绳索可视化器类。"""
def __init__(self, rope_physics, window_size=(1280, 720), title="Rope Simulation"):
pass
def update(self, anchor):
"""更新可视化状态。"""
pass
def render(self):
"""渲染一帧。"""
pass
def is_running(self):
"""检查是否需要继续运行。"""
pass
def shutdown(self):
"""关闭可视化器。"""
pass
def get_key_state(self, key):
"""获取键盘按键状态。"""
pass项目采用模块化设计,分为以下核心模块:
- physics.py:纯物理计算,无可视化代码
- visualization.py:MuJoCo 渲染和用户交互
- utils.py:工具函数(四元数、XML 生成等)
- examples/:示例代码
使用 Position-Based Dynamics (PBD) 算法:
- Verlet 积分预测位置
- 距离约束迭代求解
- 最小化求解器优化结果
- 添加碰撞检测和响应
- 支持多种绳索材质
- 优化性能(GPU 加速)
- 添加更多示例场景
- 完善文档
MIT License
