Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

The game "Breakout", stuck with collision checking

 
Ranch Hand
Posts: 59
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:


 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic