We are about to switch to a new forum software. Until then we have removed the registration on this forum.
hello i have taken the standar particle system with the texture,and i want to give the location of the mouse in draw function rather inside the class because i want to connect it with kinect,tried it but the result was strange,i tried to move the acceleration and the velocity,the result is better but it is still strange.i tried to move also the the lifespan parameter but it is also strange. Im thinking that is something with the add particle function and the iterator,but im not very good with tha concept,to figure out the problem.I will be very thankful if you can help me.Thank you in advance
here is the code:
import java.util.Iterator;
ParticleSystem ps;
PVector location;
PVector acceleration;
PVector velocity;
float lifesp = 255;
float _lifesp = 2.0;
void setup(){
size(640,360,P2D);
PImage image = loadImage("texture.png");
ps = new ParticleSystem(image);
}
void draw(){
background(0);
//ps.applyForce();
acceleration = new PVector(0, 0.05);
velocity = new PVector(random(-1, 1), random(-2, 0));
PVector location = new PVector (mouseX,mouseY);
lifesp -= _lifesp;
ps.run(location, acceleration, velocity,lifesp );
ps.addParticle();
}
the particle class:
class Particle {
PVector loc;
PVector location;
float lifespan;
PImage image;
Particle(PImage img) {
image = img;
}
void run(PVector location ,PVector _acceleration, PVector _velocity,float _lifespan) {
update(location,_acceleration,_velocity);
display(location,_lifespan);
}
void update(PVector _location, PVector _acceleration, PVector _velocity) {
velocity.add(_acceleration);
_location.add(_velocity);
}
void display(PVector _location, float _lifespan) {
stroke(0, lifespan);
imageMode(CENTER);
tint(255,lifespan);
image(image,_location.x,_location.y);
//fill(175, lifespan);
//ellipse(location.x, location.y, 8, 8);
}
boolean isDead(float _lifespan) {
lifespan = _lifespan;
if (_lifespan < 0.0) {
return true;
} else {
return false;
}
}
}
and the particle system class:
class ParticleSystem{
ArrayList<Particle> particles;
PVector origin;
//PVector _loc;
PImage image;
ParticleSystem(PImage _img){
particles = new ArrayList<Particle>();
image = _img;
}
void addParticle(){
particles.add(new Particle(image));
}
void run(PVector location, PVector acceleration, PVector velocity,float _lifespan){
Iterator<Particle> it = particles.iterator();
while (it.hasNext()){
Particle p = it.next();
p.run(location,acceleration,velocity, _lifespan);
if(p.isDead(_lifespan)){
println("khgkhk");
it.remove();
}
}
}
}`
Answers
Correct me if I'm wrong, but I understand that you want to:
But you are having trouble because the information you receive from the kinekt is not usable in a class.
Is this correct? If so, wouldn't it be easier to pass the received information on to another var and then accessing that from within the class? Instead of rewriting the whole sketch? And it's usually a good idea to keep all the variables the class needs inside the class.
Is it possible to see the original sketch? That way it's easier to see what you've done and perhaps where something went wrong?
i need to take the location of the particles from draw function,because there i can call the kinect calibration,now i m working with mouseX mouseY but if i find this i could make the connection,except if i import the library in the class and instantiate the kinect object and call all the functions inside the class,i tried it but it seemed more complicated
here is the original
the particle class:
the particle system class:
Ok, so mouseX replaces the x-coordinate you receive from the kinekt. And let's say you can't access that information from within the class, or that it's too complicated. What you can do is to make a new variable called xMouse. We declare it before setup and in draw we say xMouse = mouseX;
The information received is now stored in a global variable that is no different from any other variable. And we do the same with mouseY
In the class we instantiate the location PVector by accessing the global variable xMouse like this:
location = new PVector(xMouse, yMouse)
;the global variable that i declare before the setup can be accessed from everywhere?also inside the class panel?is any difference if the classes are in seperate panels or in the same?the vector of the kinect is declared inside an if statement. the vector of the kinect from the right hand is the location of the ellipse
A global variable is available from anywhere. No matter you are calling it from a class or a function or somewhere else.
When you declare a variable inside a class it is private. It is only usable by the class unless you specify that it's global. And if you declare a variable in a function, it's only available in that function. That's why cars usually are declared before setup().
I have never worked with kinekt, but from what I can understand, you problem will be solved if you do this:
But I might be wrong. If this doesn't work @Chrisir might be of better help, or maybe @GoToLoop has some neat solution.
ok thank you i will try it
it gives null pointer exception.what about .get()?
and i put print("location "+location) and gives location null.the get() fuction doesnt work
try
If both location & convertedRightHand are PVector objects, in order to assign the latter to the former, it's easier to just call method set() rather than individually assign their fields to each other:
location.set(convertedRightHand);
https://Processing.org/reference/PVector_set_.html
I am not sure what is his Problem anyway
my problem is that i want the declared convertedRightHand Pvector inside the if statement in draw to be called in the location PVector of the particle class,so to make the kinect move the particles
And does what I posted work?
I dint try it yet,i m doing an other job now,but should i declare it inside particle class?The processing sketch works until start tracking the skeleton,but then prints location null and then null pointer exception.I will try both tours and GoToLoop solutions
Neither worked.here is the sketch with the particle system inside kinect:
is it possible?If i pass it as an argument to particle system class and then to particle class?
except maybe i pass it in ps.run() function as argument
yes.
these are the original lines in draw()
now just modify them like
i have to pass all the arguments?
I tried it and it gives strange results
here i disable the kinect PVector and i put mouseX,mouseY
The particle system:
The particle class:
class Particle {
I have processing 2.21, i dont know where is the problem
this version works and is totally without the kinect
Java language introduced the "enhanced"
for ( : )
in its version 5 at year 2004, 12 years ago! @-)Still folks rely on Iterator, a more complicated technique for traversing containers! :-O
It's true Iterator allows us to remove() within a loop. That's a legit use.
But for List containers, the same can be accomplished w/ a backwards
for ( ; ; )
loop too. *-:)Another advantage at avoiding Iterator is keeping compatibility w/ Pjs for web deployment. \m/
i dont still unsterstand much of the iterator,Chrisir do you know what i have done wrong?
it is doing the same effect chrisir.This has complete different effect.The original particle system is this:
//The Particle class
//the particle system:
sorry for disturbance,im trying to find how to solve it,because it will give my a lot of advance in the latest sketch with kinect
and GoToLoop can you be more specific?
I believe I was clear I was talking about Iterator usage only? ^#(^
do you think that the iterator makes the problem,should i use an array and for( , ,) loop?
No problems. It's just some relic from the past. ~O)
ok,thank you,but can you see any problem in the original code and the code that a give the parameters inside draw.The original particle system is taken from Nature of code,maybe there is also in the examples of processing,or here
because i dont know what else to do
and this is the effect that now does:
what should it do?
do you know what is the problem?
if you see the first video the effect is different
the first video is like particles the second is like lines
i took out the image stuff (because i didn't have one) and what was left looks fine
if you NEED the image, and if it's the same for every particle, then just make it a global variable - much less fuss.
the concept is to instatiate the parameteres in draw function not in particle class ,so to connect it with kinect.i know that it works.i doesnt work good,or better with different effect if i take the parameters in draw function :P
that is my problerm,when i give the parameters to draw rather than declare them in particle class the effect is completely different
Re-tweaked the sketch again in order to use backwards loop in place of Iterator.
Now it can be deployed only via Pjs library (JS Mode). Check it out: :-bd
http://studio.ProcessingTogether.com/sp/pad/export/ro.9tixqv9DytSpX
phaidon, essentially, you want to join two sketches:
a particle system that runs on mouse position (A)
a kinect system that gives you values (B).
Make sure A and B work separately.
Now in the joined sketch (C) make sure both parts work independently and C runs.
Now, we want to replace the mouse values with the kinect values and display the particle system. Thus make a global variable PVector globalLocation;
Give it values from the kinect.
Use it where you had mouseX and mouseY in the old sketch.
please hit ctrl-t in processing to auto-format your sketch in processing.
please post the entire sketch C as it is now.
Thank you.
Best, Chrisir ;-)
you mean like this?
Chrisir all the sketches work,the only problem is when i put the parameters in draw function the effect completely different from the original sketch.i will try both solutions,and i will let you know.
koog your solution works it has to do something with the add.particle function :P
i will try also removing the iterator.