aspose file tools*
The moose likes Beginning Java and the fly likes The game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "The game "Breakout", stuck with collision checking" Watch "The game "Breakout", stuck with collision checking" New topic
Author

The game "Breakout", stuck with collision checking

Kristjan Toots
Ranch Hand

Joined: Jun 03, 2011
Posts: 59

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.

Please correct my english.
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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

Joined: Jun 03, 2011
Posts: 59

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:


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: The game "Breakout", stuck with collision checking