Circle Gears Code

About advanced scenes, and the Thyme scripting language used in Algodoo.

Circle Gears Code

Postby phunHunger » Thu Dec 16, 2010 12:08 am

The code for creating circle gears with radius (of inner circle) and number of teeth that alternate between no and A collision sets:

Code: Select all
Scene.my.makeCog:=(n,r)=>{
cs=0;
a=(2*math.pi)/n;
c=0;
lr=r*sin(math.pi/n);

Scene.addCircle({
  radius:=r;
  pos:=[0,0];
  body := Scene.my.bodyn;
  glued := true;
  collideSet:=0;
});

for(n,(c)=>{
  t=a*c;
  (c%2>0)?{cs=1}:{cs=0};
  Scene.addCircle({
   pos:=[r*cos(t),r*sin(t)];
   radius:=lr;
   body := Scene.my.bodyn;    glued := true;
   collideset:=cs
  });
});

Scene.my.bodyn = Scene.my.bodyn + 1
};


n = number of teeth
r = radius of inner circle
a = angle between teeth positions
Thyme works in radians so 2 x pi is a full circle. The full circle divided by number of teeth equals the angle between them. So a=(2*math.pi)/n.
c = number of individual tooth
lr = teeth radius
Using trigonometry r*sin(math.pi/n) is the radius of each tooth. If you draw a triangle with one corner at the centre of the gear, one at the centre of a circle and the other two at the centre of two teeth that are next to each other, two right-angled triangles can be made from this triangle with a forth line going from the centre of the gear to the centre of the line connecting the two teeth centres. Then using one of these to calculate what the tooth radius should be, an equation can be formed. Half of the angle between teeth positions is the acute angle at the centre of the gear in our triangle. To get half of this angle we could say a/2 but in the code, math.pi/n is used which is the same thing. Hypotenuse times the sin of this angle equals the opposite of the angle which is the radius of each tooth!
circle position
This can also be found using trigonometry. From a hypotenuse and an angle, we can use trigonometry to find the x and y of the teeth. In the code, t=a*c is used which just finds the angle of the circle using a which is the angle between each tooth. Timesing that by the circle number gives us an angle about the origin which can be used with the radius of the inner circle to find the x and y positions.
bodyn = body id of incrementing value
Noobs are the best ;)
User avatar
phunHunger
 
Posts: 31
Joined: Mon Apr 26, 2010 10:01 pm

Re: Circle Gears Code

Postby Mystery » Wed Dec 22, 2010 3:51 am

hmm no comments.
Rather amazing work of code, i might find a need to use this one day.
User avatar
Mystery
 
Posts: 2802
Joined: Thu Sep 03, 2009 1:16 pm
Location: Southern Australia

Re: Circle Gears Code

Postby Kilinich » Wed Dec 22, 2010 8:37 am

Don't forget to declare scene.my.bodyn before use.
Dream of Algodoo as game development engine...
User avatar
Kilinich
[Best bug reporter 2010]
 
Posts: 2098
Joined: Mon Aug 31, 2009 8:27 pm
Location: South Russia


Return to Thyme scripting

Who is online

Users browsing this forum: No registered users and 2 guests

cron