Hi,
Basically I want to move an ellipse between two points.
This is the code which I have written:
Path_Tracing.pde
Particle.pde
The problem with this code is its not smooth and accurate if you closely see the moving ellipse it tends to wiggle up and down. Moreover, with large values of mVel it doesn't follow the path that closely.
Removing the delay in the Path_Tracing.pde works as the wiggle is unnoticeable but I want to keep the delay.
Thanks in advance!
Basically I want to move an ellipse between two points.
This is the code which I have written:
Path_Tracing.pde
- PVector point1 = new PVector(200, 300);
- PVector point2 = new PVector(700, 500);
- Particle p;
- void setup()
- {
- size(800, 600);
- smooth();
- p = new Particle(new PVector(point1.x, point1.y));
- }
- void draw()
- {
- background(50);
- stroke(0);
- strokeWeight(5);
- line(point1.x, point1.y, point2.x, point2.y);
- stroke(150);
- fill(255);
- ellipse(point1.x, point1.y, 20, 20);
- ellipse(point2.x, point2.y, 20, 20);
- p.move(point1, point2);
- delay(100);
- }
Particle.pde
- public class Particle
- {
- PVector mLoc;
- PVector mDir;
- float mVel;
- float mRadius;
- // movement variables
- PVector currNode;
- PVector nextNode;
- Particle(PVector loc)
- {
- mLoc = loc;
- mDir = new PVector(random(1), random(1));
- mVel = random(5);
- mRadius = 10.0;
- }
- void draw()
- {
- stroke(240, 150, 0);
- strokeWeight(1);
- fill(255, 255, 0);
- ellipse(mLoc.x, mLoc.y, mRadius, mRadius);
- }
- void move(PVector p1, PVector p2)
- {
- this.update();
- PVector pTemp = new PVector(p2.x, p2.y);
- pTemp.sub(p1);
- pTemp.normalize();
- mDir = pTemp;
- this.draw();
- }
- void update()
- {
- PVector tempDir = new PVector(mDir.x, mDir.y);
- tempDir.mult(mVel);
- mLoc.add(tempDir);
- }
- }
The problem with this code is its not smooth and accurate if you closely see the moving ellipse it tends to wiggle up and down. Moreover, with large values of mVel it doesn't follow the path that closely.
Removing the delay in the Path_Tracing.pde works as the wiggle is unnoticeable but I want to keep the delay.
Thanks in advance!
1