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

#1 2012-06-14 14:29:52

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Movement for directional gravity

My project, Circle in a square world, has directional gravity.  I need the square able to move.  Any suggestions?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#2 2012-06-14 17:50:49

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: Movement for directional gravity

I'm going to create a quick project and give you a link when I finish to show you a way I came up with.


http://i48.tinypic.com/2072ctw.gif

Offline

 

#3 2012-06-15 12:34:25

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

I have the gravity but not the movement.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#4 2012-06-15 13:32:05

powerpoint56
Scratcher
Registered: 2012-04-19
Posts: 500+

Re: Movement for directional gravity

Yikes! I can't figure this out! I mean, If the circle is completely round, I suppose you could do something like this... Have your sprite face up the way you want it when it's pointed 180 degrees.

when gf clicked
Point in direction (180)
forever
if <key [left arrow v] pressed?>
Turn (-2) degrees
Move (-3) steps
if <key [right arrow v] pressed?>
move (3) steps
Turn (2) degrees
end
I don't know exactly how to do a jumping thing. Maybe add this to the other script for the square...

when gf clicked
forever
if <key up arrow v] pressed?>
repeat (10)
move -(10) steps
if <not<touching [world v]?>>
reapeat until <touching [world v]
move (10) steps
But there's an issue with this. When you tap the left/right arrow keys, it basically does a tiny jump. EEESH... I dunno. You could always make an invisible sprite that does this...
when gf clicked
hide
point in direction 90
forever
if <key [left arrow] pressed?>
turn [-2] degrees
move (-3) steps
if <key [right arrow] pressed?>
turn [2] degrees
move (3) steps
end
Call this, I don't know - "mover". Then just add to the main square, "when key left arrow or key right arrow pressed, go to [mover].

OK. Then remove the other left/right moving scripts from earlier. This is pretty messy - sorry.

Last edited by powerpoint56 (2012-06-24 16:52:41)


http://i48.tinypic.com/2072ctw.gif

Offline

 

#5 2012-06-15 18:07:41

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

So can you just post the singular script w/ instructions?  I greatly appreciate your help.

~~Firedrake969


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#6 2012-06-16 01:12:35

AngstromDog
Scratcher
Registered: 2009-06-16
Posts: 6

Re: Movement for directional gravity

If you don't mind trig...
set a variable for gravity direction (gravdirec in example) as well as use a variable for gravity (or just hardcode a number in place of the (gravity) variable. also have variables for storing x and y velocities like xvel and yvel

when gf clicked
forever
if <key [left arrow] pressed>
change [gravdirec v] by (3)
end
if <key [right arrow] pressed>
change [gravdirec v] by (-3)
end
change [xvel v] by ((gravity)  x  [cos] of  (gravdirec))
change [yvel v] by ((gravity) x ([sin] of (gravdirec))
change x by (xvel)
change y by (yvel)
end
i couldnt get the change xvel and yvels to show up right so


change [xvel] by ((gravity)  x  [cos] of  (gravdirec))
change [yvel] by ((gravity) x ([sin] of (gravdirec))

Offline

 

#7 2012-06-16 03:00:29

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

Re: Movement for directional gravity

AngstromDog wrote:

when gf clicked
forever
if <key [left arrow v] pressed?>
change [gravdirec v] by (3)
end
if <key [right arrow v] pressed?>
change [gravdirec v] by (-3)
end
change [xvel v] by <(gravity)  *  ([cos v] of  (gravdirec))>
change [yvel v] by <(gravity) * ([sin v] of (gravdirec))>
change x by (xvel)
change y by (yvel)

Fixed


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

Offline

 

#8 2012-06-16 06:46:21

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

Re: Movement for directional gravity

You don't actually need to use trig.  For the sake of computation speed (trigonometric functions cause a lot of overhead in every language), I would suggest that you use vector math. 

Here's how it's done:

Find the vector connecting the center of the player to the center of the planet by subtracting Player.X - Planet.X and Player.Y - Planet.Y.

Divide both components by the vector's magnitude (length) to give the unit vector.

Multiply the vector (both components) by your gravitational constant. 

Add the vector to the velocity of the player. 


I have a rather buggy example (I never took the time to deal with collisions with the planets) on my test account that supports multiple planets. 

Planet Gravity.

Offline

 

#9 2012-06-18 09:39:57

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

Re: Movement for directional gravity

Firedrake969- Are you still interested in this topic?

Offline

 

#10 2012-06-18 20:18:57

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

Yes.  Definitely.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#11 2012-06-19 10:57:12

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

Re: Movement for directional gravity

If you don't want to deal with something unnecessarily complicated (like my post above, #8), check out some of zubblewu's planetary gravity things.  The code is pretty straightforward.  You can also look at a remix of one of his projects that I uploaded to my test account.  I would recommend checking out that link, since the project works pretty well.  I didn't even have to use color or sprite sensing, just distance checking.

Offline

 

#12 2012-06-19 18:10:20

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

Is it possible to write out the script here?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#13 2012-06-20 16:10:30

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

Re: Movement for directional gravity

Okay, so there's something we need to talk about before I (or anyone else) writes out a script.

How physically accurate do you want this simulation to be?  Zubblewu's planet projects, though cool, weren't quite accurate.  The velocity variable in those expressed a change in direction; the direction of the line drawn from the center of the circle to the player.  Now, if you only talk about the velocity at which that line is turning, you don't consider that if the player is further away from the circle, the same rotation will move him further (imagine putting something on the end of a long stick and rotating the stick 1 degree per second).

Last edited by amcerbu (2012-06-20 16:10:56)

Offline

 

#14 2012-06-20 18:29:18

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

Re: Movement for directional gravity

amcerbu wrote:

Okay, so there's something we need to talk about before I (or anyone else) writes out a script.

How physically accurate do you want this simulation to be?  Zubblewu's planet projects, though cool, weren't quite accurate.  The velocity variable in those expressed a change in direction; the direction of the line drawn from the center of the circle to the player.  Now, if you only talk about the velocity at which that line is turning, you don't consider that if the player is further away from the circle, the same rotation will move him further (imagine putting something on the end of a long stick and rotating the stick 1 degree per second).

I think that might be a bit overboard. He doesn't even know how to do basic directional  gravity (hence the existence of this topic) so true physics might be a bit... much.


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-22 17:11:43

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

Wes64 wrote:

amcerbu wrote:

Okay, so there's something we need to talk about before I (or anyone else) writes out a script.

How physically accurate do you want this simulation to be?  Zubblewu's planet projects, though cool, weren't quite accurate.  The velocity variable in those expressed a change in direction; the direction of the line drawn from the center of the circle to the player.  Now, if you only talk about the velocity at which that line is turning, you don't consider that if the player is further away from the circle, the same rotation will move him further (imagine putting something on the end of a long stick and rotating the stick 1 degree per second).

I think that might be a bit overboard. He doesn't even know how to do basic directional  gravity (hence the existence of this topic) so true physics might be a bit... much.

I want it to be fairly accurate, so that the player does not move off of the circle when moving.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#16 2012-06-22 19:16:25

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

Re: Movement for directional gravity

Wes64 wrote:

I think that might be a bit overboard. He doesn't even know how to do basic directional  gravity (hence the existence of this topic) so true physics might be a bit... much.

True... I'll just post the generic script for directional gravity.

Offline

 

#17 2012-06-22 19:49:14

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

Re: Movement for directional gravity

This particular version is 1s1s, but it could be adapted to work for a system that uses multiple sprites.  To increase the accuracy of the simulation, it doesn't use the "touching color" or "touching sprite" sensors; everything is done by measuring distance.  Warning: this has not been tested yet, but I feel fairly confident it will work (some of the code was inspired by zubblewu's planet projects). 

when gf clicked
hide
clear
set [Planet.Radius v] to [value] // These statements initialize variables.  
set [Square.Size v] to [value] // Length/width number for the square costume.  
set [Planet.X v] to [0] // Center position of planet. 
set [Planet.Y v] to [0]
set [Velocity.Y v] to [0]
set [Velocity.R v] to [0] // Rotational velocity.  
set [Rotation v] to [0] // Straight up.  
set [Distance v] to ((Planet.Radius) + (Square.Size)) // A little above planet. 
set [MinimumDistance v] to ((Planet.Radius) + ((Square.Size) / (2)))
set [Friction v] to [0.8]
set [Gravity v] to [-0.1]
set [Jump v] to [4]
set [Speed v] to [1]
forever
set [Square.X v] to (([cos v] of (Rotation)) * (Distance))
set [Square.Y v] to (([sin v] of (Rotation)) * (Distance))
clear
go to x: (Square.X) y: (Square.Y) // Drawing the square and circle.
switch to costume [Square v]
point in direction ((90) - (Rotation))
stamp
go to x: (Planet.X) y: (Planet.Y) // The center of the planet.
switch to costume [Planet v]
point in direction (90 v)
stamp
set [Rotation v] to ((Rotation) mod (360))
set [Velocity.R v] to ((Velocity.R) * (Friction))
change [Rotation v] by (Velocity.R)
change [Distance v] by (Velocity.Y)
if <(Distance) < (MinimumDistance)> // Collision with planet.  
set [Distance v] to (MinimumDistance)
set [Velocity.Y v] to (0)
else
change [Velocity.Y v] by (Gravity)
end
if <key [right arrow v] pressed?> // Arrow key responses.  
change [Velocity.R v] by ((-1) * (Speed))
end
if <key [left arrow v] pressed?>
change [Velocity.R v] by (Speed)
end
if <<key [up arrow v] pressed?> and <(Distance) = (MinimumDistance)>>
set [Velocity.Y v] to (Jump)
end

Last edited by amcerbu (2012-06-25 18:14:07)

Offline

 

#18 2012-06-25 18:15:11

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

Re: Movement for directional gravity

...bumping the above post (#17).  Firedrake, you requested the script.  Here it is ^^

Offline

 

#19 2012-06-26 16:12:50

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

Re: Movement for directional gravity

You, sir, deserve a medal (amcerbu). I'm impressed with your script, and I'm impressed that you took the time to figure out how to do it in Scratch Blocks. xDD

I just write it in scratch and take a screenshot.  lol


Hai.

Offline

 

#20 2012-06-26 17:24:32

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

Re: Movement for directional gravity

^^ Haha, thanks.  I do my best to help Scratchers code in a way that allows easy prototyping: for example, storing constants in variables, rather than writing them out manually each time, consolidating functions into a single script like this one, etc. 

But yeah, sometimes I do go overboard (like on that other thread)...

Offline

 

#21 2012-06-26 22:11:51

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

Would AngstromDog be as good as that one?  I don't like too many variables in a script.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#22 2012-06-27 01:24:04

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

Re: Movement for directional gravity

Variables are deathly useful. Trust me, you'll love variables once you really get into coding.  smile

Variables are not a problem, because you can simply use local variables (click for this sprite only, with creating a variable) and all of the variables won't show up on the blocks bar, unless you're actively on the sprite.


Hai.

Offline

 

#23 2012-06-27 14:35:23

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

Re: Movement for directional gravity

Firedrake969 wrote:

Would AngstromDog be as good as that one?  I don't like too many variables in a script.

I tried out AngstromDog's script, and I don't think it's what you're looking for.  You want to make "planet gravity," right?  In which a character walks around a circular planet?

And yes, fg123 is right: Variables are deathly useful.  If/when you start programming in another language, you'll see that variables are everything.  The main difference about Scratch is that variables can't go "out of scope," you can't make variables local to a script, etc.

Last edited by amcerbu (2012-06-27 14:54:18)

Offline

 

#24 2012-06-27 17:01:29

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: Movement for directional gravity

This will take a while, but I'll do that.


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#25 2012-06-28 11:02:20

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

Re: Movement for directional gravity

Okay.  Some of the variables aren't completely necessary in my script, but they make your life simpler when you want to play around with different "qualities."  For example, how fast the player goes, how quickly he slows down, the size of the planet, the location of the planet, etc.  There's an idea in programming that you should avoid "magic constants."  Check out this article.

Offline

 

Board footer