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 › classes question
Page Index Toggle Pages: 1
classes question (Read 318 times)
classes question
Oct 28th, 2008, 11:09am
 
all,

this probably has a really obvious solution, but i'm new and it is doing me head in

i have two classes, one of which is called within the other (ie parent/child). so far so good.

at the minute the parent is constantly redefining two variables; 'bearing' & 'speed', which the child then uses to set a position. the parent then references this. etc etc.

problem is, for both classes to reliably see the variables, i have to define them outside of the parent class - ie in the universal level. how can i put them into the parent class and still have the child see them? i need to do this so that i can do an array of independent versions of the parent...

would be great if someone could give me a few pointers!

sam

p.s. how do i attach a file?
s

Re: classes question
Reply #1 - Oct 28th, 2008, 12:15pm
 
you can use parent.bearing and parent.speed but only if the two members are public

(and this is usually done via accessor methods - parent.getParent())

however, the child would then need to know which parent it's trying to get the values from. if the members within the parent are static then Parent.getSpeed() will work. if it isn't then you're going to have to tell the child about the parent, usually at instantiation.

confused yet? 8)

http://java.sun.com/docs/books/tutorial/java/javaOO/usingobject.html

http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html

processing further complicates this because all your classes are effectively inner classes within the main Processing class but...

> how do i attach a file?

i don't think you can. put it on the web somewhere and link to it or, if it's code, choose the 'export for discourse' option in processing and paste it here.
Re: classes question
Reply #2 - Oct 28th, 2008, 12:17pm
 
it'll make more sense with an example. this java stuff's mostly just complicated words to describe things that you already know about 8)
Re: classes question
Reply #3 - Oct 28th, 2008, 12:26pm
 
okey dokey...

i think i know what that means - i'm doing something similar for the parent to get values from the child

but here's the code anyway as an example...

tab 1:

Agent agent1 = new Agent(250,250,50);

   float speed;
   float bearing =0;    
   float degreeS;
   

void setup()
 {
   rectMode(CENTER);
   size(500,500);
   frameRate(50);
   smooth();
 }

void draw()
 {
   background(192);
   stroke(100);
   
   rect(150,100,100,100);
   rect(400,350,100,200);
   rect(mouseX,mouseY,50,50);
   agent1.draw();
 }


other tabs to follow...
Re: classes question
Reply #4 - Oct 28th, 2008, 12:27pm
 
tab 2
(parent class)

class Agent
 {
   int posA;
   int posB;
   float posX;
   float posY;
   float diam;
   int eye = 13;
   float head_turn = 0;
   float head_turn_previous = 0;
     
   float vector_1;
   float vector_2;
   float vector_3;
   float vector_4;
   float vector_speed;
   
   float left_rotate_vector;
   float right_rotate_vector;
   float rotate_vector;
   float rotate_vector_previous;

   Agent(int x, int y, float d){
   posX = x;
   posY = y;
   diam = d;
 }

void draw()
 {
   pushMatrix();
   turn();
   move();
   translate(posA,posB);
 
     rotate(head_turn);
   
   ellipse(0,0,diam,diam);
   pushMatrix();
   translate(eye,0);
   ellipse(0,0,diam/5,diam/5);
   rotate(-0.4*PI);
   pushMatrix();
   for (int i = 0; i < depth_rays.length; i++)
   {
   depth_rays[i].draw();
   depth_rays[i].detect();
   rotate((0.8*PI)/(depth_rays.length - 1));
   }
   popMatrix();
   popMatrix();
   popMatrix();
  }
 
Depth_ray [] depth_rays = {new Depth_ray(250,250,-2,150), new Depth_ray (250,250,-1,150), new Depth_ray(250,250,0,150), new Depth_ray(250,250,1,150), new Depth_ray(250,250,2,150)};

void turn()
{  
   degreeS = PI/180;

   vector_1 = (depth_rays[0].vector/depth_rays[0].ray_view);
   vector_2 = (depth_rays[1].vector/depth_rays[1].ray_view);
   vector_3 = (depth_rays[3].vector/depth_rays[3].ray_view);
   vector_4 = (depth_rays[4].vector/depth_rays[4].ray_view);
   
   left_rotate_vector = (((2*vector_1)+vector_2)/(vector_1+vector_2));
   right_rotate_vector = (((2*vector_3)+vector_4)/(vector_3+vector_4));
     
   rotate_vector = (right_rotate_vector - left_rotate_vector)*(36);  // vector of rotation * angle between rays
   
   if (rotate_vector == rotate_vector_previous)
   {
   head_turn = head_turn; // number is in terms of PI
   bearing = bearing; // accumulative bearing is in terms of degrees
   }
   else
   {
   head_turn = head_turn+(rotate_vector*degreeS); // number is in terms of PI
   bearing = bearing+(rotate_vector); // accumulative bearing is in terms of degrees
   rotate_vector_previous = rotate_vector;
   }
   
   if (bearing > 359)
   {
     bearing = 0;
     head_turn = 0;
   }
   if (bearing < -359)
   {
     bearing = 0;
     head_turn = 0;
   }
   
   
}

void move()
{
   vector_speed = (depth_rays[2].vector/depth_rays[2].ray_view);
   
   speed = 1*vector_speed;
   
   posX = posX+speed*(cos(bearing*degreeS));
   posA = int (posX);
   posY = posY+speed*(sin(bearing*degreeS));
   posB = int (posY);
   
}
 }
