Loading...
Logo
Processing Forum
How would one go about creating an array of a float inside of a array of a class? For example, say I have a class called Module[1...50], how could I create two types of variables like  ModuleX[1...50] and ModuleY[1...50] ?
Basically I need to store the X and Y values of the center point of a class Module[1...50] so that another class, Enemy, can check for collision detection with any given Module.

Perhaps a total noob question, I'm sorry. Still new to processing. I would show the code, but there are so many other things going on that it wouldn't be much good. Just looking for a small code example of how to do this. Thanks guys!

Replies(8)

Could you show a snippet of the code? not quite sure what you mean, 

heres a collection of simple collision detections 

might help


How would one go about creating an array of a float inside of a array of a class? For example, say I have a class called Module[1...50], how could I create two types of variables like  ModuleX[1...50] and ModuleY[1...50] ?
Basically I need to store the X and Y values of the center point of a class Module[1...50] so that another class, Enemy, can check for collision detection with any given Module.
This lay leads to disaster. A Module object should be totally unaware of any other Module object. Imagine the amount of data if there are 50 Module objects and each one had an array of x,y coordinates to the others it would require 2500 sets of coordinates to maintain - a Module might move a Module might die a new Module might be added - it would be impossible to keep the data accurate.

The program below has two classes Friend and Enemy if you look inside the classes then you will see that a Friend object only has information about itself, but it does have method to see if a particulare Enemy object is close enough to represent a collision. The Enemy class is similar.

This is sample output from this program

Friend 101
Friend 102
Friend 103
   Enemy 218
   Enemy 281
Friend 104
Friend 105
Friend 106
Friend 107
   Enemy 225
Friend 108
Friend 109
Friend 110

We can see that friend 103 has collided with two enemies and friend 107 with one enemy.

Copy code
  1. Friend[] friends;
  2. Enemy[] enemies;
  3. float d = 50;

  4. void setup() {
  5.   Friend[] friends = new Friend[10];
  6.   Enemy[] enemies = new Enemy[100];
  7.   for (int i = 0; i < friends.length; i++)
  8.     friends[i] = new Friend(101 + i, random(10, 190), random(10, 190));
  9.   for (int i = 0; i < enemies.length; i++)
  10.     enemies[i] = new Enemy(201 + i, random(10, 190), random(10, 190));
  11.   // Report all friends who have enemies with d pixels
  12.   for (int i = 0; i < friends.length; i++) {
  13.     println("Friend " + friends[i].id);
  14.     for (int j = 0; j < enemies.length; j++) {
  15.       if (friends[i].collision(enemies[j])) {
  16.         println("   Enemy " + enemies[j].id);
  17.       }
  18.     }
  19.   }
  20. }

  21. class Friend {
  22.   int id;
  23.   float x, y;

  24.   Friend(int pid, float px, float py) {
  25.     id = pid;
  26.     x = px;
  27.     y = py;
  28.   }

  29.   boolean collision(Enemy e) {
  30.     if ((x - e.x)*(x - e.x) + (y - e.y)*(y - e.y) < d)
  31.       return true;
  32.     else
  33.       return false;
  34.   }
  35. }

  36. class Enemy {
  37.   int id;
  38.   float x, y;

  39.   Enemy(int pid, float px, float py) {
  40.     id = pid;
  41.     x = px;
  42.     y = py;
  43.   }

  44.   boolean collision(Friend f) {
  45.     if ((x - f.x)*(x - f.x) + (y - f.y)*(y - f.y) < d)
  46.       return true;
  47.     else
  48.       return false;
  49.   }
  50. }

How the old games of yore did collision detection was the use of a bounding box and checking all moving objects if the bounding box had a collision. There's also the other method, for 2D, which is you project the movement and see if any of the lines have intersected. 
Sorry, perhaps I should rephrase my question. Basically I have two classes in arrays, Shot[50] and Enemy[50]. When a Shot class hits an Enemy class, both of them need to detect the collision, but only the two colliding need to detect the collision, not every Shot and Enemy in the array. I need globolized variables for Shot x y and Enemy x y so that on contact, not every class in the array reads collision detection, correct?
To do this, I'd like to make 4 arrays of information, ShotX[50], ShotY[50], EnemyX[50], and EnemyY[50]. Now, my problem comes in where I don't know how to link each XY variable (ShotX, ShotY, EnemyX, EnemyY) to the corresponding arrayed Class. For example, I don't know how to attach variables ShotX[42] and ShotY[42] to class Shot[42]. And I don't understand how I tell the sketch to only affect the Shot and Enemy classes colliding and not the whole array of classes.

Say I have a collision with Enemy[42] and Shot[47]. How do I create/manipulate variables to use for ONLY one specific collision (the collision between Enemy[42] and Shot[47]) that won't affect any other Enemy or Shot object in the array?

Sorry for the confusion, thanks you guys for your input!
Quark, it looks very much like your code will do exactly what I need. I'll try experimenting with it, thanks!

I'm still a bit fuzzy on the Boolean functions, what purpose do the "e" and "f" variables serve in their respective boolean functions? are they placeholder variables, like the "i" and "j" in the "for" statements?
You could look at 'e' and 'f' as placeholder variables - but in this context they are called parameters.

So in the Friend class we have the method

Copy code
  1.   boolean collision(Enemy e) {
  2.     if ((x - e.x)*(x - e.x) + (y - e.y)*(y - e.y) < d)
  3.       return true;
  4.     else
  5.       return false;
  6.   }
This method is used to detect if this Friend is close to e, which can be any object of type Enemy.

SO in the main code we have the collision detection double loop

Copy code
  1.   for (int i = 0; i < friends.length; i++) {
  2.     println("Friend " + friends[i].id);
  3.     for (int j = 0; j < enemies.length; j++) {
  4.       if (friends[i].collision(enemies[j])) {
  5.         println("   Enemy " + enemies[j].id);
  6.       }
  7.     }
  8.   }

In this line we are saying
for the friend object at index i (in friends) call its collision method passing the enemy object at index j (in enemies)

If you look at the values of i and j as the double loop progresses you get

i                  j
0                  0
0                  1
0                  2
0                  3
...                ...
0                  enemies.length -1
1                  0
1                  0
1                  0
...                ...

So by the time the double loop is finished every friend has been tested against every enemy.

Notice that
  • the code inside the Friend class has no attributes/fields that identify any other Friend objects   and  
  • the code inside the Enemy class has no attributes/fields that identify any other Enemy objects

This is the way it should be when using OO i.e. classes and objects  

HTH


A Bullet class basic template:
Copy code
    class Bullet {
      static final int DIM = 4;
      int x, y;
    
      Bullet(int xx, int yy) {
        x = xx;
        y = yy;
      }
    
      void display() {
        ellipse(x, y, DIM, DIM);
      }
    
      boolean hasCollided_A(Enemy e) {  // for squared Enemy forms
        return x > e.x & x < e.x + e.w & y > e.y & y < e.y + e.h;
      }
    
      boolean hasCollided_B(Enemy e) {  // for elliptical Enemy forms
        return sq(x - e.x) + sq(y - e.y) < sq(e.radius);
      }
    }
    
And a more complex online example:
http://studio.processingtogether.com/sp/pad/export/ro.91kLmk61vAOZp/latest
An online example similar to quarks': 
http://studio.processingtogether.com/sp/pad/export/ro.9s0026dE7B8v-/latest

Pay attention to function connectPoints(), which is called from the for loop update within draw().