• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

dx = -dx; dy = -dy

 
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you explain how variables dx and dy are used to reverse the direction of a bouncing ball?



Source Core Java Vol.I (8th Ed), Ch.14
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It isn't very hard code to figure out -- in fact, it is incredibly easy. And figuring out another's code is a common task all developers do. Care to take a shot at it, and tell us how you think it works first?

Henry
 
Jon Camilleri
Ranch Hand
Posts: 664
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:
It isn't very hard code to figure out -- in fact, it is incredibly easy. And figuring out another's code is a common task all developers do. Care to take a shot at it, and tell us how you think it works first?

Henry



I figured out that by default the ball object moves by (1,1), however, when the ball reaches the enclosing bounds (of type Rectangle2D), the sign is reversed so that the sign of x and y are changed. This is done so when the dx or dy exceeds the bounds the ball switches to the opposing bound. For example, if x is becoming -5 then it becomes +5, so that the ball position starts from (5, y). I just needed a confirmation.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Confirmed...
 
Ranch Hand
Posts: 164
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jon Camilleri wrote:
I figured out that by default the ball object moves by (1,1), however, when the ball reaches the enclosing bounds (of type Rectangle2D), the sign is reversed so that the sign of x and y are changed. This is done so when the dx or dy exceeds the bounds the ball switches to the opposing bound. For example, if x is becoming -5 then it becomes +5, so that the ball position starts from (5, y). I just needed a confirmation.



No the sign of x and y is never changed, when the ball's position crosses any of the four boundaries, the ball is set to the boundary wall it exceeded and the sign of either dx or dy is changed f.ex when x becomes less than bounds.getMinX() x is set to bounds.getMinX() and dx becomes -dx
 
reply
    Bookmark Topic Watch Topic
  • New Topic