Connecting grid0 with grid1.
in
Contributed Library Questions
•
7 months ago
Hi,
What I am trying to do is to connect every grid0 particle with every grid1 particle.
For now, I managed to connect only one particle from grid0 with grid1.
Could you help me please?
I think, I need to get into VParticleGrid class, function "createGrid", in double for() loop in order to access particles from a grid.
Current place of the connection:
- VParticleGrid grid0 = new VParticleGrid(physics, amountX, amountY, strength, a0, b0, c0, d0);
- VParticleGrid grid1 = new VParticleGrid(physics, amountX, amountY, strength, a1, b1, c1, d1);
- VParticle p0 = grid0.above;
- VParticle p1 = grid1.above;
- VSpring s = new VSpring(p0, p1, p0.sub(p1).mag(), strength/10);
- physics.addSpring(s);
Whole script:
- import peasy.PeasyCam;
- import punktiert.math.Vec;
- import punktiert.physics.*;
- // physics system for the mesh
- VPhysicsSimple physics;
- // physics for the constraints
- VPhysicsSimple physicsConstraints;
- PeasyCam cam;
- boolean pause = true;
- public void setup() {
- size(1280, 720, P3D);
- cam = new PeasyCam(this, 600);
- physics = new VPhysicsSimple();
- BConstantForce force = new BConstantForce(0, 0, .1f);
- physics.addBehavior(force);
- physicsConstraints = new VPhysicsSimple();
- physicsConstraints.addBehavior(new BAttraction(new Vec(), 1000, .2f));
- // create a mesh
- float amountX = 5;
- float amountY = 40;
- float strength = 1f;
- float offset = 1 * 50;
- // lock all the Constraints (otherwise the springforce will alter the
- // position
- VParticle a0 = new VParticle(-width * .5f, -height * .5f-0, 0).lock();
- VParticle b0 = new VParticle(width * .5f, -height * .5f-0, 0).lock();
- VParticle c0 = new VParticle(width * .5f, height * .5f+0, 0).lock();
- VParticle d0 = new VParticle(-width * .5f, height * .5f+0, 0).lock();
- VParticle a1 = new VParticle(-width * .5f, -height * .5f-offset, offset).lock();
- VParticle b1 = new VParticle(width * .5f, -height * .5f-offset, offset).lock();
- VParticle c1 = new VParticle(width * .5f, height * .5f+offset, offset).lock();
- VParticle d1 = new VParticle(-width * .5f, height * .5f+offset, offset).lock();
- // add the Particles as Constraints to the mesh physics
- physics.addConstraint(a0);
- physics.addConstraint(b0);
- physics.addConstraint(c0);
- physics.addConstraint(d0);
- physics.addConstraint(a1);
- physics.addConstraint(b1);
- physics.addConstraint(c1);
- physics.addConstraint(d1);
- //VParticleGrid(VPhysics physics, float amountX, float amountY, float strength, Vec a, Vec b, Vec c, Vec d)
- VParticleGrid grid0 = new VParticleGrid(physics, amountX, amountY, strength, a0, b0, c0, d0);
- VParticleGrid grid1 = new VParticleGrid(physics, amountX, amountY, strength, a1, b1, c1, d1);
- VParticle p0 = grid0.above;
- VParticle p1 = grid1.above;
- VSpring s = new VSpring(p0, p1, p0.sub(p1).mag(), strength/10);
- physics.addSpring(s);
- }
- public void draw() {
- background(240);
- if (!pause) {
- physics.update();
- for (VParticle c : physicsConstraints.particles) {
- c.unlock();
- }
- physicsConstraints.update();
- for (VParticle c : physicsConstraints.particles) {
- c.lock();
- }
- }
- stroke(0, 0, 200);
- noFill();
- strokeWeight(4);
- for (VParticle p : physics.constraints) {
- //ellipse(p.x, p.y, p.radius*2, p.radius*2);
- point(p.x, p.y, p.z);
- }
- stroke(100);
- strokeWeight(1);
- for (VSpring s : physics.springs) {
- line(s.a.x,s.a.y,s.a.z,s.b.x,s.b.y,s.b.z);
- }
- }
- public void keyPressed() {
- if (key == ' ')
- pause = !pause;
- }
- class VParticleGrid {
- VPhysicsSimple physics;
- ArrayList<VParticle> particles;
- Vec a, b, c, d;
- VParticle previous;
- VParticle above;
- VParticle p;
- VParticleGrid(VPhysicsSimple physics, float amountX, float amountY, float strength, Vec a, Vec b, Vec c, Vec d) {
- this.physics = physics;
- this.a = a;
- this.b = b;
- this.c = c;
- this.d = d;
- particles = new ArrayList<VParticle>();
- createGrid(amountX, amountY, strength);
- }
- void createGrid(float amountX, float amountY, float strength) {
- for (int i = 0; i <= amountY; i++) {
- Vec a0 = a.interpolateTo(d, i / amountY);
- Vec b0 = b.interpolateTo(c, i / amountY);
- for (int j = 0; j <= amountX; j++) {
- Vec pos = a0.interpolateTo(b0, j / amountX);
- p = null;
- if (random(1) < .0f) { // movers
- p = new VParticle(pos, 1, random(10, 30)).lock();
- p.addBehavior(new BCollision());
- physics.addConstraint(p);
- physicsConstraints.addParticle(p);
- }
- else {
- p = physics.addParticle(new VParticle(pos));
- }
- particles.add(p);
- if (j > 0) {
- previous = physics.getParticle(particles.get(particles.size() - 2));
- VSpring s = new VSpring(p, previous, p.sub(previous).mag(), strength);
- physics.addSpring(s);
- }
- if (i > 0) {
- above = physics.getParticle(particles.get(particles.size() - (int) amountX - 2));
- VSpring s = new VSpring(p, above, p.sub(above).mag(), strength/10);
- physics.addSpring(s);
- }
- }
- }
- }
- }
1