SImple Touch Path Sketch
in
Android Processing
•
9 months ago
I've made a simple sketch that draws a line for an object to follow at an specified speed. I'm curious to see how others would implement this or if there is a commonly used solution for this already.
- import android.view.MotionEvent;
- PFont junction;
- int currentTime;
- int lastTime;
- Mover mover;
- boolean touched;
- void setup(){
- size( displayWidth, displayHeight );
- orientation( PORTRAIT );
- strokeWeight(1.5);
- mover = new Mover();
- junction = createFont("Junction.otf", 32);
- textFont( junction , 32);
- lastTime = millis();
- touched = false;
- }
- void draw(){
- background(255);
- mover.display();
- mover.update();
- fill(0);
- text( "Point size: " + mover.motionLine.points.size(), 10, 50);
- text( "Frame Rate: " + frameRate, 10, 100);
- }
- public boolean surfaceTouchEvent( MotionEvent event ){
- int action = event.getAction();
- switch( action ){
- case MotionEvent.ACTION_DOWN: {
- currentTime = millis();
- if(mover.motionLine.isEnd == true){
- mover.isMoving = false;
- mover.motionLine.clearLine();
- mover.motionLine.isEnd = false;
- }
- if(dist( event.getX(), event.getY(), mover.position.x, mover.position.y ) < 25){
- touched = true;
- mover.motionLine.add( mover.position.x, mover.position.y );
- }
- lastTime = currentTime;
- break;
- }
- case MotionEvent.ACTION_MOVE: {
- if(touched){
- currentTime = millis();
- if((currentTime - lastTime) > 25){
- mover.motionLine.add( event.getX(), event.getY() );
- lastTime = currentTime;
- }
- }
- break;
- }
- case MotionEvent.ACTION_UP: {
- if(touched){
- mover.motionLine.isEnd = true;
- mover.startMoving();
- touched = false;
- }
- break;
- }
- } // end switch
- return super.surfaceTouchEvent( event );
- } // end surfaceTouchEvent
- class MotionLine {
- ArrayList<PVector> points;
- int max; /* max number of points before reset */
- boolean isEnd;
- MotionLine(){
- isEnd = false;
- points = new ArrayList<PVector>();
- max = 50;
- }
- void display(){
- for(int i = 1; i < points.size() - 1; i++){
- PVector last = points.get( i-1 );
- PVector current = points.get( i );
- line(last.x, last.y, current.x, current.y);
- }
- } // end display
- void add( float x, float y ){
- if( points.size() == max ){
- isEnd = true;
- }else{
- points.add( new PVector(x, y) );
- }
- } // end add
- void clearLine(){
- points.clear();
- }
- void resetLine(){
- clearLine();
- isEnd = false;
- }
- } // end MotionLine
- class Mover {
- PVector position;
- PVector velocity;
- PVector target;
- float speed;
- MotionLine motionLine;
- boolean isMoving;
- int lineSize;
- int pointIndex;
- Mover(){
- position = new PVector(100, 100);
- velocity = new PVector(0, 0);
- target = new PVector(0, 0);
- speed = 2.5;
- motionLine = new MotionLine();
- isMoving = false;
- pointIndex = 1;
- }
- void display(){
- ellipse(position.x, position.y, 50, 50);
- motionLine.display();
- }
- void update(){
- if( isMoving ){
- position.add( velocity );
- if(dist( position.x, position.y, target.x, target.y ) <= speed){
- if( pointIndex == lineSize ){
- isMoving = false;
- motionLine.clearLine();
- velocity.set(0, 0, 0);
- pointIndex = 1;
- }else{
- pointIndex++;
- getNextTarget( motionLine.points.get( pointIndex ) );
- }
- }
- }
- } // end update
- void calcVelocity(){
- PVector displacement = PVector.sub( target, position );
- displacement.normalize();
- displacement.mult( speed );
- velocity = displacement;
- }
- void getNextTarget( PVector t ){
- target.set(t);
- calcVelocity();
- }
- void startMoving(){
- lineSize = motionLine.points.size() - 1;
- if(lineSize > 1){
- getNextTarget( motionLine.points.get( pointIndex ));
- isMoving = true;
- }
- }
- } // end Mover
1