Skip to content

Latest commit

 

History

History
126 lines (88 loc) · 4.63 KB

File metadata and controls

126 lines (88 loc) · 4.63 KB

Interactive Dynamics for Physics Simulations

This is a test made for the Constraint Based Simulator. It provides a constraint satisfaction physics simulator with automatic differentiation.

Screenshots

Usage

Setup

cd code
python3 -m venv venv
source venv/bin/activate
python3 -m pip install -r requirements.txt

Running

cd code
python3 main.py

Adding functionality

Generate a new Constraint subclass and use it in a case.

Design

Math for a single particle

This explanation is meant to complement the references, please read that first. This explains how to compute the derivates with respect to time and position.

$C_p(t)$ is a constraint function on a set of particles $p$ so that:

$$ \exists t / C_p(t) = 0 \land \exists \dot{C_p} $$

To obtain the constraint as a function of particles positions (instead of time) we use:

$$ \widetilde{C}_p(x(t)) = C_p(t) $$

We don't have the single particle $x(t)$ function analitically, but we can use an approximation (a Taylor approximation at $t = 0$):

$$ \widetilde{C}_p(\widetilde{x}(t)) \approx C_p(t) $$

$$ \widetilde{x}(t) = x + t * v + \frac{1}{2} * t^2 * a \approx x(t) $$

$$ \widetilde{x}(0) = x_t \land \dot{\widetilde{x}}(0) = v_t \land \ddot{\widetilde{x}}(0) = a_t $$

And that lets us compute the derivatives ($p_i \in p$ and $C^i$ is a constraint):

$$ C = \begin{bmatrix} C^0(0) & \cdots & C^m(0) \end{bmatrix}^T $$

$$ \dot{C} = \begin{bmatrix} \frac{\partial C^0} {\partial t}(0) & \cdots & \frac{\partial C^m} {\partial t}(0) \end{bmatrix}^T $$

$$ J = \begin{bmatrix} \frac{\partial C_ {p_0}^0}{\partial x_1}(0) & \frac{\partial C_ {p_0}^0}{\partial x_2}(0) & \cdots & \frac{\partial C_ {p_n}^0}{\partial x_1}(0) & \frac{\partial C_ {p_n}^0}{\partial x_2}(0) \\ \vdots & & \vdots & & \vdots \\ \frac{\partial C_ {p_0}^m}{\partial x_1}(0) & \frac{\partial C_ {p_0}^m}{\partial x_2}(0) & \cdots & \frac{\partial C_ {p_n}^m}{\partial x_1}(0) & \frac{\partial C_ {p_n}^m}{\partial x_2}(0) \\ \end{bmatrix} $$

$$ \dot{J} = \begin{bmatrix} \frac{\partial \dot{C}_ {p_0}^0}{\partial x_1}(0) & \frac{\partial \dot{C}_ {p_0}^0}{\partial x_2}(0) & \cdots & \frac{\partial \dot{C}_ {p_n}^0}{\partial x_1}(0) & \frac{\partial \dot{C}_ {p_n}^0}{\partial x_2}(0) \\ \vdots & & \vdots & & \vdots \\ \frac{\partial \dot{C}_ {p_0}^m}{\partial x_1}(0) & \frac{\partial \dot{C}_ {p_0}^m}{\partial x_2}(0) & \cdots & \frac{\partial \dot{C}_ {p_n}^m}{\partial x_1}(0) & \frac{\partial \dot{C}_ {p_n}^m}{\partial x_2}(0) \\ \end{bmatrix} $$

We can use the result:

$$ \ddot{C} = (J W J^T) {\lambda}^T + \dot{J} \dot{\widetilde{x}} + J W \ddot{\widetilde{x}} = - k_s C - k_d \dot{C} $$

And compute λ such that:

$$ (J W J^T) {\lambda}^T + \dot{J} \dot{\widetilde{x}} + J W \ddot{\widetilde{x}} + k_s C + k_d \dot{C} = 0 $$

We compute using an approximate least squares method.

Thanks

License

MIT License

Copyright (c) 2023 EmmanuelMess

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.