We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi there! I am using an ArrayList to store some PVectors as history of the position values of some moving particles; than I would like to use vector components to draw lines connecting all the old positions in my ArrayList, in order to have trails for my particles. Sound simple but after just a bunch of particles on screen, performance drop down heavily.
As I have see much more complex stuff running smooth on processing I am wondering what is it that slow down my program so impressively. I noticed the console message: "method get() from the type PVector is deprecated". So I am most likely doing something wrong but I can't figure it out.
Any help? Suggestion? Thanks a Lot!
class Particle {
//DATA
PVector position;
PVector velocity;
PVector acceleration;
float mass;
float G; // gravitational constant
//////
int posToStore; // how many position do I want to keep track of
ArrayList <PVector> history; // an ArrayList to store my particle's prev positions
//CONSTRUCTOR
Particle(PVector v, float m) {
position = v.get();
velocity = new PVector();
acceleration = new PVector();
mass = m;
G = 1;
//////
posToStore = 400;
history = new ArrayList <PVector>();
}
//FUNCTIONALITY
void run() {
//borders();
update();
display();
}
void applyForce(PVector force) {
//PVector f = PVector.div(force, mass);
PVector f = force.get(); // making a copy of the original force passed
f.div(mass);
acceleration.add(f);
}
void update() {
velocity.add(acceleration);
position.add(velocity);
acceleration.mult(0); // resetting accelleration
velocity.limit(7); // constraining velocity
//////
if(!isFinished()){ // if there are still available positions in the array
history.add(position.get()); // pushing a new PVector into my position's history arraylList
} else {
history.remove(0);
}
}
void display() {
stroke(255);
//fill(175);
println(history.size());
for(int i = history.size()-1; i > 1; i--){
//println(history[i].x);
PVector oldhpos = history.get(i-1);
line(history.get(i).x, history.get(i).y, oldhpos.x, oldhpos.y);
}
stroke(255,0,0);
ellipse(position.x, position.y, 40*mass, 40*mass);
}
// attraction force defined for every particle as a function which return a PVector
PVector attract(Particle p) {
PVector force = PVector.sub(position, p.position); // Calculate direction of force
float d = force.mag(); // distance btw objects
d = constrain(d, 5.0, 25.0);
force.normalize(); // now force just gives me the direction of the vector
float strength = (G * mass * p.mass) / (d * d); // Calculate gravitional force magnitude
force.mult(strength); // Get force vector --> direction * magnitude
return force;
}
boolean isFinished(){
if(history.size() > posToStore){
return true;
} else {
return false;
}
}
}
Answers
Just ignore it! :-j But if you can't bear the red sight of it, replace it w/ copy(). :P
http://studio.ProcessingTogether.com/sp/pad/export/ro.9s0026dE7B8v-
http://studio.ProcessingTogether.com/sp/pad/export/ro.989GaZC5t7EkE
Thank you for the tip GoToLoop! I have a long learning way in front of me ;)
What do you mean > Field history doesn't belong to class Particle since it was declared at the top of your sketch. ?
ArrayList history; It is indeed declared inside my Particle class.. So I ma accessing something declared within the class... No?
Oops, my bad! Somehow it's escaped moi! X_X
class
, it's intended to represent 1 entity only.what I am trying to achieve is to keep track of the history of my particle's prev position in the ArrayList, then use the position to draw lines btw them in the following loop (it works, but extremely slow):
Those trails are similar to these 2 online examples:
But if you're looking for a more adequate container, how about an ArrayDeque as a Queue?:
Gonna re-post a very good example for it from this old forum thread: O:-)
https://forum.Processing.org/two/discussion/2829/fifo-queue-problem-with-code#Item_1
GULP! this code if far too advanced for my poor coding skills (I confess It is the first time I see "final" keyword ;) ) But I will dig into it hoping to find answers and improve my toolset! Thank you very very much for your help! ^:)^
It is to me as well. I'm not its author btW. :-\"
The important thing is to observe how the PVector objects are recycled back into the Queue.
That is, once the size() reaches MAX, no more PVector objects are ever created. *-:)
See how variable np receives the PVector remove() from the ArrayDeque points container if
fc > MAX
.Thanks! ~O)
https://Processing.org/reference/final.html
Interesting! The more I learn, the more there is to learn ;) Have a great day!