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

#1 2013-03-22 08:45:10

troyk
New Scratcher
Registered: 2013-02-06
Posts: 14

Distance Forumula in code - Not consistently working

So here is my formula to first calculate the distance from the original position of the sprite from the position it is told to go to (mouse click). I then put it into a code that basically takes that distance and divides it by 62.5 (that is the speed I want my sprite to move at , 62.5 units/second). Can someone help me figure out what is wrong and why at some positions it goes much much to slow or waaaay to fast and sometimes works perfectly.


when I receive [Move v]

set [DistanceVariable v] to ( [abs v] of ( [sqrt v] of ((((MouseX) - ( [x position v] of [Sprite3 v] )) * ((MouseX) - ( [x position v] of [Sprite3 v] ))) + (((MouseY) - ( [y position v] of [Sprite3 v] )) * ((MouseY) - ( [y position v] of [Sprite3 v] ))))))

Offline

 

#2 2013-03-22 19:39:30

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Distance Forumula in code - Not consistently working

troyk wrote:

So here is my formula to first calculate the distance from the original position of the sprite from the position it is told to go to (mouse click). I then put it into a code that basically takes that distance and divides it by 62.5 (that is the speed I want my sprite to move at , 62.5 units/second). Can someone help me figure out what is wrong and why at some positions it goes much much to slow or waaaay to fast and sometimes works perfectly.


when I receive [Move v]

set [DistanceVariable v] to ( [abs v] of ( [sqrt v] of ((((MouseX) - ( [x position v] of [Sprite3 v] )) * ((MouseX) - ( [x position v] of [Sprite3 v] ))) + (((MouseY) - ( [y position v] of [Sprite3 v] )) * ((MouseY) - ( [y position v] of [Sprite3 v] ))))))

First of all, you don't need the absolute block as the sqrt function will always give you a positive answer. Secondly, could I see your movement script? The problem might be in there.

With regards,

ErnieParke

Last edited by ErnieParke (2013-03-23 11:49:53)


http://i46.tinypic.com/35ismmc.png

Offline

 

#3 2013-03-23 01:58:28

kayybee
Scratcher
Registered: 2009-12-07
Posts: 1000+

Re: Distance Forumula in code - Not consistently working

ErnieParke wrote:

First of all, you don't need the abstract block as the sqrt function will always give you a positive answer. Secondly, could I see your movement script? The problem might be in there.

With regards,

ErnieParke

Abstract block? The abs?
That means absolute value...

Offline

 

#4 2013-03-23 11:49:41

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Distance Forumula in code - Not consistently working

kayybee wrote:

ErnieParke wrote:

First of all, you don't need the abstract block as the sqrt function will always give you a positive answer. Secondly, could I see your movement script? The problem might be in there.

With regards,

ErnieParke

Abstract block? The abs?
That means absolute value...

Oh... Yes, you're correct; my mistake. Well thanks for catching it!  smile

With regards,

ErnieParke


http://i46.tinypic.com/35ismmc.png

Offline

 

#5 2013-04-18 00:04:58

dinosaur1995
New Scratcher
Registered: 2013-04-18
Posts: 8

Re: Distance Forumula in code - Not consistently working

what if when you are trying to find the distance between 2 mouse clicks?

Offline

 

#6 2013-04-18 06:38:15

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Distance Forumula in code - Not consistently working

dinosaur1995 wrote:

what if when you are trying to find the distance between 2 mouse clicks?

Well for that, all you need to do is store your previous mouse click in two variables, (P Mouse Click X) and (P Mouse Click Y), and whenever you click, you can then use the distance formula, though make sure that you record the mouse's click position only after that has occurred or else you'll keep on getting a distance of 0.

So, for example:

if (mouse down?)
 set [distance v] to ([sqrt v] of ((((P Mouse Click X) - (Mouse X)) * ((P Mouse Click X) - (Mouse X)))  * (((P Mouse Click Y) - (Mouse Y)) * ((P Mouse Click Y) - (Mouse Y)))))
 set [P Mouse Click X v] to (Mouse X)
 set [P Mouse Click Y v] to (Mouse Y)
 wait until <not (mouse down?)>
end

I hope that this helps!

With regards,

ErnieParke

Last edited by ErnieParke (2013-04-18 06:38:30)


http://i46.tinypic.com/35ismmc.png

Offline

 

Board footer