Hi all,
please try to help me undestanding translate() combined with rotate(radians()).
My left valve is working nearly as expected but the right valve get() function is not referencing the top of the valve.
Thanks a lot!
grillgemuese
//4 stroke animation 20.03.2019
//update names into eng
PImage engineblock, crankshaft, rod, piston, valve, camshaft;
final int camMidX = -11, camMidY = -19;
final int ignitionAngle = 15;
final color BGCol = #F0F0F0;
boolean engineState = true, spark = false;
int lastMs = 0, crankshaftDeg = 0;
int tx = 0, ty = 0;
int leftValvePos = 0, rightValvePos = 0;
color colRightValve = #F016E2, colLeftValve = #F016E2;
void setup()
{
size(228, 400);
frameRate(60);
surface.setTitle("Engine");
engineblock = loadImage("engineblock_transparent.png");
crankshaft = loadImage("crankshaft_transparent.png");
rod = loadImage("rod_transparent.png");
valve = loadImage("valve_transparent.png");
camshaft = loadImage("camshaft_transparent.png");
piston = loadImage("piston_transparent.png");
}//void setup() END
void draw()
{
background(BGCol);
//fill(0);
//textSize(15);
//text("IN", 2, 128);
//text("OUT", 195, 128);
drawEngine();
drawCrankshaft();
drawRod();
drawPiston();
//rect(mouseX-10, mouseY-10, 20, 20);
drawCamshafts();
drawLeftValve();
drawRightValve();
println("tx:" + tx + " ty:" + ty);
//print("x:" + mouseX, " y:" + mouseY + " = ");
//println(hex(get(mouseX, mouseY)));
if (engineState && millis() - lastMs >= 5)
{
lastMs = millis();
crankshaftDeg += 2;
if (crankshaftDeg >= 360*2) crankshaftDeg = 0;
if (crankshaftDeg >= 360 - ignitionAngle && crankshaftDeg <= 360 + ignitionAngle + 5) spark = true;
else spark = false;
}
fill(#F01616);
if (spark) rect(10, 10, 20, 20);
}//void draw() END
void keyPressed()
{
if (key == 'w') ty--;
else if (key == 's') ty++;
else if (key == 'a') tx--;
else if (key == 'd') tx++;
}
void drawEngine()
{
pushMatrix();
scale(2.0);
image(engineblock, 17, 10);
popMatrix();
}
void drawCrankshaft()
{
pushMatrix();
scale(2.0);
translate(57, 142);
rotate(radians(crankshaftDeg));
image(crankshaft, -14, -19);
popMatrix();
}
void drawRod()
{
pushMatrix();
scale(2.0);
translate(47, 86);
//rotate(radians(ty));
//get()
image(rod, 0, 0);
popMatrix();
}
void drawPiston()
{
pushMatrix();
scale(2.0);
translate(40, 75);
image(piston, 0, 0+0);
popMatrix();
}
void drawLeftValve()
{
pushMatrix();
scale(0.9);
translate(31, 109);
rotate(radians(-40));
colRightValve = get(valve.width + leftValvePos, valve.height + leftValvePos + 5);
//println("lVP:" + leftValvePos + " col:" + hex(colRightValve));
if(colRightValve == BGCol)
{
leftValvePos--;
}
else if(colRightValve != BGCol)
{
leftValvePos++;
}
image(valve, 0, leftValvePos);
popMatrix();
}
void drawRightValve()
{
pushMatrix();
scale(0.9);
translate(196, 83);
rotate(radians(40));
colLeftValve = get(valve.width + rightValvePos, valve.height + rightValvePos + 5);
//rect(valve.width + rightValvePos, valve.height + rightValvePos + 5, 10, 10);
println("rVP:" + rightValvePos + " col:" + hex(colLeftValve));
if(colLeftValve == BGCol)
{
rightValvePos--;
}
else if(colLeftValve != BGCol)
{
rightValvePos++;
}
image(valve, 0, rightValvePos);
popMatrix();
}
void drawCamshafts()
{
//left
pushMatrix();
scale(1.5);
translate(18, 48);
rotate(radians(crankshaftDeg / 2 + 90));
image(camshaft, camMidX, camMidY);
popMatrix();
//right
pushMatrix();
scale(1.5);
translate(136, 48);
rotate(radians(crankshaftDeg / 2 - 90));
image(camshaft, camMidX, camMidY);
popMatrix();
}