You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Pre-requisite:

  1. Polar and Cartesian Coordinates

Objectives:

  1. To demonstrate an object launch to a target point

Descriptions:

  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)

    Source
     
    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.mcurrentx ) {  //> 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. Essential Code explaination
     
     
    

     

     

 

  • No labels