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

#1 2013-02-04 12:28:15

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Rolling ball

I'm trying to make a game where you draw a line and then you press space and a ball falls from the top left of the screen and rolls along it realistically. There will then be a tub that the ball must fall into. I am doing this on byob but (for people who haven't used it) it contains all the blocks in scratch. I've tried several times from different aproaches but can never get it. The drawing bit is easy, but can someone please help with the inetaction of the ball and the line.

Offline

 

#2 2013-02-04 13:51:13

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Rolling ball

Well, here goes my guess.

You need to put it so that you have a variable that's named "is_Rolling?",
then you need to have it roll if "is_Rolling?" is equal to yes.

You need to also specify of its rolling in the -x direction to roll counter clockwise. you can use a Boolean variable for that.

so, here's an example:

 
when gf clicked
forever if < <[is_Rolling?]> = <[yes]> >
turn cw (15) degrees // or how ever much you want.
end 

Then, make this script:

 
when i receive [start_Ball v]
set [is_Rolling? v] to (yes)

Once the ball is stopped, you then need to set "is_Rolling?" to no.
 
when i receive [ball_Stopped v]
set [is_Rolling? v] to (no)

Now, about the following the line, i really don't yet know how to make it stop falling, but here's an idea:

 
when gf clicked
change y by (-5)
Then, this:

 
when gf clicked
forever if < <touching [line v] ?> > // you also need to specify "tub" with an "or" operator.
change y by (5)
You could also use another variable and make a script that sets "touching_Line?"
to "yes" if touching it and then use a repeat instead of a forever if.
Now as for the physics of making it fall along the line in the x and y, i have no idea, and my whole "change y by (5)" thing would make it just stop, you would have to build some kind of physics engine or get one off of scratch.


Hope that helps,


CAA14

Offline

 

#3 2013-02-04 13:57:18

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Re: Rolling ball

Thanks CAA14, what about a way to detect which side of the ball the line is on, I tried making lists of all th x and y coordinates of the line but that didn't work, any suggestions for that because if I can get that I can make it move along, just without momentum but I can add that in later?

Offline

 

#4 2013-02-05 10:27:03

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Rolling ball

Hmmmm.....
Could you clarify a little?
When you say, "Which side of the ball the line is on", do you mean which way the ball is facing on the line?

If that's what you mean, then try this:


if < <[direction] = [90]> > // right.
your script here
end
if < <[direction] = [-90]> > // left.
your script here
end
That (In theory,) should work.

But, i just thought of what you might mean instead... Your algorithm is to have the line tell the ball what side it's touching, and that would determine which way it rolls, right?
If so, let me know and I'll think about a solution.  smile 
Regards,

CAA14

Offline

 

#5 2013-02-05 11:24:55

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Re: Rolling ball

What I mean is:
Imagine that the line is in a mountain shape, if you place te ball on the top, it wouldn't move, but if it was slightly to one side it would start to roll down the mountain. My problem is that a script that would make it roll down one side of the mountain, may not necessarily make it roll down the other side. The problem is basically working out what is a left-facing slope as opposed to a right-facing slope.
Thanks for any help in advance.

Offline

 

#6 2013-02-05 19:18:48

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Rolling ball

Hmmm.... I'm not totally sure how to do that... That sounds like something you'd do in a 3d editor.

Well, here's my last idea:

You need to make a variable called "ground_Level" or something like that.

Then set it's value to the "floor" y concordance.

Then have a script like this:

when gf clicked
forever
if < [y position] = [ground_Level] >
change y by (10)
end
end
when gf clicked
forever
if < not < [y position] = [ground_Level]> >
change y by (-10)
end
end
This will get it to fall. Now about having it follow the line, you might try to do the same as the first block, Only 30 y instead of 10. Though this is all a guess as scratch doesn't have (as far as i know,) a good way to do physics.

If that doesn't make it work, which it might, then you'll have to check the direction and tell it to move in the x direction when facing right, and -x direction if facing left (I think, maybe the other way around).


Well, as far as getting it to move in a direction based off the slant of the line, i have no idea... Except for maybe some of the higher math functions, Sorry.  sad

I hope that works or is at least some help,

CAA14

Offline

 

#7 2013-02-05 21:30:31

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

Re: Rolling ball

CAA14 wrote:

Hmmm.... I'm not totally sure how to do that... That sounds like something you'd do in a 3d editor.

Well, here's my last idea:

You need to make a variable called "ground_Level" or something like that.

Then set it's value to the "floor" y concordance.

Then have a script like this:

when gf clicked
forever
if < (y position) = [ground_Level] >
change y by (10)
end
when gf clicked
forever
if < not < (y position)= [ground_Level]> >
change y by (-10)
end
CAA14

Fixed a bit.


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

Offline

 

#8 2013-02-06 11:34:42

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Re: Rolling ball

I was thinking something along the lines of: you draw a line and all the x and y coordiantes of the line go into a list (one for x and one for y) then the computer works out the angles between them. Then if the ball is touching the line, it works out which bit of the line it is touching (and therefore what angles) and bounces off it. The bouncing is trigonometry and momentum which isn't that hard, relatively, but the angles and detecting which part of the line has been touched is difficult.

Offline

 

#9 2013-02-06 12:50:52

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Rolling ball

Thanks for fixing my blocks, Ernie.

Well, I don't know trig, so I can help you there.  sad
Hopefully you or someone else can think of something that works, because I don't have any other ideas...sorry.  sad

Regards,

CAA14

Offline

 

#10 2013-02-06 14:47:31

shadowmouse
New Scratcher
Registered: 2013-02-03
Posts: 100+

Re: Rolling ball

Thanks CAA14, I've managed to get my maths teacher to talk me through trigonometry and vector projection, though it's not exactly easy, so I should be O.K. But if anyone does have an idea please post because I'm intending on spending ages doing this and making it look really good.

Offline

 

Board footer