hype / HYPE_Processing

HYPE for Processing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

HVelocity : Particles Accelerating Even after Reaching Destination

aswinmohanme opened this issue · comments

Problem:
I have the following code to render a text of Bubbles on to the screen. The bubbles should move from a random location on the screen to their respective positions.

Here is the Code

import hype.*;
import hype.extended.layout.*;
import hype.extended.colorist.*;
import hype.extended.behavior.*;

HDrawablePool pool;
PVector[] finalLoc;

PFont fnt;
HText txt;
HDrawable[] drawablePool = new HDrawable[800];
HVelocity vel[] = new HVelocity[800];

HTimer timer;
void setup() {
  size(800, 600);
  H.init(this);

  fnt = createFont("Roboto",64);
  txt = new HText("ISQUIP", 200, fnt);
   H.add(txt)
    .anchorAt(H.CENTER)
    .locAt(H.CENTER)
    .noStroke()
    .noFill()
  ;
  HShapeLayout lay = new HShapeLayout().target(txt);

  finalLoc = new PVector[800];
  for(int i=0; i < 800; ++i){
    finalLoc[i] = lay.getNextPoint();
  }

  pool = new HDrawablePool(800);
  pool.autoAddToStage()
    .add(
      new HEllipse()
    )
    .layout(new HShapeLayout().target(new HRect().size(width, height)))
    .colorist(new HColorPool(#e74c3c, #f1c40f, #2980b9, #27ae60, #16a085, #8e44ad).fillOnly())
    .onCreate(
      new HCallback() {
        public void run(Object obj) {
          HEllipse e = (HEllipse) obj;
          e.size(random(2,10))
            .noStroke()
          ;
        }
      }
    )
  ;

  for(int i=0; i < 800; ++i){
    vel[i] = new HVelocity();
    drawablePool[i] = pool.request();
    vel[i].target(drawablePool[i]).launchTo(finalLoc[i].x, finalLoc[i].y,60);
  }
}

void draw() {
  H.drawStage();
}

The problem is that the particles are not slowing down after reaching their positions, and they accelarate out of the screen.

How do I fix this ?

Use HTween instead....something like this:

import hype.*;
import hype.extended.layout.*;
import hype.extended.colorist.*;
import hype.extended.behavior.*;

HDrawablePool pool;
PVector[] finalLoc;

PFont fnt;
HText txt;
HDrawable[] drawablePool = new HDrawable[800];
HTween tween;

HTimer timer;
void setup() {
  size(800, 600);
  H.init(this);

  fnt = createFont("Roboto",64);
  txt = new HText("ISQUIP", 200, fnt);
   H.add(txt)
    .anchorAt(H.CENTER)
    .locAt(H.CENTER)
    .noStroke()
    .noFill()
  ;
  HShapeLayout lay = new HShapeLayout().target(txt);

  finalLoc = new PVector[800];
  for(int i=0; i < 800; ++i){
    finalLoc[i] = lay.getNextPoint();
  }

  pool = new HDrawablePool(800);
  pool.autoAddToStage()
    .add(
      new HEllipse()
    )
    .layout(new HShapeLayout().target(new HRect().size(width, height)))
    .colorist(new HColorPool(#e74c3c, #f1c40f, #2980b9, #27ae60, #16a085, #8e44ad).fillOnly())
    .onCreate(
      new HCallback() {
        public void run(Object obj) {
          HEllipse e = (HEllipse) obj;
          e.size(random(2,10))
            .noStroke()
          ;
        }
      }
    )
  ;

  for(int i=0; i < 800; ++i){
    drawablePool[i] = pool.request();
     tween = new HTween()
    .target(drawablePool[i]).property(H.LOCATION)
    .start( drawablePool[i].x(), drawablePool[i].y() )
    .end( finalLoc[i].x, finalLoc[i].y )
    .ease(0.1)
    .spring(0.2) 
  ;
    
  }
}

void draw() {
  H.drawStage();
}

Thanks, works perfectly