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

#1 2012-04-09 15:03:05

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Help with parabolas, sine, cosine, and tangents in SCRATCH

I do NOT need a trigonometry lesson (I know the definitions of sin, cos, and tan), but I need help learning how to actually use them in SCRATCH.

I've downloaded many games in which scripts involving trigonometry are used, but do not understand how they work/what they do. For example, I see many cannon-shooting games and stuff like that with sine and cosine, and the sine and cosine creates a natural arc. But, I do not understand how to use it for my own games, some of which I want to create using natural arcs.

So, can anyone please help me?


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#2 2012-04-09 15:04:33

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

I don't understand it either but you can just copy the scripts from that other game you were looking at and mess around with it until you get the feel for it, and, if you don't feel like copying, after you have the feel for it create your own script.

Offline

 

#3 2012-04-09 15:22:20

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

It was kind of difficult to adapt for me too, coming from triangle trigonometry, and having to apply it to Scratch's directions and circles. What worked best for me was taking pen and paper and imagining triangles all over the screen, and then working out the right formulae and putting them into my scripts.

TobyFork is right though about trying to copy out the trig scripts from other projects and playing around with them and see what the differences are between your classic knowledge of trig and how Scratch applies it!

Offline

 

#4 2012-04-09 15:31:41

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

For cannon shooting games, I find using x-vel and y-vel to be the simplest (with a constant x-vel and a changing y-vel*).  Finding the initial values for these can be done using trig, but after that initial trig, you really don't need any.

set [x-vel v] to (initial x velocity) // use trig
set [y-vel v] to (initial y velocity) // use trig
repeat until <touching [ground v]?>
change x by (x-vel)
change y by (y-vel)
change [y-vel v] by (-.5)
end
Edit: this creates a parabola

Last edited by MoreGamesNow (2012-04-09 15:32:42)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#5 2012-04-09 15:47:47

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

If I may add to what MoreGamesNow has said above, to find the x and y initial velocities, think about the two velocities being like the two legs of a triangle. The hypotenuse would be the initial strength and alpha would be the angle of the cannon.
    /|
  /  |
/___|
That's what the triangle would look like approximately  tongue

Offline

 

#6 2012-04-09 16:40:09

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

I know you said you know how to use trig, but for Scratch, the sine and cosine are reverse from normal geometry. So:

Code:

xvel = force * sin (angle)
yvel = force * cos (angle)

http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#7 2012-04-09 17:29:26

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

O.K., but what is "force," and how do I symbolize it in SCRATCH?


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#8 2012-04-09 17:32:50

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

mythbusteranimator wrote:

O.K., but what is "force," and how do I symbolize it in SCRATCH?

Force is just a multiplier.  The force behind the projectile, just set it to any positive number.  You can think of "force" as "power" if you want.

set [xvel v] to ((force)*([sin v] of (direction)))
set [yvel v] to ((force)*([cos v] of (direction)))
repeat until <touching [edge v]?>
change x by (xvel)
change y by (yvel)
change [yvel v] by (-1)
end

Last edited by MoreGamesNow (2012-04-09 17:33:26)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#9 2012-04-10 09:21:06

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

Thanks! That helped a lot!


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#10 2012-04-10 12:22:48

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

I'm going to create a game using this (Archery 4) involving bouncing off of walls. Any suggestions involving the bounce off walls and the angles involved? Or is it just

if <touching [wall]
 set [xvel] to (xvel * -1)
 change x by (xvel)
 change y by (yvel)
 
Is that all I need?


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#11 2012-04-10 12:26:20

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

mythbusteranimator wrote:

I'm going to create a game using this (Archery 4) involving bouncing off of walls. Any suggestions involving the bounce off walls and the angles involved? Or is it just

if <touching [wall]
 set [xvel] to (xvel * -1)
 change x by (xvel)
 change y by (yvel)
 
Is that all I need?

If the wall is perfectly vertical, I suppose so...

Offline

 

#12 2012-04-10 12:29:09

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

