2-D translations:
My feeble attempt to make a dog with a wagging tail:
How could I make the tip of the tail move back and forth without the whole thing moving?
(I'll put my code in the comments)
Also could maybe go over some of the input methods like atan2() that uses the mouse's position?
int taleAngle = 0;
ReplyDeleteint angleChange = 2;
final int ANGLE_LIMIT = 8;
void setup()
{
size (400,400);
smooth();
frameRate(10);
}
void draw()
{
background(255,155,250);
pushMatrix();
translate(5,5); //keep tails always on screen
drawPuppy();
taleAngle += angleChange;
// if the arm has moved past its limit,
// reverse direction and set within limits.
if (taleAngle > ANGLE_LIMIT || taleAngle < 0)
{
angleChange = -angleChange;
taleAngle += angleChange;
}
popMatrix();
if (keyPressed == true && key=='s')
{
saveFrame("dog.jpg");
}
}
void drawPuppy()
{
stroke(0);
fill(255,235,155);
ellipse(200,200,150,50);
//triangle(260, 190, 260, 100, 275, 200);
ellipse(120,180, 50, 50);
rect(140, 215, 10, 60);
rect(160, 220, 10, 60);
rect(240, 220, 10, 60);
rect(220, 220, 10, 60);
drawTail();
}
void drawTail()
{
pushMatrix();
translate(10,-10);
rotate(radians(taleAngle));
triangle(260, 190, 275, 100, 275, 200);
popMatrix();
}
You have misunderstood a key concept in 2-D translations. We will talk about it when we next meet.
ReplyDeleteThe drawTail code below should be closer to what you intended.
void drawTail()
{
pushMatrix();
translate(260, 190);
rotate(radians(taleAngle)+PI);
triangle(0, 0, 20, 0, 20, 120);
popMatrix();
}