Practical Use of AI Coding Models in Physics Simulation: Building a Lightweight Kinematic Solver

    This article shares a practical case study of using AI coding models to build a lightweight, pure-kinematic constraint solver alongside a self-developed physics simulation engine. It explains why dynamics-based solvers are overkill for purely kinematic linkages, how to turn a vague request into verifiable engineering requirements and a reusable prompt template, what to observe during solution generation, how to decide when iteration has converged, and reusable engineering recommendations for delegating non-core, high-frequency tooling tasks to AI coding models.

    1. Background: Why a Lightweight Kinematic Solving Capability Is Needed

    In the development environment of a self-developed physics simulation engine, the core solver typically relies on impulse-based soft-constraint dynamics modeling. This approach expresses contact, friction, joint limits, and similar phenomena as constraints, iteratively solves a velocity-level linear system to obtain impulses, and then integrates them into poses. Its advantages are clear: the physical meaning is transparent, it is numerically forgiving, and it is robust for rigid-body stacking and contact jitter. This is why it has become the mainstream choice for interactive simulation.

    But problems come with it. For purely kinematically coupled linkage mechanisms—such as worm-and-worm-gear reduction drives, parallel four-bar linkages, scissor mechanisms, and rack-and-pinion systems—what we actually care about is not force or acceleration, but the geometric relationship that “a given input angle uniquely determines the end-effector pose.” Using a dynamics solver to handle such mechanisms is like bringing in a full set of hydraulic equipment just to open a bottle of water: you need to set mass, inertia, damping, and drive stiffness, and repeatedly tune parameters to keep the numerics stable. The computational cost is far higher than the complexity of the problem itself.

    So the idea is straightforward: in addition to the dynamics solver, add a lightweight pure-kinematic constraint solving module for scenarios such as mechanism topology validation, model-building assistance, and automated test fixtures. This kind of demand is a “non-core but high-frequency” tooling need—low in priority and often left on the shelf for a long time. The emergence of AI coding models happens to provide a suitable testing ground for small tasks with clear boundaries and quantifiable acceptance criteria.

    It is worth emphasizing that the difficulty of such tasks lies not in the amount of code, but in implicit constraints: determining the degrees of freedom of a mechanism, loop cutting in closed kinematic chains, choosing among multiple solution branches, numerical degeneration near singular configurations, and consistency of unit and coordinate-system conventions. These constraints are often not written into the requirements, yet they determine whether a solution can truly be put into practice.

    2. Requirement Breakdown: Turning a “Wish-Style Requirement” into Verifiable Engineering Items

    The most common failure mode in practice is handing a single natural-language wish directly to the model and then expecting runnable code in return. A more reliable approach is to do the requirements engineering yourself first, breaking vague statements into decidable items:

    • Input: mechanism topology description (bodies, joint types, connection relationships), driver variable values, and an initial pose seed.
    • Output: poses of each body, end-effector pose, Jacobian matrix (optional), and diagnostic information such as error and iteration count.
    • Constraints: closed loops must converge within a residual threshold; no NaN should be produced near singular configurations; the same input should converge to the same branch under different initial values, or clearly report branch differences.
    • Non-functional requirements: no heavy dependencies, runnable as a single file, easy to embed into the existing toolchain, and readable logs provided.

    Putting these items into the prompt significantly improves the usability of the model’s output. A reusable prompt skeleton is as follows:

    1. Role: Senior multibody kinematics engineer
    2. Task: Implement a pure kinematic closed-loop constraint solver (no dynamics)
    3. Input format: <topology JSON example>
    4. Output requirements:
    5. 1) Pose results + residual + iteration count
    6. 2) Jacobian and damped least squares (DLS) strategy
    7. 3) Singular configuration detection and warnings
    8. Acceptance: residual < 1e-6, no NaN across 200 random inputs
    9. Prohibited: introducing heavy simulation frameworks; using unspecified third-party interfaces

    The key here is not polished wording, but putting acceptance criteria up front. Once clearly constrained, the model’s output is closer to a “reviewable design draft” rather than demo code that needs extensive rework.

    3. Observations from the Solution Generation Stage: Speed Is Not the Only Dimension

    In the first round of implementation, different models and different tiers (fast tier and high-performance tier) produced highly convergent technical routes: all chose the “closed-loop constraints + numerical iteration” approach rather than reintroducing dynamics. This shows that for problems with clear boundaries, mainstream models are already quite stable in solution selection, with differences mainly in engineering details and response pacing.

    Regarding time consumption, one easily misread phenomenon is that in tasks requiring understanding the project structure from scratch, the actual time taken by models across tiers is significantly leveled out. The reason is that the cold-start phase requires multiple retrievals and context reading, and this overhead has little to do with the model’s own inference speed; only after the context is sufficient and the task converges do speed differences between tiers become apparent.

    Another style difference worth noting is the length of the chain of thought. A shorter reasoning process yields a faster first version, suitable for quickly validating a direction; but it is also more likely to miss implicit constraints and drift off course in “one-sentence wish” scenarios. In contrast, a longer reasoning process proactively fills in boundary conditions, at the cost of increased time. The engineering compromise is: use the fast tier during direction exploration, and the high-performance tier for key modules and acceptance stages.

    From a version-evolution perspective, earlier models were more like “smart but inexperienced engineering graduates”: capable, but with scattered boundary handling; later versions improved markedly in robustness, handling degenerate cases and input validation more proactively. For real projects, this kind of change is often more valuable than a pure improvement in response speed.

    4. The Iteration Convergence Stage: How to Decide “We Can Stop”

    After multiple rounds of iteration, the solutions converge structurally, and continuing to have multiple models do repetitive work only wastes time. To judge convergence, rely on the following signals:

    1. The design draft covers all acceptance items, and key algorithm choices have reached a consistent conclusion;
    2. Error-prone points such as residuals, singularity detection, and unit conventions are all handled explicitly;
    3. Remaining differences are only code style and naming, no longer affecting runnability;
    4. Subsequent changes are matters of human review preference rather than defect fixes.

    Once these conditions are met, you can stop “multi-model parallelism” and move into the human review and test-completion stage.

    5. Reusable Engineering Recommendations

    • Tool non-core requirements: prioritize tasks whose acceptance criteria can be automated for AI coding models, such as solvers, parsers, data conversion, and test fixtures.
    • Drive acceptance with tests: write residual assertions and randomized-input regression first, then let the model complete the implementation, avoiding subjective judgment.
    • Use models in tiers: fast tier for exploration, high-performance tier for convergence, and humans for review.
    • Explicitly declare prohibited items: banning heavy frameworks and unspecified external interfaces can significantly reduce rework.
    • Retain diagnostic information: outputs such as iteration count, residual, and condition number are first-hand material for locating numerical problems.
    • Build up prompt assets: incorporate reusable prompt skeletons into the team knowledge base to form stable productivity.

    In summary, a lightweight kinematic solver is an ideal pilot scenario: clear requirement boundaries, quantifiable acceptance, and low failure cost. With the help of AI coding tools, a team can quickly obtain a reviewable design draft and a runnable prototype without investing core R&D resources, and then leave the engineering judgment to the developers themselves.