Most fustration and making two gears to mesh correctly!

I have a issue with two gears. I am making a __ application that simulated two spur gears to mesh with each other. The teeth should mesh together, then, when one is roated, the force of the surface of the side of one or two teeth, moves the gear it is driving. also. both gear are not fixed in a given position so if one gear is pushed into the next gear, the teeth should mesh and would still push the other gear. I want to also hand rotate one gear it will spin the other. I have the code here, im not to much interested in the design gui for the gear, just need the physics resolved. deep-seek cant get it right and some of the other ai chat engines cannot.

KCAD Gear Sim 3D :root{--bg:#10151c;--fg:#dde5ee;--dim:#8894a4;--ac:#4aa3ec;--warn:#e8590c;--ok:#2fa84f} html,body{margin:0;height:100%;background:var(--bg);color:var(--fg);font:13px system-ui,sans-serif;overflow:hidden} #wrap{display:flex;height:100vh;width:100vw} #side{width:340px;flex:0 0 340px;padding:10px;overflow:auto;border-right:1px solid #2a3340;box-sizing:border-box} #cv{flex:1 1 auto;display:block;width:100%;height:100%;min-width:0;min-height:0;background:#10151c} h3{margin:14px 0 4px;font-size:13px;color:var(--ac);text-transform:uppercase;letter-spacing:.6px} label{display:flex;justify-content:space-between;align-items:center;gap:8px;margin:3px 0;color:var(--dim)} input,select,button{font:inherit;background:#1b222c;color:var(--fg);border:1px solid #33404f;border-radius:4px;padding:3px 6px} input\[type=number\]{width:72px} button{cursor:pointer} button:hover{border-color:var(--ac);color:var(--ac)} #log{font-family:ui-monospace,Menlo,monospace;font-size:11px;line-height:1.4;white-space:pre-wrap;max-height:240px;overflow:auto;background:#0b1017;border:1px solid #222c38;border-radius:4px;padding:6px;color:var(--dim)} .row{display:flex;gap:6px;margin:4px 0} .row>\*{flex:1}
<h3>Add</h3>

<div class="row"><button id="add1">+ Gear A</button><button id="add2">+ Gear B</button></div>

<label>Module (mm)<input id="m" type="number" value="2" step=".5"></label>

<label>Teeth A<input id="zA" type="number" value="20" step="1"></label>

<label>Teeth B<input id="zB" type="number" value="30" step="1"></label>

<label>Face width (mm)<input id="fw" type="number" value="8" step="1"></label>

<label>Pressure angle (°)<input id="pa" type="number" value="20" step="1"></label>

<label>Bore Ø (mm, 0 = solid)<input id="bore" type="number" value="0" step="1" min="0"></label>



<h3>Mesh / motor</h3>

<div class="row"><button id="mesh">Mesh A–B</button><button id="unmesh">Unmesh</button></div>

<label>Motor on A<input id="motOn" type="checkbox" checked></label>

<label>Torque (mN·m)<input id="tq" type="number" value="40"></label>

<label>No-load rpm<input id="rpm" type="number" value="300"></label>



<h3>Physics</h3>

<label>Tooth friction μ<select id="lube"><option>0.25</option><option selected>0.10</option><option>0.05</option></select></label>

<label>Bearing<select id="brg"><option selected>plain</option><option>bushing</option><option>ball</option><option>none</option></select></label>

<label>Substep (ms)<input id="sub" type="number" value="1" step=".5" min=".1"></label>

<label>Speed 1×<input id="scale" type="range" min="0" max="2" step=".05" value="1" style="flex:1">2×</label>

<div class="row"><button id="run">▶ Run</button><button id="pause">⏸ Pause</button><button id="reset">↺ Reset</button></div>



<h3>Collision</h3>

<label>Mode<select id="col"><option value="on" selected>On — real tooth prisms</option><option value="off">Off</option></select></label>

<label>Penetration tol (µm)<input id="tol" type="number" value="50" step="10"></label>

<label>Restitution<input id="rest" type="number" value="0.15" step=".05" min="0" max="1"></label>

<label>Contact friction<input id="mu" type="number" value="0.15" step=".05" min="0"></label>



<h3>Diagnostics</h3>

<div id="stats" style="font-family:ui-monospace,Menlo,monospace;font-size:11.5px;line-height:1.5;color:var(--dim);white-space:pre"></div>

<h3>Log</h3>

<div id="log"></div>

gear is not complex you need two thing
rotation and ratio
ex: if gear 1 turn , gear 2 turn = gear 1 rotation * ratio
ratio can be 0.5, 2, -2 …

The ratio only works after both gears already sit on fixed axles. Here the centers can move, so the drive has to come from contact.

Put the impulse on the line of action, not on every tooth triangle. For a 20 degree pressure angle that line sits 20 degrees off the common tangent of the two pitch circles. When the center distance is near module times (teeth A plus teeth B) divided by 2, apply an equal and opposite impulse along that line, and a torque on each gear equal to the impulse times the base-circle radius. That both shoves a free gear away and spins it.

Tunneling is a step-size problem. Each substep should rotate a gear by less than half a tooth, about pi times the module divided by the tooth count, measured along the pitch circle. If one step can jump a whole tooth, friction and restitution will not catch the flank.

Use the pitch circles to decide the pair is in mesh, then test only the one or two flank faces the line of action actually crosses.