Discuss Scratch

stasikgri
Scratcher
26 posts

Advanced velocity and gravity script.

I got an idea for a nice game, but i need a velocity and gravity script. I searched a lot, but i didnt find anything good. Let me explain: a ball will roll down a small hill and will gain speed at the same time.
http://scratch.mit.edu/projects/25446100/ <— easier explanation.
PS: i need that the ball will roll smooth and wont just randomly stop.
Thanks.
derpmeup
Scratcher
1000+ posts

Advanced velocity and gravity script.

I can't really help too much with gravity, but I'll show you a velocity script:
when green flag clicked
set [xvel v] to [0]
forever
if <key [left arrow v] pressed?> then

change [xvel v] by (-1)
end
if <key [right arrow v] pressed?> then

change [xvel v] by (1)
end
set [xvel v] to ((xvel) * (0.9))
change x by (xvel)


hey bro that's a pretty good meme xD!
stasikgri
Scratcher
26 posts

Advanced velocity and gravity script.

Well, thanks, now i need only a gravity script.
MegaApuTurkUltra
Scratcher
1000+ posts

Advanced velocity and gravity script.

define apply_gravity
set [yvel v] to (0)
reset timer
repeat until <touching ground>
change [yvel v] by ((timer)*(-9.81))
// assuming a pixel is a meter. If you have a bigger unit add more multiplication here
// also, the acceleration is -9.81 instead of 9.81 because down is negative y
// 9.81 is the acceleration of earth's gravity near the surface
change y by ((yvel)*(timer))
// this isn't the most accurate thing ever because it discretely calculates velocity and position
// you'll need calculus for more accuracy
// but I'm assuming this is good enough
reset timer
// the timer makes sure this runs mostly the same with or without turbo mode and on different computers
HTH

Last edited by MegaApuTurkUltra (Aug. 10, 2014 14:51:35)


$(".box-head")[0].textContent = "committing AT crimes since $whenever"
stasikgri
Scratcher
26 posts

Advanced velocity and gravity script.

I will give it a try and say what happened.

Powered by DjangoBB