How to make Gears mesh
(I don't know a lot of gear type terminology...)
I have two gears (Gear A and B) that I'm trying to get to mesh.
Things That Work
I've successfully written code that makes sure the gears are the correct distance apart and that rotates gears with varying numbers of cogs at the appropriate speed based on this number.The Problem
I am having trouble calculating at what rotation gear B should be when it's placed next to Gear A so that the cogs on the gears mesh together (the cog size is the same on both gears).Gear A is centered at x1,y1 and is rotated at q degrees. Gear B is centered at x2,y2 and is d distance from Gear A. The distance between two cogs on a gear is c.
I use Math.atan2(y2-y1, x2-x1) *(180/Math.PI);
to calculate the angle between the two gears relative to the origin but don't always get good results (may be an issue in my code somewhere else, not sure...) My thought was to use this angle and q to compute a rotational value for gear B.
I could probably use q%c
to get a more precise/smaller r开发者_Go百科otation value for gear B, but not sure quite how to proceed.
Consider a gear with n cogs. If n is even, then opposite every cog is another cog. If n is odd then opposite every cog is a notch (or whatever the correct term is). In either case, if you rotate the gear by 2π/n radians, it will look the same as it did before-- each cog has "moved over". So angle matters only modulo 2π/n.
Consider two identical gears, meshing, both centered on the x-axis (y=0). If n is odd, then their angle can be equal (we can call that angle 0, when a cog is pointing in the x direction), and in general if one gear is at angle a, then the other will be at angle -a. If n is even then when one gear is at 0, the other will be at π/n, and in general when one is at a the other will be at π/n-a.
Now suppose that the gear on the left ("gear A") has n cogs and the gear on the right ("gear B") has m cogs. If m is odd then when gear B is at am, gear A is at -an. If m is even, when gear B is at am, gear A is at π/n-an.
Now suppose that their centers are not at the same y value, so that the center of gear B is at angle b as seen from the center of gear A. Now the subtract that angle from the angle of rotation of each gear, and you reduce the problem to the previous one.
精彩评论