Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » Speed
- barberl19
-
17 posts
Speed
I need to know how i can make it so that a sprite detects the rate of change and uses it to calculate the “speed”.. I was thinking about using timers and variables.
- lafoudre
-
100+ posts
Speed
You are right. If you record the previous x position and previous timer value, you can calculate the speed in x direction of the period since your records as follows:
(((x position) - (last x pos)) / ((timer) - (last timer val)))If you want the speed, you must compute the distance between the last point (defned by the last x position and the last y position) and the current position (defined with x position and y position). I guess you know the formula to compute the distance between two points. The one with the square root of the square of difference of … ;-)
- lafoudre
-
100+ posts
Speed
I forgot to tell you a simpler solution:
just set a hidden dummy sprite to the last position of your main sprite.
Then when you want to compute the speed, you just have to do this:
just set a hidden dummy sprite to the last position of your main sprite.
Then when you want to compute the speed, you just have to do this:
((distance to [hidden dummy sprite v]) / ((timer) - (last timer val)))
- peppermintpatty5
-
1000+ posts
Speed
Okay, you seem to be on the right track. Try this script here:
when [space v] key pressed
set [old x v] to (x position)
set [old y v] to (y position)
reset timer
wait until <not <key [space v] pressed?>>
wait until <key [space v] pressed?>
set [new x v] to (x position)
set [new y v] to (y position)
set [time v] to (timer)
set [x distance v] to ((new x) - (old x))
set [y distance v] to ((new y) - (old y))
set [distance v] to ([sqrt v] of ((((x distance)*(x distance)) + ((y distance) * (y distance))))
set [speed v] to ((distance) / (time))
say (join [Speed: ] (join (speed) [units per second])
Last edited by peppermintpatty5 (Nov. 25, 2014 21:31:36)
- barberl19
-
17 posts
Speed
I was also wondering how to make it so that it wouldnt be randomized with which direction the puck went when hit (im making an air hockey game) , for example, if on left side, then hit at a 45 degree angle to the left, instead of randomly going in an direction or to any direction.
- barberl19
-
17 posts
Speed
What is the difference between that one and the previous one shown to me by lafoudre
- peppermintpatty5
-
1000+ posts
Speed
The other one only calculates in the x direction. Please note that you don't have to use the spacebar start/stop part of the script. What is the difference between that one and the previous one shown to me by lafoudre
VERY IMPORTANT EDIT: The speed will not be accurate if the sprite bounces off a wall and returns close to its original spot.
Last edited by peppermintpatty5 (Nov. 25, 2014 21:32:55)
- peppermintpatty5
-
1000+ posts
Speed
Yes, I can help with that too. If your “paddle” is on the left side of the screen: Okay, do you know the answer to my last question?
if <touching [paddle v] ?>If your “paddle” is on the right side of the screen:
point in direction (((([y position v] of [paddle v]) - ([y position v] of [puck v])) * (1.5)) + (90))
end
if <touching [paddle v] ?>
point in direction (((([y position v] of [paddle v]) - ([y position v] of [puck v])) * (-1.5)) - (90))
end
- barberl19
-
17 posts
Speed
its in the midde. What do I do if i find myself in that situation? its 3D. Here, ill show you. Look at my untitled project, ill share it as a draft.
- peppermintpatty5
-
1000+ posts
Speed
Hmm. The game looks good by the way, and the speed calculator seemed to work as well. its in the midde. What do I do if i find myself in that situation? its 3D. Here, ill show you. Look at my untitled project, ill share it as a draft.

when green flag clicked
go to x:(15) y:(0)
set size to (50)%
go to front
go back (1) layers
forever
if <<touching [Sprite5 v]?> and <([direction v] of [Sprite5 v]) = [90]>> then
broadcast [message 2 v]
repeat until <touching [Sprite2 v]?>
move ((Overall Speed) / (4)) steps
change size by (-0.1)
end
set [Overall Speed v] to [0]
repeat until <touching [Sprite5 v]?>
change y by (-5)
change size by (0.1)
end
end
end
Last edited by peppermintpatty5 (Nov. 27, 2014 01:16:44)
- Discussion Forums
- » Help with Scripts
-
» Speed