Hi All,
I am working on making a single-sprite version of my project Fling Ball and I need a way to find the distance between two places with only one sprite. Let's say I have variables for "Old_X" and "Old_Y" and variables for "New_X" and "New_Y." Is it possible to use trig functions to find the distances between two points? Any help is greatly appreciated.
Offline
Don't really need the trig functions, just the square root function, sqr[ ]. The equation is:
distance = sqr((old_x - new_x) * (old_x-new_x) + (old_y - new_y)*(old_y - new_y)).
that is, you take the difference between the x's and square the result (multiply it by itself), then take the difference between the y's and square that result, add the two results together, then take the square root.
maybe somebody good at putting scratch blocks into posts can illustrate it better than this.
Offline
Thanks, Chalkmarrow. That's a good explanation for me.
I still have a problem, though. How can I find the direction from (new_x, new_y) to (old_x, old_y)? This is important because this direction determines which way the ball will roll. Any help from the scratch community would be appreciated.
Offline
Well, it's not as simple as you might hope. You can do it with the Trig function ATAN by giving it the quotient of the difference in X divided by the difference in Y but it gets ugly as it won't tell you what quadrant it is in. So you end up doing a lot of testing on the sign of the differences of X and Y to get the quadrant correct. Some languages have a function called ATAN2 (which takes two arguments) which will give you the angle in the correct quadrant. It would be nice if Scratch implemented ATAN2 someday. Until then, if you really want it, I can upload a project with the logic in it. Or, you might want to consider an easier approach, like send a sprite to the location and query the sprite.
Offline
Thanks for the info, Paddle2See. I would appreciate an example project, if you have time for it. If you have something better to do (which I assume you have) don't worry about it.
Offline
Getting the angle and the distance is essentially converting from Cartesian (rectangular) coordinates to Polar coordinates. I have uploaded a project called Cartesian to Polar on the account I use for demonstrations and repaired projects, Paddle2SeeFixIt. The link is as follows:
http://scratch.mit.edu/projects/Paddle2SeeFixIt/95622
Let me know if you have any questions.
Offline