This site requires JavaScript, please enable it in your browser!
Greenfoot back
hamchi
hamchi wrote ...

2012/10/17

Breakout code, very confusing? care to explain

hamchi hamchi

2012/10/17

#
now you guys dont get payd enough by the hour to explain this as a teacher, but can someone give somewhat of a direction what this code exactly doese? // offset returns the absolute distance between two objects on the X asis? WHY do you need that?
     int offset = Math.abs(this.getX() - object.getX()); 


          if(offset >= getImage().getWidth()/2) { 
				X = -X;
  			}
			else {
				Y = -Y;
			}
          
danpost danpost

2012/10/17

#
It appears that it is needed to determine which axis the ball should 'bounce' on (is the ball hitting the side of the block or is it hitting the top or bottom of the block). This code is most probably executed after an intersecting object (a block or the paddle) has been detected by the ball.
hamchi hamchi

2012/10/22

#
how doese it determine that though, sorry its been on my mind and i cant seem to figure it out still. and yes indeed its executed after getOneIntersectingObject. i have yet to figure out though, how the distance between to -X objects result in a way of determining where the ball hits the oh, wait. i managed to figure it out while writing this, well how about that not sure what to do with this line of text now such a waiste to delete it. to confirm? it gets the X of the specific object and calculates where it hit the image, but this calculation cant be exact right? i mean ive been running the game and its working, but not in 100% of the scenarios where the ball hits the image? any suggestions as to how i can fix this, it wont matter much for my project that i am assigned for school. but it seems like the code used, is more like a guessing game as to where ball hit the image.
danpost danpost

2012/10/22

#
The code says the same thing as the following
int offset = Math.abs(this.getX() - object.getX());
if (offset < getImage().getWidth() / 2) y = -y; else x = -x;
which might make more sense. If the objects are closer along the x-axis than half the width of the object (which should be the width of the block or paddle -- not the width of the ball) then the ball must be hitting the top or bottom face of the block or paddle and so the y-direction must reverse; otherwise, the x-direction must be reversing. It would seem, however, that corner bouncing would not simulate natural behaviour. I believe that would be what you were refering to when you said "not in 100% of the scenarios where the ball hits the image."
You need to login to post a reply.