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 & HelpSyntax Questions › Creating and passing around objects
Page Index Toggle Pages: 1
Creating and passing around objects (Read 416 times)
Creating and passing around objects
Aug 26th, 2009, 12:47am
 
I have a CircBody class with the following method that adds a child to a CircBody object:

Code:
  
void add_child(CircBody child) {
CircBody[] temp = new CircBody[children.length+1];
System.arraycopy(children, 0, temp, 0, children.length);
temp[children.length] = child;
children = temp;
children[children.length-1].rescale(0.1);
}


To add a child, I do for example:

Code:

body.add_child(new CircBody(-1, 100, 20));


This works. However, in order to make creation of children a little bit more simple I also added the following method to CircBody:

Code:

void add_child(float x, float y, float mass) {
add_child(new CircBody(x, y, mass));
}


So that I can do

Code:

body.add_child(-1, 100, 20);


However, this does not give exactly the same results for some reason. Before I go any further with trying to find the reason for this difference, I would like to know if I am wrong to expect both methods to give exactly the same results? Am I missing something about creating objects/passing around objects/...?

Thanks for any help.

Regards,
Dingeman

Re: Creating and passing around objects
Reply #1 - Aug 26th, 2009, 1:52am
 
It should give the same result, whenever you pass the reference to the already-created object or create it on the fly.

Maybe a float / int problem? Does the CircBody constructor expect 3 float parameters?

By the way, why don't you use the append() method?

Code:
void addChild(CircBody child) {
 children = (CirdBody[]) append(children, child);
}

Re: Creating and passing around objects
Reply #2 - Aug 26th, 2009, 2:33am
 
Thanks for your reply.

I thought I read somewhere that the append method doesn't work for arrays of objects. Well, it seems to do, so that cleans up the code. Thanks. Doesn't solve the problem however.

The constructor takes three floats as parameters. To be sure, I tried if appending all constants with '.0' changes anything, but it didn't.

Looking again at the code, I see the use of the new method is not the only thing that has changed. Previously I did something like this:

Code:
body = new CircBody(0.0, 0.0, 10.0);
body.add_child(new CircBody(-1.0, 100.0, 20.0));
...  
CircBody child = new CircBody(0.0, 0.0, 10.0);
child.add_child(new CircBody(0.0, 1.0, 50.0));
...
body.add_child(child);


So I add the children to a object before I made that object a child of some other object. Since the children are now created by their parent, I now have to do something like this:

Code:
body = new RectBody(0.0, 0.0, 10.0);
body.add_child(-1.0, 100.0, 20.0);
...  
body.add_child(0.0, 0.0, 10.0);
body.child(3).add_child(0.0, 1.0, 50.0);


So I now add the children to an object, while the object is already a child of an other object.

Could this be the problem? I have a c++ background. In c++ the new code would not work if children is a vector of CircBody objects as I would be changing the size of the objects stored in the vector (using pointers would solve this, but has some other disadvantages). I am not sure how this works in java. Am I allowed to add children to an object when that object is stored in an array?



Re: Creating and passing around objects
Reply #3 - Aug 26th, 2009, 2:41am
 
Well, both ways are OK in my opinion.

Quote:
this does not give exactly the same results


Can you explain this a little more?
Re: Creating and passing around objects
Reply #4 - Aug 26th, 2009, 3:27am
 
I knew I was a bit vague when I said that.

I have to explain a little bit what happens in the program. In my program I have rectangle  filled with circles filled with smaller circles (all different sizes given by the mass parameter). The program tries to place and scale the circles so that they are as large as possible and do not overlap. The coordinates given in the constructor are just initial values.

In the original code this works fine. However, when I do the modifications mentioned above, the children suddenly stick out of their parents and overlap. I looks like they are to big and the only solution is to let them overlap.

Now that I am writing this I think I know what the problem is. In the original code the children were already added to the object before I add the object to its parent. The parent then calls scale on this object. This scaling is the propagated down the 'generations'. In the modified code the object is first added, then scaled and after that the children are added. The children therefore never receive the scaling.

At the moment I don't have time to check if this is indeed the problem, but I am quite sure it is. I will check this evening.

Thanks for having me explain the problem.

Dingeman
Page Index Toggle Pages: 1