Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. To move your object towards a location,provided targeted location and current location, you can suppose your current location origin and then get the relative position of the target location in polar coordinate.
  2. This will give you the direction you need to travel.
  3. Given the speed you want the object to travel,
  4. Stepped example : (Click mouse to step, Hold down to see the hint, More content under demo 1, please scroll down to view Keynote 0)

    Processing
    Height600
    Width600
    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()//*******************************************************
    // Global variables
    //*******************************************************
    final int appwidth = 600;
    final int appheight = 600;
    final float stepsize = 10;
    targetedobject myobject;
    //*******************************************************
    // The class of the moving object
    //*******************************************************
    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;		// The flag whether the helper needs to be displayed or not
      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 drawhelperupdatedraw() {
      // Draws if ( this.demostep ) {  //Need drawthe shape
        pushMatrix();
        translate( mcurrentx, float textx, textymcurrenty );
        rotate( mrotation noFill();
          strokeWeightnoStroke(3);
          stroke( 0,fill( 255, 2550, 1920 );
          line( this.mcurrentx, this.mcurrenty, this.mtargetx, this.mtargety ellipse(0, 0, 40, 40);
          line( this.mtargetx, this.mcurrenty, this.mtargetx, this.mtargety noFill();
        stroke(255);
          line( this.mcurrentx, this.mcurrenty, this.mtargetx, this.mcurrentystrokeWeight(5);
        line( -10, 0, 10, 0 );
        line( 10, 0, 5, -5 textSize(24);
        line( 10, 0, textAlign(CENTER5, 5 BOTTOM);
          noStrokepopMatrix();
          fill(255this.drawhelper();
      }
      void drawhelper() {
       textx =if ( this.mtargetxdemostep ) +{ this.mcurrentx) // 2; Need draw helper
          textyfloat = this.mcurrentytextx, texty;
          textnoFill();
     "X2 - X1", textx, texty strokeWeight(3);
          textAlign(LEFT, CENTERstroke( 0, 255, 255, 192 );
           textx =line( this.mcurrentx, this.mcurrenty, this.mtargetx, this.mtargety );
          texty = (this.mtargety +line( this.mtargetx, this.mcurrenty, this.mtargetx, this.mcurrenty) / 2mtargety );
          textline( "Y2 - Y1", textx, texty this.mcurrentx, this.mcurrenty, this.mtargetx, this.mcurrenty );
          textSize(24);
          textAlign(CENTER, CENTER BOTTOM);
          noStroke();
          fill(255);
          textx = (this.mtargetx + this.mcurrentx) / 2;
          texty = (this.mtargety + this.mcurrenty) / 2;
          text( "LX2 - X1", textx, texty );
          textAlign(LEFT, CENTER);
          textx  noFill()= this.mtargetx;
          texty = strokeWeight(3)(this.mtargety + this.mcurrenty) / 2;
          stroke(255,0,0text( "Y2 - Y1", textx, texty );
          arc( this.mcurrentx, this.mcurrenty, 64, 64, 0, this.mrotation textAlign(CENTER, CENTER);
          textx = (this.mtargetx + this.mcurrentx) +/ 402;
          texty = (this.mtargety + this.mcurrenty) +/ 402;
          text( degrees(this.mrotation)"L", textx, texty );
        }
      }
       void startstep(int x, int y) {
     noFill();
         float targetlengthstrokeWeight(3);
         float temprotation stroke(255,0,0);
          arc( this.demostep = true;
       mcurrentx, this.mcurrenty, 64, 64, 0, this.mtargetxmrotation = x);
          this.mtargetytextx = ythis.mcurrentx + 40;
        targetlength = sqrt(texty pow(= this.mtargetx - this.mcurrentx, 2 )mcurrenty + 40;
          powtext(  degrees(this.mtargety - this.mcurrenty, 2 )mrotation), textx, texty );
      //Get the target distance}
      }
      ifvoid startstep(targetlength <= 0.5int x, int y) {
        float targetlength;
       this.mrotation =float 0temprotation;
        this.demostep = return;  true;
     
    	//Do Remember nottarget movecoordinates
        }
    this.mtargetx = x;
      if (targetlength > stepsize) {  //Calculate step size
          this.thisstep = stepsize;this.mtargety = y;
     
    	// Calculate the distance between the endpoint and start point
        }
    targetlength = sqrt( pow( else {
         this.mtargetx - this.thisstepmcurrentx, =2 targetlength;
        }) + 
        temprotation = acos( (this.mtargetx 					 pow(  this.mtargety - this.mcurrentx) / targetlength );mcurrenty, 2 ) );  //Get the target distance
        if (targetlength this.mtargety > this.mcurrenty <= 0.5) {  //> 180 degrees
          this.mrotation = temprotation0;
        }else{
      return;  //Do not this.mrotationmove
     = 2 * PI}
     - temprotation;
      if (targetlength }
    > stepsize) {  // Calculate the step targetsize
        this.mtargetx_t = (int)(this.thisstep * cos(this.mrotation))= stepsize;
        this.mtargety_t = (int)(this.thisstep * sin(this.mrotation));
      }
      void endstep() {}
        else {
          this.thisstep = targetlength;
        }
     
    	// Get the direction (0~180 degree(s))
        this.demosteptemprotation = false;
       acos( (this.mtargetx - this.mcurrentx) += this.mtargetx_t;
       / targetlength );
        if ( this.mtargety > 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);
      }
    } ) {
          this.mrotation = temprotation;
        }else{
          this.mrotation = 2 * PI - temprotation;	// > 180 degrees
        }
        // 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;
    	// Move the object
        this.mcurrentx += this.mtargetx_t;
        this.mcurrenty += this.mtargety_t;
      }
    }
    //*******************************************************
    // Setup the size and the moving object
    //*******************************************************
    void setup() {
      size(appwidth, appheight);
      background(0);
      myobject = new targetedobject( width / 2, height / 2, 0 );
    }
    //*******************************************************
    // Update the draw
    //*******************************************************
    void draw() {
      background(0);
      myobject.updatedraw();
    }
    //*******************************************************
    // When mouse is pressed, start the stepping 
    // Calculation process
    //*******************************************************
    void mousePressed() {
      if (mouseButton == LEFT) {
        myobject.startstep(mouseX, mouseY);
      }
    }
    //*******************************************************
    // After the mouse is released,
    // End the stepping and move the object
    //*******************************************************
    void mouseReleased() {
      if ( myobject.demostep ) {
        myobject.endstep();
      }
    }
  5. Anchor
    keynote00
    keynote00

    Processingcode
    titleKey note
    //Get the target distance
    targetlength = sqrt( pow( this.mtargetx - this.mcurrentx, 2 ) + 
                         pow( this.mtargety - this.mcurrenty, 2 ) );
     
    //Arc cosin adjacent side divide by Hypotenuse
    //OR arc sin subtense divide by Hypotenuse
    //Not using tangent to avoid Divide by Zero
    temprotation = acos( (this.mtargetx - this.mcurrentx) / targetlength );
     
    //In this case, we are using cosin, so for the angle > 180 which means that
    //targety is under currend y
    if ( this.mtargety > this.mcurrenty ) {  //> 180 degrees
        this.mrotation = temprotation;
    }else{
        this.mrotation = 2 * PI - temprotation;
    }
     
    //Now that we have the rotation, we recalculate the x and y we need to travel
    this.mtargetx_t = (int)(this.thisstep * cos(this.mrotation));
    this.mtargety_t = (int)(this.thisstep * sin(this.mrotation));
    
     
    //Finally, move the object
    this.mcurrentx += this.mtargetx_t;
    this.mcurrenty += this.mtargety_t;
  6. Continuous Example

    Processing
    final int appwidth = 600;
    final int appheight = 600;
    final float stepsize = 5;
    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();
      }
      void moveto(int x, int y) {
        this.mtargetx = x;
        this.mtargety = y;
        //Get the target distance
        float targetlength = sqrt( pow( this.mtargetx - this.mcurrentx, 2 ) + 
          pow( this.mtargety - this.mcurrenty, 2 ) );
        if(targetlength < 0.5){
          return;
        }
        //Arc cosin adjacent side divide by Hypotenuse
        //OR arc sin subtense divide by Hypotenuse
        //Not using tangent to avoid Divide by Zero
        float temprotation = acos( (this.mtargetx - this.mcurrentx) / targetlength );
        //In this case, we are using cosin, so for the angle > 180 which means that
        //targety is under currend y
        if ( this.mtargety > this.mcurrenty ) {  //> 180 degrees
          this.mrotation = temprotation;
        }
        else {
          this.mrotation = 2 * PI - temprotation;
        }
        if (targetlength > stepsize) {  //Calculate step size
          this.thisstep = stepsize;
        }
        else {
          this.thisstep = targetlength;
        }
        //Now that we have the rotation, we recalculate the x and y we need to travel
        this.mtargetx_t = (int)(this.thisstep * cos(this.mrotation));
        this.mtargety_t = (int)(this.thisstep * sin(this.mrotation));
    
        //Finally, move the object
        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 );
      frameRate(30);
    }
    void draw() {
      background(0);
      myobject.moveto( mouseX, mouseY );
      myobject.updatedraw();
    }

...