Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Want a ball to go from point A to point B

 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Unnar Björnsson
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cool! Let us know how things go.
 
Ole Kristian Sandum
Ranch Hand
Posts: 31
1
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 31
1
IntelliJ IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48652
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to apologise You did nothing wrong
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic