Search code examples
javacollision-detection

Check for collision between objects in a static way?


Full disclosure: I am working on a homework assignment. Part of the assignment is to determine if two objects (call them Thing objects) have the same x,y coordinates.

My approach has been to instantiate the two Things in a main method, and check for an overlap in their coordinates by calls to a public boolean sameSpace(Thing one, Thing two) method. This approach is working perfectly.

However, the instructions of the question say to implement a method public static boolean sameSpace(Thing one, Thing two) within the Thing class. The static part of this confuses me.

I can't figure out how I would do the overlap check from within the class - how would one object have access to the coordinates of the other object? (For that matter, how would one object even "know" that the other object existed?) It seems to me that the overlap checking has to be done non-statically.

Any thoughts?


Solution

  • I know it's taboo to answer homework questions directly but I think you understand the assignment and are just confusing yourself on the details. Your professor most likely wants something that looks like this.

    public class Thing {
    
        public int x, y;
    
        public static boolean sameSpace(Thing one, Thing two) {
            return one.x == two.x && one.y == two.y;
        }
    
    }
    

    static simply means that the method can be invoked as such:

    Thing.sameSpace(thing1, thing2)