This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2008-02-04 13:33:33

danp357
Scratcher
Registered: 2007-11-11
Posts: 7

Trig Distances

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.


Am I supposed to write something here? I guess I just did....

Offline

 

#2 2008-02-04 14:12:11

chalkmarrow
Scratcher
Registered: 2007-05-18
Posts: 100+

Re: Trig Distances

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

 

#3 2008-02-04 17:13:03

danp357
Scratcher
Registered: 2007-11-11
Posts: 7

Re: Trig Distances

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.


Am I supposed to write something here? I guess I just did....

Offline

 

#4 2008-02-04 18:48:50

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Trig Distances

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.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#5 2008-02-08 17:40:18

danp357
Scratcher
Registered: 2007-11-11
Posts: 7

Re: Trig Distances

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.


Am I supposed to write something here? I guess I just did....

Offline

 

#6 2008-02-08 18:45:57

Paddle2See
Scratch Team
Registered: 2007-10-27
Posts: 1000+

Re: Trig Distances

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.


http://i39.tinypic.com/2nav6o7.gif

Offline

 

#7 2008-02-09 17:28:53

danp357
Scratcher
Registered: 2007-11-11
Posts: 7

Re: Trig Distances

Thanks for the info, Paddle2See. If I do have any questions, they'll be here.


Am I supposed to write something here? I guess I just did....

Offline

 

Board footer