final int appwidth = 600;
final int appheight = 600;
final float stepsize = 10;
targetedobject myobject;
class targetedobject { // This is the object to move
int mcurrentx, mcurrenty; // This will store the current location of the object
int mtargetx, mtargety; // This will store the target location of the object
int mtargetx_t, mtargety_t; // Step travel
float mrotation; // This is the current orientation of this object
float thisstep;
boolean demostep;
targetedobject( int x, int y, int r ) { // Initialize with coordinate and orientation
mcurrentx = x;
mtargetx = x;
mcurrenty = y;
mtargety = y;
mrotation = r;
demostep = false;
}
void updatedraw() { //Draws the shape
pushMatrix();
translate( mcurrentx, mcurrenty );
rotate( mrotation );
noStroke();
fill( 255, 0, 0 );
ellipse(0, 0, 40, 40);
noFill();
stroke(255);
strokeWeight(5);
line( -10, 0, 10, 0 );
line( 10, 0, 5, -5 );
line( 10, 0, 5, 5 );
popMatrix();
this.drawhelper();
}
void drawhelper() {
if ( this.demostep ) { //Need draw
float textx, texty;
noFill();
strokeWeight(3);
stroke( 0, 255, 255, 192 );
line( this.mcurrentx, this.mcurrenty, this.mtargetx, this.mtargety );
line( this.mtargetx, this.mcurrenty, this.mtargetx, this.mtargety );
line( this.mcurrentx, this.mcurrenty, this.mtargetx, this.mcurrenty );
textSize(24);
textAlign(CENTER, BOTTOM);
noStroke();
fill(255);
textx = (this.mtargetx + this.mcurrentx) / 2;
texty = this.mcurrenty;
text( "X2 - X1", textx, texty );
textAlign(LEFT, CENTER);
textx = this.mtargetx;
texty = (this.mtargety + this.mcurrenty) / 2;
text( "Y2 - Y1", textx, texty );
textAlign(CENTER, CENTER);
textx = (this.mtargetx + this.mcurrentx) / 2;
texty = (this.mtargety + this.mcurrenty) / 2;
text( "L", textx, texty );
noFill();
strokeWeight(3);
stroke(255,0,0);
arc( this.mcurrentx, this.mcurrenty, 64, 64, 0, this.mrotation );
textx = this.mcurrentx + 40;
texty = this.mcurrenty + 40;
text( degrees(this.mrotation), textx, texty );
}
}
void startstep(int x, int y) {
float targetlength;
float temprotation;
this.demostep = true;
this.mtargetx = x;
this.mtargety = y;
targetlength = sqrt( pow( this.mtargetx - this.mcurrentx, 2 ) +
pow( this.mtargety - this.mcurrenty, 2 ) ); //Get the target distance
if (targetlength <= 0.5) {
this.mrotation = 0;
return; //Do not move
}
if (targetlength > stepsize) { //Calculate step size
this.thisstep = stepsize;
}
else {
this.thisstep = targetlength;
}
temprotation = acos( (this.mtargetx - this.mcurrentx) / targetlength );
if ( this.mtargety > this.mcurrenty ) { //> 180 degrees
this.mrotation = temprotation;
}else{
this.mrotation = 2 * PI - temprotation;
}
// Calculate the step target
this.mtargetx_t = (int)(this.thisstep * cos(this.mrotation));
this.mtargety_t = (int)(this.thisstep * sin(this.mrotation));
}
void endstep() {
this.demostep = false;
this.mcurrentx += this.mtargetx_t;
this.mcurrenty += this.mtargety_t;
}
}
void setup() {
size(appwidth, appheight);
background(0);
myobject = new targetedobject( width / 2, height / 2, 0 );
}
void draw() {
background(0);
myobject.updatedraw();
}
void mousePressed() {
if (mouseButton == LEFT) {
myobject.startstep(mouseX, mouseY);
}
}
void mouseReleased() {
if ( myobject.demostep ) {
myobject.endstep();
}
}