We are about to switch to a new forum software. Until then we have removed the registration on this forum.

- All Categories 25.7K
- Announcements & Guidelines 13
- Common Questions 30
- Using Processing 22.1K
- Programming Questions 12.2K
- Questions about Code 6.4K
- How To... 4.2K
- Hello Processing 72
- GLSL / Shaders 292
- Library Questions 4K
- Hardware, Integration & Other Languages 2.7K
- Kinect 668
- Arduino 1K
- Raspberry PI 188
- Questions about Modes 2K
- Android Mode 1.3K
- JavaScript Mode 413
- Python Mode 205
- Questions about Tools 100
- Espanol 5
- Developing Processing 548
- Create & Announce Libraries 211
- Create & Announce Modes 19
- Create & Announce Tools 29
- Summer of Code 2018 93
- Rails Girls Summer of Code 2017 3
- Summer of Code 2017 49
- Summer of Code 2016 4
- Summer of Code 2015 40
- Summer of Code 2014 22
- p5.js 1.6K
- p5.js Programming Questions 947
- p5.js Library Questions 315
- p5.js Development Questions 31
- General 1.4K
- Events & Opportunities 289
- General Discussion 365

## Answers

Develop your sketch one step at a time. First, a simple program.

Doesn't get much more simple. And we can be sure that that works.

Now we need to add ellipses that spawn randomly. But we don't just want to draw them. We want to remember where they are (because we'll need to know their positions later, and change them too, right?). Let's start with just one of them.

Great. We now have a randomly placed ellipse.

Now this is an important step. We make our ellipse into its own object.

Notice that it works the same as before. So why bother with this step?

This is why. It's so we can make many copies of that one dot object easily, without needing to make different arrays for each variable that each dot needs to remember.

Also notice that we added and id field for each dot, so each dot knows which numbered dot itself is. This will be handy later.

See? In this step we add a color to each dot, and we do it by adding a new color variable to our Dot object (instead of having to add a whole separate array of colors to track this). We also break the draw() method into simulate() and render() steps.

Before we can even start to apply different forced to each ellipse, we need to make them move. That comes next.

you made a custom class then an array list for it, then functions. very systematic and educative.

right now i cant totally grasp arrays and classes but i ll study your entries again tomorrow, with re writing your code and checking references .

thanks so much and please continue :)

So what's involved with adding movement? Not much! Instead of doing a lot of crazy looping over many arrays, we only need to add the new behavior to our Dot class. We add a velocity vector, make it point in a random direction, and then add it to that dot's position vector. And now all the dots move!

We have movement! Now we just need to make the movement based on the positions of other ellipses instead of just being random.

this is creating dots =>

`for (int i=0; i<200; dots.add (new Dot (i++)));`

but what is this doing?=> (specially, what does ".size" mean?)

`for (int i=0; i<dots.size (); dots.get(i++).draw());`

Remove the initial random velocity, and add some logic to loop over all the dots, setting their accelerations, and...

Magic.

You need to use .size() when working with ArrayLists, because the ArrayList can store objects of different types. It is similar to Array.length, which tells you how many objects are in the Array. It returns how may objects are stored in the ArrayList.

can you briefly tell the physics logic behind it? what is happening here :)

For every dot in the ArrayList from myself onwards,

If that dot is not me,

Find a vector, t, that is the difference from that dot's position and my position. This is the first step towards finding how much force that Dot pulls on me.

Find the distance between that other dot and myself.

Divide the vector found by the distance twice, so that the force between us is proportional to the square of the distance between us (like real gravity!).

Add this force to my acceleration.

Add the negative of that force to the other dot's acceleration.

Notice that each dot's acceleration vector is zeroed-out after the position is updated in simulate().

Also notice that since the "other dot" always appears later in the Array List, that "other dot" won't re-add this force again, as it will only add more forces to itself and dots that come after it.

I lied. The first t.div(dis); normalizes the force. The second one makes it proportional to the distance. If you add a THIRD t.div(dis);, then THAT'S like real gravity.

i tried adding the third t.div(dis), its running normally, looking like gravity but the motion is too slow, what could be the reason?

No gravitational constant is being used. Well, it is, but it's implied to be 1.

For bonus points, make masses with different sizes and make more massive ellipses attract more!

F = G * m * M / d / d

Hi TFGuy44, I have been doing this in java. I would like to use the gravitational constant which I set to 6.673e-11. I tried using the force formula as double strength = (G * mass * particles.get(i).getMass()) / (dis * dis); But I am unsure as to what I should apply this to and how in the set_acceleration function.

Mine so far looks like this in java:

`public void setAcceleration() { for(int i = id; i < particles.size(); i++) { if(i != id){ Vector vec = new Vector(particles.get(i).getPos().getX() - pos.getX() ,particles.get(i).getPos().getY() - pos.getY()); double dis = Vector.distance(pos, particles.get(i).getPos()); double force = (G * mass * particles.get(i).getMass()) / (dis * dis); vec.divide(dis); vec.divide(dis); vec.divide(dis); acc.add(vec); vec.multiply(-1); particles.get(i).getAcceleration().add(vec); } } }`

First, let's clarify something: The "acceleration" this is creating is NOT acceleration - it's the resulting FORCE that should be applied to this Dot based on all the other Dots. Since my original dots all had the same mass of "one mass", F = ma, or F = a if m is 1.

But no more! Let's have dots with mass!

This looks about right, if you ask me. Large planets can slingshot smaller ones off at great speeds if they get close to the center. Of course, with real masses, this would be a whole lot more collision-y. (Bonus points: Change the code so masses that collide stuck to each other!)

You may also wonder why I kept G at 1, instead of 0.00000000006673.

This is because my masses range from 3 to 100, which are much, much smaller than "planetary" masses that you would be using in Physics. Basically, the "large" value I use for G cancels our the "small" value I use for planet masses.

TfGuy44 it works! I had no idea that formula for the force had to be applied to the new vector in that way! I've never had a good physics background but I do love astronomy and I've been wanting to create some kind of solar system simulation, and I am definitely aiming at the next step of having the particles combine! But this gravity simulating part had been kicking my butt! I tried using the G constant and you were right it was making objects barely move because the mass values I've been using were too small. Thank you so much!

I've been thinking about which approach I want to take for combining the particles/dots.

First and simpler approach which I did first is to each dot to check if its thin the area of another dot, then the more massive dot gets the mass of the smaller dot added to it and I remove the smaller dot from the arrayList. In turn I made the dot's size depend on its mass.

A second approach I find more interesting to think about but a bit more complex, that would be to not get rid of the smaller dot but to keep it attached to the more massive one, maybe by setting the smaller dot's velocity to zero upon impact and then set its velocity to the bigger dot's?

I am sure in a more realistic simulation I would have to take account for other forces like inertia for example. Any thoughts on this?