Hey guys, I am posting this topic here because i am in need of semi-urgent help for my scratch project. Basically, My school attended scratch day 2012, and we were set with a competition, a simple one, to make a game, but the only catch is it needs to include a type of 'force'. i have chosen gravity, and i have developed a preety good script for it (ask if you would like to see it). But, now i am stumped, i am creating a kind of missile game, the aim is to shoot the missile up into the sky at an angle, and i would like the missile to come down in a curve, just like in real life. And for the love of god i can not figure out how to do this, so any help will be appreciated!
Thanks
Dan Jones
P.S the winner of the Competition wins an IPod, so i kind of want to win!
Offline
Wow, I hope you win too!
It would be helpful to me to know what math and science classes you are taking. Have you taken algebra? Trig? Physics?
I'll explain some of the physics and math necessary here. Sorry if you already know what I'm telling you.
-- First you'll need a starting velocity stored in a variable. I'll call it velInit. The way you get that velocity is up to you. Maybe you could use a slider variable or the ask function. Or, you could use the mouse's distance to the cannon to determine the velocity by putting this in the cannon's scripts:
set [velInit v] to (distance to [mouse-pointer v])-- You'll also need a launch angle stored in a variable. I'll call in angleInit. Once again, you'll need to choose how to get the angle. If you want to use the mouse, the easiest method might be to have the cannon point towards the mouse, and then find the angle the cannon is pointing with this cannon script:
point towards [mouse-pointer v] set [angleInit v] to ([90] - (direction) )To correct for Scratch's angle system, you'll have to put 90-direction so the upcoming trig will work.
set [velInitX v] to ((velInit) * ( [cos v] of (angleInit) )) set [velInitY v] to ((velInit) * ( [sin v] of (angleInit) ))-- Now, you'll need two more variables: the initial starting position of the missile in the x (initX) and the initial starting position in the y (initY). Set initX and initY at the beginning of the project. They are constants.
repeat until < (y position) < [-180] > wait (1) secs change [t v] by (1) broadcast [Find X and Y v] endHaving it update once a second will probably be choppy, so you may want to change both 1's to .5's or .1's.
((initX) + ((velInitX) * (t)))-- Like I said, there is an acceleration in the y: -9.8. So, the new y position is:
(((initY) + ((velInitY) * (t))) + ([-9.8] * ((t) * (t))))-- As a result, the missile's script to find x and y at each point in time is:
when I receive [Find X and Y v] go to x: ((initX) + ((velInitX) * (t))) y: (((initY) + ((velInitY) * (t))) + ([-9.8] * ((t) * (t))))I wish you luck! If you need any more explanation of this, just let me know.
Last edited by scmb1 (2012-05-24 13:22:03)
Offline
Hey scmb1, thanks for your help, but sadly i am only in year 9 and taking simple science and math. But i do understand Algebra, but none of that Cos Sin stuff HaHa! xD. At the moment this script you provided is excellent, but it seems to be a bit over my head, i understand the actual idea behind it, but still i am yet to fully understand it. I will go over it again a few times to try understand it. It's funny, i can spit out a HTML script or even a VBScript script with ease, but i struggle with scratch's simple GUI, possibly because i am use to actually inputing the script by well, 'scratch'. haha.
Thanks heaps
Dan Jones
PS. I will be sure to add your name to my project for helping!
Offline
Oh yeah! one more thing, my script for the gravity i put together is in my projects, i think i successfully pulled it out of my game correctly lol. I will put my game online when I (hopefully) win the competition!
Offline
Looks great! Good luck.
The trigonometry (cos and sin) is only necessary to split the velocity into x and y components because the x and y velocities change at different rates. (x velocity doesn't change at all; y velocity changes at a rate of -9.8). The trig script I gave you is all you will probably need. Once you do that, algebra should help you with the rest.
Here is a table that might help you understand the trig:
In your project, velInit is the hypotenuse, velInitX is the side adjacent to the angle and velInitY is the side opposite the angle.
Here is how you can solve for velInitX:
-- Use trig to find
cos(angleInit) = velInitX/velInit
-- Now, multiply velInit to both sides.
velInit * cos(angleInit) = velInitX
Solving for velInitY is basically the same, but with sine (sin) instead of cosine (cos)"
-- sin(angleInit) =velInitY/velInit
-- Multiply velInit to both sides.
velInit * sin(angleInit) = velInitY
Last edited by scmb1 (2012-05-25 13:51:39)
Offline
o.o, i think i understand now, lol, i never expected to be learn math on a saturday! xD
Just wondering if you could check ou my new gravity and walking script at http://scratch.mit.edu/projects/daniel_j/2565935 I am hopefully going to combine your exxcelent math lesson and this script into a game.... Thank god i have 1 school term to complete it lol!
thanks
Offline