Pages: 1
Topic closed
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?
Offline
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
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
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) endEdit: this creates a parabola
Last edited by MoreGamesNow (2012-04-09 15:32:42)
Offline
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
Offline
I know you said you know how to use trig, but for Scratch, the sine and cosine are reverse from normal geometry. So:
xvel = force * sin (angle) yvel = force * cos (angle)
Offline
O.K., but what is "force," and how do I symbolize it in SCRATCH?
Offline
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)
Offline
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?
Offline
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
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
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
Offline
What happen if the walls are slanted, or curved? :S
Offline
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 ) 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)
Offline
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.
Offline
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]
Offline
I'll try that, but can you explain the loop it got stuck in?
Offline
I believe it is getting stuck in the original forever loop (that gets activated when the green flag is clicked).
Offline
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
Offline
Looks good. Making the wall thicker should get rid of the occasional "pass through wall" glitch.
Offline
Topic closed
Pages: 1