This week's book giveaway is in the Java 8 forum.
We're giving away four copies of Java 8 in Action and have Raoul-Gabriel Urma, Mario Fusco, and Alan Mycroft on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes dx = -dx; dy = -dy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "dx = -dx; dy = -dy" Watch "dx = -dx; dy = -dy" New topic
Author

dx = -dx; dy = -dy

Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

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


Jon
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18139
    
  39


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jon Camilleri
Ranch Hand

Joined: Apr 25, 2008
Posts: 659

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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 13884
    
  10

Confirmed...


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dx = -dx; dy = -dy
 
Similar Threads
A simple syntax question, pls help
exception java.lang.InterruptedException is never thrown in body of corresponding try statement
C++ish Address &
Help needed with the Code
New to Java. Need some proformance suggestions