We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi: This is a simple gears visual simulation test code I built for fun and wanted to share it with yoiu. Please let me know if you have any suggestions or find any problems.
Thanks,
and here is the code:
//Simple Gears(visual simulation test code).
//Built with Processing 2.03 by Adrian Fernandez. Miami, FL. USA. (07-31-2014).
final int minNumberOfTeeth=3;
final int maxNumberOfTeeth=40;
void setup()
{
size(1350, 690);
frameRate(60);
textAlign(CENTER);
textSize(18);
}
void draw()
{
background(0);
int radio=100;
float teethHeight=0.18*radio;
float centerPositionX=2*width/3-radio-teethHeight/2;
float centerPositionY=height/2;
int rotationDirection=1;
float rotationSpeed=1;
drawGear(radio, centerPositionX, centerPositionY, teethHeight, rotationDirection, rotationSpeed);
radio=50;
rotationDirection=-1;
rotationSpeed=2;
centerPositionX=2*width/3+radio+teethHeight/2;
centerPositionY=height/2-teethHeight/(2*rotationSpeed);
drawGear(radio, centerPositionX, centerPositionY, teethHeight, rotationDirection, rotationSpeed);
radio=200;
rotationDirection=-1;
rotationSpeed=0.5;
centerPositionX=2*width/3-2*radio-4*teethHeight/3;
centerPositionY=height/2+3*teethHeight/(2*rotationSpeed);
drawGear(radio, centerPositionX, centerPositionY, teethHeight, rotationDirection, rotationSpeed);
fill(255);
text("Move mouse sideways to see rotation.", width/2, 100);
}
void drawGear(int radio, float centerPositionX, float centerPositionY, float teethHeight, float rotationDirection, float rotationSpeed )
{
float rotationAngle=map(mouseX, 0, width, 0, TWO_PI );
int numberOfTeeth=radio/5;
numberOfTeeth=constrain(numberOfTeeth, minNumberOfTeeth, maxNumberOfTeeth);
float teethAngle=TWO_PI/numberOfTeeth;
float teethWidth=sin(teethAngle/2)*radio;
float lineY=cos(teethAngle/2)*radio+teethHeight;
pushMatrix();
translate(centerPositionX, centerPositionY);
rotate( rotationDirection*rotationSpeed*rotationAngle);
fill(150);
stroke(255);
for (int i=0; i<numberOfTeeth; i++)
{
rotate(teethAngle);
stroke(200);
strokeWeight(1);
triangle(-3*teethWidth/4, -lineY+teethHeight, teethWidth/2, -lineY+teethHeight, -teethWidth/2, -lineY);
triangle(teethWidth/4, -lineY, -teethWidth/2, -lineY, teethWidth/2, -lineY+teethHeight);
stroke(150);
strokeWeight(2);
line(-teethWidth/2, -lineY, teethWidth/2, -lineY+teethHeight);
}
stroke(100);
ellipse(0, 0, 2*(-lineY+teethHeight), 2*(-lineY+teethHeight)) ;
stroke(80);
strokeWeight(radio/2);
ellipse(0, 0, radio, radio);
fill(0);
noStroke();
ellipse(0, 0, radio/5, radio/5);//Shaft
AdditionsBlock(radio);
popMatrix();
}
void AdditionsBlock(float radio)
{
rectMode(CENTER);
rect(0, -radio/10, radio/20, -radio/15);
ellipse(0, 0.85*radio, radio/15, radio/15);//counterWeight
rotate(PI/20);
ellipse(0, 0.85*radio, radio/15, radio/15);//counterWeight
rotate(PI/20);
ellipse(0, 0.85*radio, radio/15, radio/15);//counterWeight
}
Comments
Very nice! ;)
Thank you.
Made some turbines with few modifications to the same code.
Hi! I've made a total code refactoring and post it online: :ar!
http://studio.processingtogether.com/sp/pad/export/ro.9WrTp2GiTlnLW/latest
It's using a class + static background. Only the dents and holes are actually dynamically rendered! :bz
Check it out the modded code below: :D
Well, that's a more sophisticated way of doing it, thanks for sharing. I noticed a couple of things:
-In the online version the tooth's edges look really smooth; but the division line between the 2 triangles is visible. In my version, I added the line (code line 63) to erase it.
-When run in the computer the division line is not visible; but the edges look jagged.
I see you really liked this one.. ;)
Thanks
HEHE! Made it for performance and organization reasons.
Using noSmooth() in "Java Mode" seems to blend the 2 triangle() drawings together well.
But it seems like it doesn't transpose that well for "JavaScript Mode"! :-&
Can you add the line to make the triangles blend?
It's done now! New 2.03 version w/ an additional drawGapFill() method! =:)
It's conditioned to call that online only though! ;)
Ok thanks.
Update: Added a Reciprocating Engine with system inertia for acceleration and braking.
Still with problems related to the math and physics.
-To simulate inertia the angular speed varies exponentially tending asymptotically to the final speed value in both cases (acceleration and braking). In reality I don't know if that's the equation in a real physical system like that; but the visual effect is acceptable. When braking the rotation even reverses (by the end of the video) something I wasn't expecting.
These are the equations I'm using to control the angular speed.
-For acceleration [1-exp(-t/TC)] ----------TC is the Time Constant
-For braking [exp(-t/TC)]
Still under construction...
Here is how it looks: