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

#1 2012-06-25 09:09:51

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Urgent help needed - spinning script

Hey guys, quick post. I am desperatly looking for a script that will correctly create a rotating object that is controlled by the left and righ arrow keys, I need it to gain speed, and if you stop pressing it, loose speed gradually. Here is a link to my project I need it for. http://scratch.mit.edu/projects/daniel_j/2634226You will notice my script for that failed... If you can fix it please let me know. This project is really important to me and I need it by friday, that i when it is due!! Sorry this post is rushed
Thanks Dan Jones.!


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#2 2012-06-25 09:13:27

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Urgent help needed - spinning script

Rotation Velocity. Gimme a sec, I'll type up a script.


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&display=small

Offline

 

#3 2012-06-25 09:20:56

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Urgent help needed - spinning script

You need 2 variables:

rotationspeed
rotdir

and then, these scripts:

when gf clicked
set [rotdir v] to [1]
forever
 if <key [right arrow v] pressed?>
  set [rotdir v] to [1]
  change [rotationspeed v] by (0.2)
 else
  if <key [left arrow v] pressed?>
   set [rotdir v] to [2]
   change [rotationspeed v] by (-0.2)
  end
 end
end
when gf clicked
forever if <not <(rotationspeed) = [0]>>
 if <(rotdir) = [1]>
  change [rotationspeed v] by (-0.1)
 else
  change [rotationspeed v] by (0.1)
 end
 turn cw (rotationspeed) degrees
end

Last edited by SciTecCf (2012-06-25 09:24:45)


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#4 2012-06-25 09:41:05

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Urgent help needed - spinning script

I would recommend this script.  Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc.  This allows you to perform trigonometric functions accurately.  Adjust the starting values for Friction, Velocity.Max, and Speed, as you like.  They control how fast the object accelerates, how quickly it slows down, and how fast it can go.  Make sure that friction is a positive number less than 1 (if you want it to slow down).  Numbers closer to 0 will cause it to slow down more quickly. 

when gf clicked
set [Friction v] to [0.8] // 0 < Friction < 1, in general.  
set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction.
set [Velocity.R v] to [0] // Rotational velocity.
set [Velocity.Max v] to [15] // Maximum velocity.
set [Acceleration.R v] to [0] // Rotational acceleration.
set [Speed v] to [0.5] // Rotational speed. 
forever
set [Acceleration.R v] to [0]
if <key [right arrow v] pressed?>
change [Acceleration.R v] by ((-1) * (Speed))
end
if <key [left arrow v] pressed?>
change [Acceleration.R v] by ((1) * (Speed))
end
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end
change [Velocity.R v] by (Acceleration.R)
if < ([abs v] of (Velocity.R)) > (Velocity.Max) >
set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R)))
end
change [CurrentDirection v] by (Velocity.R)
set [CurrentDirection v] to ((CurrentDirection) mod (360))
point in direction ((90) - (CurrentDirection))
end

Offline

 

#5 2012-06-25 19:35:45

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

Thanks amcerbu and Scfitech! I will try out both scripts tonight!! i will be sure to add both of you to my credits!!  big_smile  amcerbu, you have been a huge help throughout this project xD thanks!
Dan Jones


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#6 2012-06-25 23:13:35

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

I know nothing about trig, mainly due to my year 9 knownloedge xD


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#7 2012-06-25 23:31:29

sanjayraj
Scratcher
Registered: 2012-03-25
Posts: 500+

Re: Urgent help needed - spinning script

Is this what you need?

when gf clicked
forever
turn (15) degrees
That's a rotation script...


http://i46.tinypic.com/23sw40j.png

Offline

 

#8 2012-06-26 03:17:59

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

sanjayraj wrote:

Is this what you need?

when gf clicked
forever
turn (15) degrees
That's a rotation script...

Hhehe sanjayraj, thanks, but I am looking for a script like the one above ^^ xD thanks anyway!


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#9 2012-06-26 03:19:08

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

amcerbu wrote:

I would recommend this script.  Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc.  This allows you to perform trigonometric functions accurately.  Adjust the starting values for Friction, Velocity.Max, and Speed, as you like.  They control how fast the object accelerates, how quickly it slows down, and how fast it can go.  Make sure that friction is a positive number less than 1 (if you want it to slow down).  Numbers closer to 0 will cause it to slow down more quickly. 

when gf clicked
set [Friction v] to [0.8] // 0 < Friction < 1, in general.  
set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction.
set [Velocity.R v] to [0] // Rotational velocity.
set [Velocity.Max v] to [15] // Maximum velocity.
set [Acceleration.R v] to [0] // Rotational acceleration.
set [Speed v] to [0.5] // Rotational speed. 
forever
set [Acceleration.R v] to [0]
if <key [right arrow v] pressed?>
change [Acceleration.R v] by ((-1) * (Speed))
end
if <key [left arrow v] pressed?>
change [Acceleration.R v] by ((1) * (Speed))
end
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end
change [Velocity.R v] by (Acceleration.R)
if < ([abs v] of (Velocity.R)) > (Velocity.Max) >
set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R)))
end
change [CurrentDirection v] by (Velocity.R)
set [CurrentDirection v] to ((CurrentDirection) mod (360))
point in direction ((90) - (CurrentDirection))
end

Agghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan Jones


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#10 2012-06-26 03:20:35

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: Urgent help needed - spinning script

daniel_j wrote:

amcerbu wrote:

I would recommend this script.  Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc.  This allows you to perform trigonometric functions accurately.  Adjust the starting values for Friction, Velocity.Max, and Speed, as you like.  They control how fast the object accelerates, how quickly it slows down, and how fast it can go.  Make sure that friction is a positive number less than 1 (if you want it to slow down).  Numbers closer to 0 will cause it to slow down more quickly. 

when gf clicked
set [Friction v] to [0.8] // 0 < Friction < 1, in general.  
set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction.
set [Velocity.R v] to [0] // Rotational velocity.
set [Velocity.Max v] to [15] // Maximum velocity.
set [Acceleration.R v] to [0] // Rotational acceleration.
set [Speed v] to [0.5] // Rotational speed. 
forever
set [Acceleration.R v] to [0]
if <key [right arrow v] pressed?>
change [Acceleration.R v] by ((-1) * (Speed))
end
if <key [left arrow v] pressed?>
change [Acceleration.R v] by ((1) * (Speed))
end
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end
change [Velocity.R v] by (Acceleration.R)
if < ([abs v] of (Velocity.R)) > (Velocity.Max) >
set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R)))
end
change [CurrentDirection v] by (Velocity.R)
set [CurrentDirection v] to ((CurrentDirection) mod (360))
point in direction ((90) - (CurrentDirection))
end

Agghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan Jones

Did you try mine?


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#11 2012-06-26 03:21:16

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

Amcerbu, I put the script I did online, can you check it to see what s wrong  big_smile  Thanks mate!
xD

BTW scifitech, your script did the exact same, it did not slow down! xD


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#12 2012-06-26 03:22:42

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

http://www.scratch.mit.edu/projects/daniel_j/2636046
^^ that is the test Amcerbu  big_smile


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#13 2012-06-26 04:15:27

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

omg, amcerbu, I coud not for the love of me get this script working in my game...
If anyone could just download the script for me and add it, I would be so * appreciative! Sorry for all your troubles....
Dan Jones
Thanks


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#14 2012-06-26 04:58:36

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: Urgent help needed - spinning script

Here you go.

I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D

Anyways, dan, here is a very basic rotation script. You can change the (.94) in the script. A smaller fraction will make it slower, a larger fraction will make it very fast.


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#15 2012-06-26 07:05:25

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

Wes64 wrote:

Here you go.

I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D

Anyways, dan, here is a very basic rotation script. You can change the (.94) in the script. A smaller fraction will make it slower, a larger fraction will make it very fast.

Thanks wes64, you amcerbu and scifitech have been huge helps  big_smile


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#16 2012-06-26 07:06:59

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

SciTecCf wrote:

daniel_j wrote:

amcerbu wrote:

I would recommend this script.  Normal direction means that 0 corresponds to pointing right, 90 to pointing up, 180 to pointing left, etc.  This allows you to perform trigonometric functions accurately.  Adjust the starting values for Friction, Velocity.Max, and Speed, as you like.  They control how fast the object accelerates, how quickly it slows down, and how fast it can go.  Make sure that friction is a positive number less than 1 (if you want it to slow down).  Numbers closer to 0 will cause it to slow down more quickly. 

when gf clicked
set [Friction v] to [0.8] // 0 < Friction < 1, in general.  
set [CurrentDirection v] to [0] // "Normal" direction, not Scratch direction.
set [Velocity.R v] to [0] // Rotational velocity.
set [Velocity.Max v] to [15] // Maximum velocity.
set [Acceleration.R v] to [0] // Rotational acceleration.
set [Speed v] to [0.5] // Rotational speed. 
forever
set [Acceleration.R v] to [0]
if <key [right arrow v] pressed?>
change [Acceleration.R v] by ((-1) * (Speed))
end
if <key [left arrow v] pressed?>
change [Acceleration.R v] by ((1) * (Speed))
end
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end
change [Velocity.R v] by (Acceleration.R)
if < ([abs v] of (Velocity.R)) > (Velocity.Max) >
set [Velocity.R v] to ((Velocity.Max) * (([abs v] of (Velocity.R)) / (Velocity.R)))
end
change [CurrentDirection v] by (Velocity.R)
set [CurrentDirection v] to ((CurrentDirection) mod (360))
point in direction ((90) - (CurrentDirection))
end

Agghhhh, I followed your script exactly, and it is awesome, but it does not slow down, it just instantly stops...? I followed your instructions as best as I could..... any ideas?
Thanks
Dan Jones

Did you try mine?

I did  big_smile  very good thankyou!


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#17 2012-06-26 07:08:27

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

You guys are now in my credits  big_smile  thanks!


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#18 2012-06-26 16:05:49

fg123
Scratcher
Registered: 2008-11-13
Posts: 1000+

Re: Urgent help needed - spinning script

Yes, amcerbu has gone a bit overboard with the coding.  lol

Simple rotation velocity like the one posted by SciTecCf is probably enough.  tongue

Last edited by fg123 (2012-06-26 16:05:59)


Hai.

Offline

 

#19 2012-06-26 16:39:43

pipercubjl
Scratcher
Registered: 2010-05-20
Posts: 73

Re: Urgent help needed - spinning script

You ALL are going overboard.

(Look below for proof)


var rot = 0
if right arrow key down
     var rot +1 
     if (var rot)> 0
            var rot -0.25

if right arrow key down
     var rot -1 
     if (var rot)< 0
            var rot +0.25
rotate (var rot) degrees


This is the basic velocity script I always use.

Hope I helped.

Last edited by pipercubjl (2012-06-26 16:40:43)


http://scratch.mit.edu/static/projects/pipercubjl/2621398_med.png  http://scratch.mit.edu/static/projects/pipercubjl/2634368_med.png
        Crypt            Pixel Lands

Offline

 

#20 2012-06-26 17:34:25

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Urgent help needed - spinning script

Wes64 wrote:

Here you go.
I simplified it a ton, amercbu, your script is overly complicated. Remember, not every user is as math-oriented as you. ;D

Yeah, sorry about that daniel_j.  Math is my favorite subject, and I tend to get carried away a bit...

About the script not working:

I think you made a small mistake copying it into Scratch.  That's why it would stop immediately after the keys were released. 

Your program says:

if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Acceleration.R)) // Caught you, bug!
end
Because of previous statements that set Acceleration.R to 0 (and ensure it remains at 0 if no keys are pressed), the object will stop immediately when you let go of the arrows, as Velocity.R will equal 0. 

Here's the fixed version:
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end
If you want, I can upload a version to my test account (although it looks like Wes64 got there first). 

Wes64 is right about the friction variable.  A value fairly close to 1 is often the best (the closer you are, the slower it slows down).  To apply friction, the program sets the velocity to itself multiplied by the friction variable.  A value closer to 1 results in a new velocity closer to the original value.  I would recommend between 0.9 and 0.99 for this, although you should play around with different values to find what you like. 

Hope that helps.


MAJOR EDIT (please read):  Actually, now that I look at it again, I would get rid of the if statement checking to see whether no keys are pressed, and simply apply friction regardless.  That'll look smoother.

So, just
set [Velocity.R v] to ((Friction) * (Velocity.R))
...not...
if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end

Last edited by amcerbu (2012-06-26 17:48:30)

Offline

 

#21 2012-06-26 18:20:58

Wes64
Scratcher
Registered: 2011-08-19
Posts: 1000+

Re: Urgent help needed - spinning script

amcerbu wrote:

if <not<<key [right arrow v] pressed?> or <key [left arrow v] pressed?>>>
set [Velocity.R v] to ((Friction) * (Velocity.R))
end

Here's a little habit of mine, I find it more convenient than the above code,

if <key [right arrow v] pressed?>
change [velocity v] by [-1]
else
if <key [left arrow v] pressed?>
change [velocity v] by [1]
else
set [velocity v] to <(friction) * (velocity)>
end
end


Experienced 2.0 Tester: Ask me questions!
Using Firefox 13.0, Flash plugin version 11.4.402.287, and Windows XP Professional.

Offline

 

#22 2012-06-26 18:26:33

ubrecma
Scratcher
Registered: 2012-05-12
Posts: 2

Re: Urgent help needed - spinning script

^^ The only problem with that script is that if both keys are pressed, the program will see a right keypress and continue accelerating right, rather than slowing down.  It turns out to work more smoothly if friction is applied constantly, without waiting until the arrow keys aren't pressed.

Offline

 

#23 2012-06-26 18:31:28

amcerbu
Scratcher
Registered: 2009-07-21
Posts: 500+

Re: Urgent help needed - spinning script

^^ Whoops, accidentally posted from the test account.

Offline

 

#24 2012-06-26 21:18:23

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

pipercubjl wrote:

You ALL are going overboard.

(Look below for proof)


var rot = 0
if right arrow key down
     var rot +1 
     if (var rot)> 0
            var rot -0.25

if right arrow key down
     var rot -1 
     if (var rot)< 0
            var rot +0.25
rotate (var rot) degrees


This is the basic velocity script I always use.
Thanks  big_smile

Hope I helped.


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

#25 2012-06-26 21:20:02

daniel_j
Scratcher
Registered: 2012-05-22
Posts: 100+

Re: Urgent help needed - spinning script

Thanks everyone  big_smile  i got it working!!! yeah, I copied it wrong... sorry about that!! great scripts everyone!!
Thanks
Dan Jones


http://i50.tinypic.com/2dhgnsx.jpg

Offline

 

Board footer