Search code examples
javaarraysgame-enginemultiple-value

return and edit of multiple variables [JAVA]


i have a problem in my first Game engine so please help me :(

There is two part in the first one i will explain the problem and in the second one i will explain my questions .

Part I :

i have an array (named "World") of object class

public Object World[] = new Object[500];

the object have many properties (name,x,y,animation,length,width ....)

i want to make condition of collession for example

if( Function_to_detect_collessions("object1_name","object2_name") ){
object2.Animation = "new value" ;
} 

and with these lines you will understand me :

1- many object can get the same name

2- if more than one collession happened with more than two objects with the same names (object1_name and object2_name) then the modification for the object2.animation will be on all the touched objects

example :
if( collesion("ball","ground") ){
ball.movement = stop;
}
//Now imagine that there is two objects (two Balls) on the ground

Part II :

i think that you understand me what i mean and now i will explain my question. questions :

1- if i can detect all the collessions how to make the modification on all the objects with one line like

object2.prop = "something"

2- is it possible in java to make modification on an object and with some functions make the same modification on more than one object automatically .

-----------------------------------------------------

I'm Sorry for my bad English but i tried to use all the words in my mind to explain the problem and i wish i did (any answer could help me even with a part of a solution so please help)


Solution

  • You could try to use Java collections like this:

    public ArrayList<Object> World= new ArrayList<>();
    
    for (int i =0; i<500; i++)
      World.add(new Object(i));
    
    // Update all objects
    for (Object myObject : World) myObject.prop = "Something";
    

    EDIT:

    As per your subsequent question. If you need to go only over a particular list you can do this:

    public ArrayList<Object> MoversAndShakers = new ArrayList<>();
    
    MoversAndShakers.add(World.get(3));
    MoversAndShakers.add(World.get(5));
    MoversAndShakers.add(World.get(9));
    
    // Update all MoversAndShakers
    for (Object myObject : MoversAndShakers) myObject.prop = "Something";
    

    Or even better you could encapsulate that in a function to detect collisions (You may want to look at this question on how to do that Simple and fast collision algorithm in java for non-axis aligned boxes) :

    public ArrayList<Object> MoversAndShakers = new ArrayList<>();
    
    ArrayList<Object>  getMovingObjects(ArrayList<Object> World)
    {
      ArrayList<Object> MoversAndShakers = new ArrayList<>();
    
      for(Object currentObj : World)
      {
         if (currentObj.velocity > 0)
              MoversAndShakers.add(currentObj);
      }
      return MoversAndShakers;
    }
    

    And then above reduces to this

    // Update all moving objects
    for (Object myObject : getMovingObjects(World)) 
       myObject.prop = "Something";