As LS97 said, if the walls are at right angles, you can simply reverse velocities (see script below).  Otherwise, you'll need to know the angle of the wall  hmm

forever
change x by (xvel)
if<touching [wall v]?>
set [xvel v] to ([-1]*(xvel))
end
change y by (yvel)
if<touching [wall v]?>
set [yvel v] to ([-1]*(yvel))
end
end


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#13 2012-04-10 12:51:43

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

What happen if the walls are slanted, or curved? :S


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#14 2012-04-10 13:27:23

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

You first have to find out the slope of the wall at the point they collided.  This is pretty much impossible to find with any amount of accuracy.  You could make the walls different colors (visually unappealing  hmm ) or try to approximate it with sensing (a special sensing costume perhaps?).  Either way, it will be challenging.  After that, some trig should do it (you always bounce at the same angle, relative to the wall, as you approached it at).

Last edited by MoreGamesNow (2012-04-10 13:28:46)


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#15 2012-04-10 17:02:53

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

OK, I just finished Archery 4 with bouncing, but it gets stuck with a loop where the ball keeps falling. I suspect I did something wrong with my variable scripting. Can you check it out and tell me what the problem is? It bounces fine (thanks for the script) and it travels at reasonable speed. If i get this bug fixed, the game is finished. I just can't find a solution to the bug. I'm not very good at diagnosing problems.  smile


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#16 2012-04-10 17:42:13

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

This script should work (goes in the ball/arrow).

when gf clicked
set [power v] to [0]
set [x-vel v] to [0]
set [y-vel v] to [0]
go to x: [-64] y: [-14]
forever
repeat until <mouse down?>
point towards [mouse-pointer v]
if<<(direction) > [135]> or <(direction) < [45]>>
point in direction [90 v]
end
end
set [power v] to ((distance to [mouse-pointer v])/[8])
set [x-vel v] to ((power)*([sin v] of (direction)))
set [y-vel v] to ((power)*([sin v] of (direction)))
repeat until<<[-160] > ([y position v] of [Ball v])> or <touching [target v]?>>
change x by (x-vel)
change y by (y-vel)
change [y-vel v] by (-1)
if<touching [wall v]?>
set [x-vel v] to ((x-vel)*[-0.5])
change x by (x-vel)
change y by (y-vel)
end
end
if<touching [target v]?>
if<touching color [blue]?>
change [SCORE v] by (20)
else
if<touching color [green]?>
change [SCORE v] by (10)
else
change [SCORE v] by (5)
end
end
end
go to x: [-64] y: [-14]
set [x-vel v] to [0]
set [y-vel v] to [0]
set [power v] to [0]


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#17 2012-04-11 09:17:22

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

I'll try that, but can you explain the loop it got stuck in?


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#18 2012-04-11 10:06:50

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

I believe it is getting stuck in the original forever loop (that gets activated when the green flag is clicked).


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#19 2012-04-11 10:11:37

mythbusteranimator
Scratcher
Registered: 2012-02-28
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

Never mind--I tried your script and it worked. It just took a couple of broadcasts. Thank you! http://scratch.mit.edu/projects/mythbusteranimator/2461988


http://www.foxtrot.com/comics/2012-04-01-fdb37077.gif
clicky

Offline

 

#20 2012-04-11 11:49:37

MoreGamesNow
Scratcher
Registered: 2009-10-12
Posts: 1000+

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

Looks good.  Making the wall thicker should get rid of the occasional "pass through wall" glitch.  smile


http://images2.layoutsparks.com/1/218929/rubiks-cube-animated-rotating.gif
"Cogito ergo sum" --  I think, therefore I am

Offline

 

#21 2012-04-21 11:13:47

qwerytiop
New Scratcher
Registered: 2012-04-09
Posts: 5

Re: Help with parabolas, sine, cosine, and tangents in SCRATCH

Using trigonometry in virtual coordinates is very useful and adds the bouncing effect

Offline

 

Board footer