Loading...
Logo
Processing Forum
Hello, I'm working on a little project for school.
Eventually this program should have 'Agents' (ellipses) which are following my mouse movement. They should also bounce off black colored objects (this is not yet included in current coding).

This is my current code 

Line 4 is where I define 'loc'

but at line1 of the other code part it gives the error: Cannot find anything named "loc"
Why?


Copy code
  1. class Agent {

  2.   PVector loc = new PVector(width/2,height/2); //Here I defined "loc"
  3.   PVector vel = new PVector(0, 0, 0);
  4.   PVector acc = new PVector(1, 1, 0);

  5.   Agent(PVector _loc) {
  6.     loc = _loc;
  7.   }

  8.   void run() {

  9.     display();

  10.     update();

  11.     followMouse();
  12.   }
  13.   void followMouse() {

  14.     PVector target = new PVector(mouseX, mouseY, 0);

  15.     PVector dif = PVector.sub(target,loc);   

  16.     float distance = dif.mag();

  17.     dif.normalize();

  18.     dif.div(100);
  19.     acc.add(dif);
  20.   }
  21.   void update() {

  22.     vel.add(acc);

  23.     vel.limit(7);

  24.     loc.add(vel);

  25.     acc = new PVector();
  26.   }
  27.   void display() {
  28.     stroke(0);
  29.     fill(255);
  30.     ellipse(loc.x, loc.y, 10, 10);
  31.   }

  32.   void botsen() {
  33.     if ((loc.x == 622) || (loc.x == 25))
  34.     { 
  35.       vel = vel*-1;
  36.     }
  37.   }
  38. }  

Along with this:
Copy code
  1. Agent myAgent = new Agent(loc); // Error: "cannot find anything named "loc" "  I defined "loc" above right?

  2. void setup() {
  3.   size (647, 694);
  4.   PImage img1;
  5.   smooth();
  6.   img1 = loadImage("Klembord01.gif");
  7.   

  8.  
  9. }

  10. void draw() {
  11.   fill(255, 10);
  12.   PImage img1;
  13.   img1 = loadImage("Klembord01.gif");
  14.   image(img1, 0, 0);
  15.   a.run();
  16.   
  17.   }

Thanks in advance!

Replies(13)

Meaning sub doesn't return a vector

It returns nothing (void).

It just makes target smaller I think.

So divide the line into two  - One being
target.sub(loc);

The other

PVector dif = target;

or so.


Thanks, it helped. But only to find the next problem.
I'll try to figure it out myself, I don't want to have a working program without know how 
Try
PVector target = new PVector(mouseX, mouseY);
instead of line 22
Also refer this[1] to make your life easy.
@Chrisir
Have a look at this[1].
According to it vec1.sub(vec2) should return a vector. I think the problem is in trying to subtract a 3 dimensional vector from a 2 dimensional vector.

I changed the code into:

PVector dif = target;
target.sub(loc);

Now it does not give an error anymore, but I do not yet know if it does what it has to do because there are some other errors in my code. I'll try to fix them and see if it works!

Thanks for the references by the way :)
Oh...my bad.
Only PVector.sub(v1,v2) returns a Pvector.
Mind if I ask you another question? ^_^ 

Now it says: the function scaleSelf(float) does not exist at line 30.
Do you know another way to put this 'scaleSelf(float)' function?

I think it gives an error because all the PVectors were Vec3D's.

I sort of 'translated' them into PVectors, this because I do not know anything about Vec3D's (I'm working on this project with a classmate of mine, he came up with the code with Vec3D's, but the Vec3D code needed a library and that somehow did not work on my computer) 
PVector dif = target.sub(loc);

The problem is that the sub method does not return a new PVector. The result of the above statement is to subtract the xyz values of loc from the xyz values in target. In other words the PVector target is changed from
target.x, target.y, target.z
to
target - loc.x, target.y - loc.y, target.z - loc.z
but the actual method always returns void which cannot be stored in diff hence the error message.

The soultion is to use the following method.
PVector dif = PVector.sub(target, loc);

In this statement target and loc are unchanged but this it will return a PVector object whcih is the difference between them.

Many replies suddenly, but thanks a lot!
If it does not work I'll try this :)
scaleSelf() is a method provided by the library you are using. So clearly you can not apply that method to a PVector object. It is something like asking a car to fly. Car does not have the method fly. You should use a plane instead. Hope you got my point
got it, thanks :)
I think I'm almost there, I fixed the scaleSelf() function.
I'll read those references and try to fix what's left :)

I hope you don't mind if I ask some questions if I'm stuck. :)
Could you please look at the code above, i edited it.
It says :  Cannot find anything named "loc"
But I defined "loc"as:

PVector loc = new PVector(width/2,height/2);
The problem is that loc is part of the class Agent so is not 'visible' outside the class. Also you want to avoid using loadImage insdie the draw method which is executed 60 times a second.

So change the code to

Copy code
  1. Agent myAgent;
  2. PImage img1;

  3. void setup() {
  4.   size (647, 694);
  5.   smooth();
  6.   img1 = loadImage("Klembord01.gif");
  7.   myAgent = new Agent(new PVector(width/2, height/2);
  8. }

  9. void draw() {
  10.   fill(255, 10);
  11.   image(img1, 0, 0);
  12.   a.run();
  13. }

In the Agent class I would change the constructor to

Copy code
  1. Agent(PVector _loc) {
  2.     loc.set(_loc);
  3. }
since you have already created an object for the attribute loc.

HTH