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)

    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();
      }
      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 );
        }
      }
      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.mcurrentxmcurrenty ) {  //> 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();
      }
    }
  5. Code Block
    themeMidnight
    languagejava
    titleKey Note
    //Get the target distance
    targetlength = sqrt( pow( this.mtargetx - this.mcurrentx, 2 ) + 
                         pow(  this.mtargety - this.mcurrenty, 2 ) );
     
    //Arc cosin adjacent side //Get the target distancedivide 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 //Arc cosin diagcosin, so for the angle > 180 which means that
    //targety is under currend y
    if ( this.mtargety > this.mcurrentxmcurrenty ) {  //> 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;