r/processing • • Oct 25 '20

Grid distortion

174 Upvotes

7 comments sorted by

9

u/020406code Oct 25 '20

4

u/uhrguhrguhrg Oct 25 '20 edited Oct 25 '20

Great stuff.

A couple of small notes:

Firstly, when working with already existing PVectors instead of doing a = b.copy() you can do a.set(b), which may make some code slightly cleaner. For example n.pos = n.origin.copy().add(force); can be written as n.pos.set(n.origin).add(force); This also has the benefit of not creating a new unnecessary PVector instance.

Secondly, you could slightly optimise the force calculation by doing

    float magSq = force.magSq();
    return force.mult(maxMag * 400.0 / magSq);

This way you avoid doing the sqrt calculation twice (once for normalisation and once for getting the magnitude), when you don't need to do it at all.

Thirdly, doing force.setMag(0); also isn't very efficient since it's forced to do a magnitude calculation first. You can do force.set(0,0) instead.

2

u/020406code Oct 25 '20

Thanks for the tips.

4

u/Salanmander Oct 25 '20

Looks cool! One thing I'd like to note is that took me a really long time to understand your calcForce method, since you have a variable called "force" in it that is inversely related to the final answer...I kept trying to figure out how "force" got bigger as the objects got closer together.

1

u/al_mitra Oct 25 '20

Thanks a lot for sharing the code. I'd absolutely love to learn this one

2

u/[deleted] Oct 25 '20

I wanted to re-create this on python for my electromagnetics class lols

1

u/GoldenKela Oct 25 '20

wonder if theres a way to optimize, right now I can only think of 3 loops