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 › Reference an objects variable
Pages: 1 2 
Reference an objects variable (Read 1435 times)
Reference an objects variable
Mar 1st, 2010, 8:51am
 
for example

say i have an arraylist of "balls", each ball has a the variable x,y and the diameter and these are all random

  Code:
Ball(float _x, float _y, float _sX, float _sY) { 



how can i found out say the x coordinate of one of the objects?

so how would i find out the value of _x or _y etc if i have not inputed it myself and there are multiple balls.

anyone understand what im saying

cheers
Re: Reference an objects variable
Reply #1 - Mar 1st, 2010, 8:55am
 
xcoord = (Ball)balls.get(i).x;

or you might have to put brackets around the first bit....

((Ball)balls.get(i)).x  ... see which one works...
Re: Reference an objects variable
Reply #2 - Mar 1st, 2010, 9:02am
 
thanks, sorry could you explain the syntax a bit more please
Re: Reference an objects variable
Reply #3 - Mar 1st, 2010, 9:10am
 
at the moment im just trying to print it

println((Circle)balls.get(2).x)


my class is called Circle, array list is called balls

i get the error

x cannot be resolved or is not a field

have also tried using _x and putting another set of brackets around it
Re: Reference an objects variable
Reply #4 - Mar 1st, 2010, 9:11am
 
balls.get(i)
Get the ith element from the arrayList.

(Ball)
Cast this to a Ball object, since arrayLists don't know the type of what they are storing.

.x
Get the data member x from the resulting Ball object.

xcoord =
And store it into this variable.
Re: Reference an objects variable
Reply #5 - Mar 1st, 2010, 9:11am
 
If i is the index number in the arraylist of the ball you want to look at, then balls.get(i) is how you get the ith value of the arraylist. But when the list is a list of objects, not primitives, you have to say what you are getting out of there by "casting" to a Ball object.
Hence (Ball)balls.get(i).
You then want to get the x value of this.... so you have:

((Ball)balls.get(i)).x

You can break this down into more steps if you want.....

Ball selectedball;
selectedball = (Ball)balls.get(i);
xcoord = selectedball.x;

I'm just trying to condense it down into one line in the original formulation..... hope this helps.  
Re: Reference an objects variable
Reply #6 - Mar 1st, 2010, 9:13am
 
try the extra brackets:

println(((Circle)balls.get(2)).x)

As you need to do the cast to Circle and THEN get the x value, not the other way around....
Re: Reference an objects variable
Reply #7 - Mar 1st, 2010, 9:14am
 
Make sure that x and y are parameters of your class :

Code:
class Ball {
 
 // parameters :
 float x, y;
 
 // constructor :
 Ball(float _x, float _y) {
   this.x = _x;
   this.y = _y;
 }

}
Re: Reference an objects variable
Reply #8 - Mar 1st, 2010, 9:23am
 
Giles wrote on Mar 1st, 2010, 9:13am:
try the extra brackets:

println(((Circle)balls.get(2)).x)

As you need to do the cast to Circle and THEN get the x value, not the other way around....


cool working good cheers man and everyone else
Re: Reference an objects variable
Reply #9 - Mar 1st, 2010, 9:34am
 
Great....

One thing I learned (from Martin Schneider on openprocessing) was to create a helper method:

Circle getball(int i)
{
 return (Circle)balls.get(i);
}

which means you can then just use getball(i).x  and so on.... saves typing all that stuff every time.
Re: Reference an objects variable
Reply #10 - Mar 1st, 2010, 9:41am
 
yeah basically im trying to get to grips with object orientated programming in processing,

so far i have a lot of balls bouncing around, now i want to make them bounce off each other aswell,

my plan is to check each ball to see if they are touching each other and then reverse the balls that touch, speed

can you tell specific object to carry out a function so something like

ball(i).reverseDirection

where i is the balls index in the array list.

this is my code that ive just written in the last 5 minutes

Code:
vvoid checkCollision(){
 
 for (int i = balls.size()-1; i >=0 ;i--){
   
   float CollisionCheckX = (((Circle)balls.get(i)).x);
   float CollisionCheckY = (((Circle)balls.get(i)).y);
   float BallDiameter = (((Circle)balls.get(i)).diameter);
   
   for (int j = balls.size()-1;  j >=0 ;j--) {
     
     float CollisionCheckX2 = (((Circle)balls.get(j)).x);
     float CollisionCheckY2 = (((Circle)balls.get(j)).y);
     float BallDiameter2 = (((Circle)balls.get(j)).diameter);
     
     if (dist(CollisionCheckX,CollisionCheckX2,CollisionCheckY,CollisionCheckY2) <= BallDiameter + BallDiameter2){
       
       changeDirection();    
     
   }
 }
}  


so im going to write and if statement that compares the coordinates of ball(i) to ball(j) and then run a function if they are touching.

any suggestions?

edit: added some code
Re: Reference an objects variable
Reply #11 - Mar 1st, 2010, 9:50am
 
Yes...have a method in the Circle class called reverse and then you would call ((Circle)balls.get(i)).reverse(); Note you need the brackets which indicate it is a method and not a field(variable).

Your condition you are looking for .... I would use the dist function...

if ( dist(x1, y1, x2, y2)<diameter1+diameter2)) - generalised (replace variables with your  own).
Re: Reference an objects variable
Reply #12 - Mar 1st, 2010, 9:56am
 
thanks Giles, i will continue this when i get home from work.

Re: Reference an objects variable
Reply #13 - Mar 1st, 2010, 9:58am
 
Of course, just reversing the balls' direction will look a bit weird.... What you really want is a collide(int i, int j) method in your main class that is called when you have a collision between ball i and ball j and calculates the speeds after the collision by conservation of momentum in the x and y directions.

The method could calculate the speeds of the balls and then set them.
Good luck...look forward to seeing it in action!

EDIT - you have to use conservation of kinetic energy, not momentum. BUt you can just use the formulas here, under "elastic collisions" http://en.wikipedia.org/wiki/Momentum#In_one_dimension
I always meant to do this myself for a container of particles but never got around to it....
Re: Reference an objects variable
Reply #14 - Mar 1st, 2010, 11:05pm
 
Giles wrote on Mar 1st, 2010, 9:34am:
Circle getball(int i)
{
 return (Circle)balls.get(i);
}

I hope we will get the Java 1.5 syntax someday to get rid of such workaround... Thus we can declare the array list to hold only Circle objects and get these objects directly.
Pages: 1 2