Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The game "Breakout", stuck with collision checking

 
Kristjan Toots
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I am following CS106A lectures from youtube and I'm stuck with one of the assignments.
I am building a Breakout game Assignment PDF

I am encountering some difficulties on object collision department. I've created the method getCollidingObject() like instructions says.
Problem: When the ball collides with paddle the getCollidingObject method returns GRect and the will return true.

When the ball collides with a brick, the getCollidingObject will return something (when I printed returned object out, it just said GRect with some parameters) but the check does not work.

What I have tried to find out where the problem is:
1) I've printed out collision object when collided with bricks & paddle, both will return GRect object.
2) I've set getCollidingObject specifically to point to some random brick, it will return GRect object aswell.


Can't figure out where I'm going wrong.
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are comparing two objects with the == operator you are asking if the two references point to the same object in memory i.e are those two objects the one and the same?
If collision == paddle returns true that means that collision and paddle are the same instance assigned to two references.
If collision == rect returns false it means that collision and rect are not the same object.

Conclusion: paddle and rect are not the same object i.e they might be of the same class but they are not the same instance of that class. Just looking at those two lines

How can you expect the colliding object to be both the paddle and the brick, or am I misunderstanding your code?
 
Kristjan Toots
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unnar Björnsson wrote:
collision == paddle
collision == rect
How can you expect the colliding object to be both the paddle and the brick, or am I misunderstanding your code?

getCollidingObject() includes getElementAt(double x, double y) method what returns the top most object on these coordinates, otherwise null.




I would expect that if I create paddle it would be the same object that will get returned by getElementAt method when collided. This holds cause when comparing collision == paddle is true.

I would expect the same thing for bricks which are created in the same fashion. The getElementAt method will return something, but that something is not rect, collision == rect will be false.

When I manually print out the result from getElementAt on the brick I will get:


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic