A question about showing moving direction.
in
Contributed Library Questions
•
9 months ago
Good day,
Maybe anyone knows how can I create a line from the center of any particle (line lenght = particle radius) , which would show direction in which the particle would be moving? I tried working with Vec2D, but got stuck while trying to create an average vector.
Here is the complete script:
Maybe anyone knows how can I create a line from the center of any particle (line lenght = particle radius) , which would show direction in which the particle would be moving? I tried working with Vec2D, but got stuck while trying to create an average vector.
Here is the complete script:
- import toxi.physics2d.behaviors.*;
- import toxi.physics2d.*;
- import toxi.geom.*;
- import toxi.math.*;
- ArrayList <Particle> particles;
- ArrayList <Attractor> attractors = new ArrayList <Attractor>();
- //Attractor attractor2;
- int gridDim = 10;//30
- VerletPhysics2D physics;
- void setup() {
- size(1000, 600);
- smooth();
- int w = width/gridDim;
- int h = height/gridDim;
- //INITIALIZE PHYSICS
- physics = new VerletPhysics2D();
- physics.setDrag(0.1); //JUDEJIMO LAISVUMAS
- //INITIALIZE PARTICLE CLASS
- particles = new ArrayList<Particle>(); // PO SITO DEDAMI DOUBLE FOR LOOP GRIDUI
- for (int y = 0; y<gridDim; y++) {
- for (int x = 0; x<gridDim; x++) {
- particles.add(new Particle(new Vec2D( (x+0.5)*w, (y+0.5)*h )));
- }
- }
- //INITIALIZE ATTRACTOR CLASS
- attractors.add( new Attractor(new Vec2D(200, 200)));
- attractors.add( new Attractor(new Vec2D(900, 500)));
- }
- void draw() {
- background(255);
- //UPDATE PHYSICS
- physics.update();
- //ATTRACTION
- for (Attractor a: attractors){
- a.display();
- for (Particle p: particles) {
- p.display();
- }
- a.lock();//JEI UNLOCK ATTRACTOR POINTAS JUDES KARTU
- }
- }
- class Attractor extends VerletParticle2D {
- float r;
- Attractor(Vec2D loc) {
- super(loc);
- r = 24;
- physics.addParticle(this);
- physics.addBehavior(new AttractionBehavior(this,300, 0.1));
- }
- void display () {
- fill(100);
- ellipse (x, y, r*2, r*2);
- }
- }
- class Connection extends VerletSpring2D {
- Connection(Particle p1, Particle p2, float len, float strength) {
- super(p1, p2, len, strength);
- }
- void display(){
- stroke(0);
- line(a.x,a.y,b.x,b.y);
- }
- }
- class Particle extends VerletParticle2D {
- float r;
- Particle(Vec2D loc) {
- super(loc);
- r=10;
- physics.addParticle(this);
- physics.addBehavior(new AttractionBehavior(this, r*4, -1));
- }
- void display(){
- fill(255);
- stroke(2);
- ellipse(x,y,r*2,r*2);
- }
- }
1