ArrayList and Inheritance explanation
in
Programming Questions
•
1 year ago
Hello,
I'm trying to get my head around inheritance. All goes well up to the point where I create an ArrayList of objects and need to provide the right argument(s) to get all of its members methods properly initiated.
The 'Bumper' class has the method separate. i don't know where i should be calling this method from and how to provide the ArrayList it is expecting. I'm assuming i should be doing it from the 'Universe' class, since the superclass of 'Bumper' is being called from there.
Hopefully the question and code are clear.
any comments/reference/help will be appreciated.
Thanks in advance
tzl
I'm trying to get my head around inheritance. All goes well up to the point where I create an ArrayList of objects and need to provide the right argument(s) to get all of its members methods properly initiated.
The 'Bumper' class has the method separate. i don't know where i should be calling this method from and how to provide the ArrayList it is expecting. I'm assuming i should be doing it from the 'Universe' class, since the superclass of 'Bumper' is being called from there.
Hopefully the question and code are clear.
any comments/reference/help will be appreciated.
Thanks in advance
tzl
- Universe universe;
- int numBoids = 100;
- void setup() {
- size(600, 400);
- smooth();
- universe = new Universe();
- for (int i = 0; i<numBoids; i++)
- universe.add(new Agent(universe));
- for (int i = 0; i<(numBoids*2); i++)
- universe.add(new Bumper(universe));
- }
- void draw() {
- background(255);
- universe.update();
- universe.display();
- }
- class Agent extends Element {
- Agent(Universe universe) {
- super(universe);
- }
- void update() {
- super.update();
- keepInside(universe, 0.5);
- }
- void keepInside(Universe universe, float efficiency) {
- // efficiency ~ 10 : boids immediately rejected
- // efficiency ~ .1 : boids slowly change direction
- float maxX = universe.x + universe.w;
- float maxY = universe.y + universe.h;
- // When a boid approach a side, an opposite vector is added to velocity (bounce)
- PVector v = new PVector();
- if (loc.x <= universe.x + rad ) v.x = (universe.x + rad - loc.x) * efficiency;
- else if (loc.x >= maxX - rad) v.x = (maxX - rad- loc.x) * efficiency;
- if (loc.y<= universe.y + rad) v.y = (universe.y + rad- loc.y) * efficiency;
- else if (loc.y >= maxY - rad) v.y = (maxY- rad - loc.y) * efficiency;
- vel.add(v);
- }
- void display() {
- super.display();
- noFill();
- stroke(5);
- ellipseMode(CENTER);
- ellipse(loc.x, loc.y, rad, rad);
- }
- }
- class Bumper extends Element {
- float maxspeed;
- float maxforce;
- Bumper(Universe universe) {
- super(universe);
- maxspeed = 0.75;
- maxforce = 0.25;
- }
- void applyForce(PVector force) {
- // We could add mass here if we want A = F / M
- acc.add(force);
- }
- // Separation
- // Method checks for nearby vehicles and steers away
- void separate (ArrayList<Bumper> bumpers) {
- float desiredseparation = rad*2;
- PVector sum = new PVector();
- int count = 0;
- // For every boid in the system, check if it's too close
- for (Bumper other : bumpers) {
- float d = PVector.dist(loc, other.loc);
- // If the distance is greater than 0 and less than an arbitrary amount (0 when you are yourself)
- if ((d > 0) && (d < desiredseparation)) {
- // Calculate vector pointing away from neighbor
- PVector diff = PVector.sub(loc, other.loc);
- diff.normalize();
- diff.div(d); // Weight by distance
- sum.add(diff);
- count++; // Keep track of how many
- }
- }
- // Average -- divide by how many
- if (count > 0) {
- sum.div(count);
- // Our desired vector is the average scaled to maximum speed
- sum.normalize();
- sum.mult(maxspeed);
- // Implement Reynolds: Steering = Desired - Velocity
- PVector steer = PVector.sub(sum, vel);
- steer.limit(maxforce);
- applyForce(steer);
- }
- }
- void update() {
- super.update();
- // Limit speed
- vel.limit(maxspeed);
- loc.add(vel);
- // Reset accelertion to 0 each cycle
- acc.mult(0);
- }
- void display() {
- noStroke();
- fill(255, 20, 36);
- ellipseMode(CENTER);
- ellipse(loc.x, loc.y, rad, rad);
- }
- }
- class Element {
- Universe universe;
- PVector loc, vel, acc;
- float rad;
- Element(Universe universe)
- {
- this(universe, universe.randomPos());
- }
- Element(Universe universe, PVector loc)
- {
- this.universe = universe;
- this.loc = loc;
- vel = new PVector(random(-2, 2), random(-2, 2));
- acc = new PVector(0, 0);
- rad = random(4, 5);
- }
- void display() {
- }
- void update() {
- loc.add(vel);
- }
- }
- class Universe {
- int x, y, w, h;
- ArrayList <Element> element;
- Universe() {
- x = 0;
- y = 0;
- w = width;
- h = height;
- element = new ArrayList<Element>();
- }
- PVector randomPos() {
- return new PVector(random(x, x+w), random(y, y+h));
- }
- Element getIt(int i)
- {
- return (Element)element.get(i);
- }
- void add(Element e)
- {
- element.add(e);
- }
- void add(Element e, int n)
- {
- for (int i=0; i < n; i++)
- element.add(e);
- }
- void update() {
- for (int i = 0; i<element.size(); i++) {
- Element el = (Element) element.get(i);
- el.update();
- }
- }
- void display() {
- for (int i = 0; i<element.size(); i++)
- ((Element)getIt(i)).display();
- }
- }
1