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

Compare with Current View Page History

« Previous Version 4 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)

    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
      float mrotation;      // This is the current orientation of this object
      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 drawhelper(){
        
      }
      void startstep(int x, int y){
        this.demostep = true;
        this.mtargetx = x;
        this.mtargety = y;
      }
      void endstep(){
        this.demostep = false;
      }
    }
    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();
      }
    }
    
    
    

 

  • No labels