aspose file tools*
The moose likes Java in General and the fly likes Want a ball to go from point A to point B Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Want a ball to go from point A to point B" Watch "Want a ball to go from point A to point B" New topic
Author

Want a ball to go from point A to point B

Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Hello

Im trying to create a timed event in java.swing which when fired moves a dot, or ball from point A to point B.
I�ve tried the mathematical way i.e every 20 milliseconds or so the point changes position like this:

currentX += abX / (Math.abs(abX)); // Where abX is the distance from X of
currentY += abY / (Math.abs(abX)); // point A to x of point B and abY is the
// distance from Y of point A to Y of point B

A simple geometry...but the screen is made up of pixels and there is no such thing as 1/2 pixel or 1/3 pixel as the calculation of abY / (Math.abs(abX)) often result there for the dot always miss its target i.e point B on longer ranges.

I need an invisible line to be drawn between A and B and the dot to follow it.
Please help.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When you compute the new position from the prior position over and over you add up errors. So if it goes a pixel north once and a pixel north again you're off by two. Maybe you could compute the new position from the original position and the number of steps you have taken.

currentX = originalX + ( stepSizeX * stepCount )

That will get you very close to the end with only one error in the last calculation instead of a sum of many errors.

And if you have to hit the targe precisely, you might work backwards.

currentX = targetX - ( stepSizeX * stepsRemaining )

Or you might try keeping current position at high precision and rounding to the nearest pixel when you draw. I guess you could use floating point or do all your math with values multiplied by a million and divide by a million as you draw.

Any way you do it you might see some jiggle around your invisible line as you go.

Any of those sound like they might work?


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Unnar Björnsson
Ranch Hand

Joined: Apr 30, 2005
Posts: 164
Yes it did help alot i.e by incrementing a variable by abY/Math.abs(abX) every time and adding it to the original Y.

Thanks alot
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Cool! Let us know how things go.
Ole Kristian Sandum
Greenhorn

Joined: Sep 27, 2013
Posts: 10
I would try to work as if the coordinates were dimensions of vectors.

So you want to find the direction in which you want the ball to travel, by calculating the distances between point A (current position) and point B (goal):


Then you want to normalize that as if it were a vector, by finding the supposed vector's length and setting a velocity for x and y accordingly.


Lastly we apply the velocity to the current position


I hope this helps
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
Thank you for the solution.
Consider the Math#hypot method instead of using Pythagoras. It uses the same formula as Pythagoras, but only requires you enter the two distances. It can be used along with atan2 to convert rectangular to polar coordinates.
Did you realise this is an old discussion which has come to the surface because somebody posted a rubbish post on it?
Ole Kristian Sandum
Greenhorn

Joined: Sep 27, 2013
Posts: 10
Campbell Ritchie wrote:-snip- Did you realise this is an old discussion which has come to the surface because somebody posted a rubbish post on it?


Oh. I didn't see that. I'm sorry, I should have been more observant
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 36508
    
  16
No need to apologise You did nothing wrong
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Want a ball to go from point A to point B
 
Similar Threads
Switch Statement
Plugin in ImageJ giving random output, can't find reason.
finding closest point
How do I make a point travel in straight line?
Problem with JTable