We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOther Libraries › Why can't I extend the Particle class!
Page Index Toggle Pages: 1
Why can't I extend the Particle class!? (Read 1305 times)
Why can't I extend the Particle class!?
Jan 10th, 2008, 9:23pm
 
Hi All,

I'm hoping someone can shed some light on this issue i've been battling almost all day.

I'm using traer.physics and I'm trying to extend the Particle class. Nothing complicated, just want to add a color property.


When I define the class this is what I have:

class myParticle extends Particle{
  color c = new color(random(255));
 
  myParticle(float m_, float x_, float y_, float z_){
    super(m_, x_, y_, z_);
  }

}

But I keep getting this error message:
Semantic Error: No applicable overload was found for a constructor with signature "Particle(float, float, float, float)" in type "Particle". Perhaps you wanted the overloaded version "Particle(float $1, traer.physics.Vector3D $2);" instead?


I looked at the documentation for traer.physics (http://www.cs.princeton.edu/~traer/physics/#ParticleSystem) and the constructor for Particle is definitely Particle(float, float, float, float)

Any ideas?

Thanks much!
Re: Why can't I extend the Particle class!?
Reply #1 - Jan 10th, 2008, 10:17pm
 
Particle doesn't have a constructor listed there.. there's a makeParticle but that's a method of the ParticleSystem class.
Re: Why can't I extend the Particle class!?
Reply #2 - Jan 10th, 2008, 10:31pm
 
If I understand the documentation there is a method of the ParticleSystem object called makeParticle(float, float, float, float). If you take a look at the library in the eclipse package explorer you will see that Particle(float m, traer.physics.Vector3D p) is the only constructor for this class. So it should looks like this:

Code:
 
class myParticle extends Particle{
color c = new color(random(255));

myParticle(float m_, float x_, float y_, float z_){
super(m_, new Vector3D(x_, y_, z_));
}
}


BUT this will have no effect to the makeParticle() function of the ParticleSystem object (the only way for you to create a new particle in the system) cause this function will still use the original Particle object. As this library isn't opensource you cant see what this function really does, to implement your own method to add your particle to the system.
Re: Why can't I extend the Particle class!?
Reply #3 - Jan 10th, 2008, 10:35pm
 
So if I did something like this with what you mentioned above, it wouldn't work?

myParticle p = physics.makeParticle()
Re: Why can't I extend the Particle class!?
Reply #4 - Jan 10th, 2008, 10:37pm
 
If you want an object you can do something like that:

Code:


class myParticle(){

Particle p;
color c;
int r;

myParticle (Particle i_p){
p = i_p;
c = new color(random(255));
r = 10;
}

void drawMe(){
fill(c);
ellipse( p.position().x(), p.position().y(), r, r );
}
}
Re: Why can't I extend the Particle class!?
Reply #5 - Jan 10th, 2008, 10:41pm
 
alecks wrote on Jan 10th, 2008, 10:35pm:
So if I did something like this with what you mentioned above, it wouldn't work

myParticle p = physics.makeParticle()


Yes that wouldn't work cause physics.makeParticle() gives you an object with the type of Particle and not of myParticle.
Re: Why can't I extend the Particle class!?
Reply #6 - Jan 10th, 2008, 10:51pm
 
oh man... you'd think that if myParticle extends Particle, you could save a Particle type in a myParticle...

your other solution is a good alternative. I will use that.

I really appreciate your help!
Re: Why can't I extend the Particle class!?
Reply #7 - Jan 12th, 2008, 5:30pm
 
It's actually the opposite, you can save a myParticle in a Particle. Since myParticle is a more specific type of Particle, the Particle interpretation of the object will just ignore all the extra bits of information and behaviors that a myParticle adds.

so...

Code:
Particle p = new myParticle(); 



...should work.
Re: Why can't I extend the Particle class!?
Reply #8 - Jan 12th, 2008, 6:00pm
 
That will work, but unfortunately I believe you need to use the particleSystem.makeParticle for the particle ot be registered with the particleSystem, and so interact with any other particle.
Re: Why can't I extend the Particle class!?
Reply #9 - Jan 12th, 2008, 7:59pm
 
Yes, the point is that you cant get a myParticle object into the ParticleSystem. I think that the main problem with non opensource libraries. You didn't know what the function makeParticle do, so you cant create an extended version of the ParticleSystem class which adds an extended myParticle object  to the system.
Re: Why can't I extend the Particle class!?
Reply #10 - Jan 21st, 2008, 1:29am
 
There is a way to do this, but it's not as clean as one might like. Traer had to create particles using a factory method in order to ensure that particles would not be created in a vacuum. The Particle class does have a constructor. It is provided by default if none is defined. More likely, he actually built a constructor and gave it only package visibility. This means the factory method can see it, but we can't.

Factory methods are a notorious pain when it comes to extending the classes they instantiate. I don't know of many ways to get around this problem. There are some patterns that handle this situation, but I am guessing Traer didn't employ any of them, or they would be documented.

So, here's what you do...

Create myParticle as a standalone class. It's data fields will be a Particle and anything else you want. The new class needs a constructor taking at least five arguments, a ParticleSystem and the four parameters required by makeParticle. The constructor first does:

    particle = thePSystem.makeParticle(#,#,#,#);

Once this is done, it can do anything else you want.

Your new class has to shadow all of the public methods of particle. Except for those you want to change, all the do is pass the call to particle.

After that you only have to deal with ParticleSystem and your new class. Things will behave differently as you will no longer explicitly use the factory method, makeParticle.
Page Index Toggle Pages: 1