• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is this the right way to use chaining with objects?  RSS feed

 
Aman Grover
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have 2 classes as follows:





In the class OverlapTester , c1 is an object of class Circle and center is an object of another class Vector2. How can the c1 object call the center object to call disSquared() method?
I am used to code in C++ and I am new to java , this is a small code snippet from a Java framework(for a game) to detect collisions between two circles. I understand the basic idea of the code , but I am confused with the line:
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Why have you made those fields public? That is usually a serious mistake. They should be private. What is Vector2? Why are you using floats rather than doubles? Maybe because you can get away with reduced precision in a game?

I think you should have intersects and collides methods in your shape classes. You can work out the distance between the two centres with Pythagoras (there is a method in the Math class which does that if given the differences in the two x and the two y values). If the distance equals the sum of the radii they are touching and if the distance is less than the sum of diameters they are intersecting/overlapping.
Beware: That method returns a double; even with casting there is a risk that you will get incorrect answers for circles which are only just touching or which have a very very small overlap because of precision problems.

Does your Circle class not implement a Shape interface? Does that interface not have intersects and collides methods?

The line you mentioned looks like bad design because it depends on public fields as mentioned above, and also two other reasons:
  • 1: So‑called Law of Demeter. You have code with lots of dots in, and that is confusing.
  • 2: You should (I think) have touches and intersects methods in the circle class, so you can write if (c1.intersects(c2)) .... Much easier to read.
  •  
    Aman Grover
    Greenhorn
    Posts: 11
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . .

    It is just a code snippet, the actual code and the project consists of hundreds of lines of code,and I understood each and every line well , just not this concept, calling method distSquared, which is of type Vector2. center.distSquared() is fine , since center is an object of class Vector2 , but what does this line mean?



    [edit]CR: Removed quote of entire previous post, which is unnecessary and simply makes the thread longer to read.[/edit]
     
    Campbell Ritchie
    Marshal
    Posts: 56546
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    It is badly written; it should say distanceSq on the left. That would make it easier to understand; hypotenuseSq would be easier still.
    What it means is that you are using the distSquared() method to find the square on the hypotenuse. You have c1 with the strangely‑typed field center (type Vector2) and you are using its distSquared method to find the square on the hypotenuse of a triangle with center at one corner and the center field of the other circle at the other corner.
     
    Junilu Lacar
    Sheriff
    Posts: 11493
    180
    Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This is a prime example of procedural code in Java and a violation of the Law of Demeter. The code really should be:

    The details of calculating whether circle1 overlaps circle2 should be entirely encapsulated in the Circle class.

    Edit: I guess Campbell already mentioned Demeter. The name Vector2 is not a good name. Given the concept that it represents, a better name would be Point.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!