Re: classes question
Reply #5 - Oct 28th, 2008, 12:27pm
 
tab 3
(child class)

class Depth_ray
{
   int noRays = 5;
 
   float rayX;
   float rayY;
   int rayA;
   int rayB;
   int rayE = 13;
   float ray_set_angle;
   float rot_dir;
   float ray_view;
   float ray_tip;
   float angle_between_rays;

   int rayXfinal;
   int rayYfinal;
   float X_tip_angled;
   float Y_tip_angled;
   
   float X_rayE;
   float Y_rayE;
       
   int vector = 150;
   int j = 0;

   color look;
   float detect;
 
   Depth_ray(int r_x, int r_y, float r_d, float r_v){
   rayX = r_x;
   rayY = r_y;
   rot_dir = r_d;
   ray_view = r_v;
 }
 
 void draw()
 {
   pushMatrix();  // here we are on eye rayY coordinates
   if (j < ray_view)
   {
     line(25,0,22,0);
     translate (25+(j),0);
     line(0,0,-0.5*(j),0);
     j++;
  }
   ray_move();
   popMatrix();
 }
 
 
 void detect()
 {
   ray_tip = (25+(j)+1);
   angle_between_rays = (144/(noRays - 1));
   ray_set_angle = angle_between_rays*rot_dir*degreeS;
   
   X_tip_angled = ray_tip*cos(ray_set_angle+(bearing*degreeS));  //position of x detection with angled rays and bearing
   Y_tip_angled = ray_tip*sin(ray_set_angle+(bearing*degreeS));  //position of y detection with angled rays and bearing
   
   X_rayE = rayE*cos(bearing*degreeS);   //length of e in x axis with current bearing
   Y_rayE = rayE*sin(bearing*degreeS);   //length of e in y axis with current bearing
   
   rayXfinal = int(X_rayE+X_tip_angled);
   rayYfinal = int(Y_rayE+Y_tip_angled);

   look = get(rayA+rayXfinal,rayB+rayYfinal);
   detect = brightness(look);
   
   if (detect != 192)
   {
     if (detect != 220)
     {
     vector = j;
     j = 0;
   }}
   else if (j == ray_view)
   {
     vector = j;
     j = 0;
   }
 }
 
 void ray_move()
 {
   rayX = rayX+speed*(cos(bearing*degreeS));
   rayA = int(rayX);
   rayY = rayY+speed*(sin(bearing*degreeS));
   rayB = int(rayY);
 }
}
Re: classes question
Reply #6 - Oct 28th, 2008, 12:29pm
 
that's the lot

code is quick and dirty, like i said - most can be ignored as the two values are the float speed; float bearing =0; ones.

i'd like to drop them into the Agent class...


s

Re: classes question
Reply #7 - Oct 28th, 2008, 12:45pm
 
ok... i get it
but...!

speed = agent1.speed; doing this for speed and bearing within the child works

obvious now i think about it! doh


but with multiple versions of the 'agent' ie agent1. agent2. etc etc
how do i tell the child which one to go for???

thanks loads
s

Re: classes question
Reply #8 - Oct 28th, 2008, 1:34pm
 
you'd need to pass the agent to the depthray during instantiation and keep a local copy within it and use that

in depth_ray class add a member

Agent agent;

change constructor to

Depth_ray(Agent r_agent, int r_x, int r_y, float r_d, float r_v) {
agent = r_agent;
...
}

and in the agent class change the calls to depth_ray to

new Depth_ray(this, 250, 250, -2, 150) etc

then within depth_ray it's just agent.whatever to use the local reference.
Re: classes question
Reply #9 - Oct 28th, 2008, 2:29pm
 
very clever! a whole new world of possibilities...

many thanks!

s

Page Index Toggle Pages: 1