I've seen a lot of topics recently asking how to add the effect of gravity in their game. I thought "This is really cluttering up the forums". So from now on, you can just link to this topic whenever someone asks that question!
_______________________________________________________________________________
First, you need to create a variable. To do this, click on the orange tab called 'Variables' in the bottom right corner of the tabs.
Then, click on the 'Make a variable' button, and make a variable called 'Y velocity'. Make sure you check the box saying 'For this sprite only', and not 'For all sprites'.
Then, on the sprite that you want to jump, put this script:
You can change the 15 in this to another number, depending on how high you wan it to jump:
(higher number=higher jump, lower number=lower jump)
What's happening?
If the sprite that's jumping is touching the colour of the ground, then one of two things will happen; if the up arrow key is being pressed (or another key of your choice), it will set the variable 'Y velocity' to 15; this is what makes it jump. If the key isn't being pressed, it will set the variable 'Y velocity' to 1; this is so that the sprite will always rise to the top of the surface, instead of partly being underground. If it's not touching the colour of the ground, it will change the variable 'Y velocity' by -1 (decreasing it). This is what gives the effect of gravity. Finally, the sprite changes it's y position by the variable 'Y velocity'.
_______________________________________________________________________________
Alternatively, if you want the sprite to bounce, you can change the previous script to one like this:
What's happening?
This script is almost the same, apart from one part of it. That one difference in the script makes a big difference in the project though. The difference is this:
Instead of this:
If you don't know what 'abs' means, it's short for 'absolute value', and it basically turns a negative number into it's positive equivalent, but if the number's positive it will stay the same. So abs of -5 = 5, and abs of 5 = 5. Another way to remember the meaning is that it's the distance from zero.
Anyway, back to the tutorial. When the sprite is touching the ground colour, it will set the variable 'Y velocity' to 'abs of Y velocty * 0.8'. You can change the 0.8 to whatever number you want; changing it so that it's higher means that it will take longer to settle on the ground, while decreasing the number will mean it takes less time to settle. Play around with it and see if you can find the perfect amount for you!
And that's it! If you use this method, I don't mind if you don't give me credit in your game, but I'd appreciate it if you do!
Also, tell me if there's any mistakes I need to correct!
Last edited by RedRocker227 (2011-12-06 10:41:16)
Offline
Bump
Offline
not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.
Offline
mrwalker81 wrote:
not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.
Sorry, could you maybe simplify that a bit?
Offline
Sweet!
Offline
Pecola1 wrote:
Sweet!
Thanks
Offline
RedRocker227 wrote:
mrwalker81 wrote:
not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.
Sorry, could you maybe simplify that a bit?
Haha. yeah. Falling objects accelerate. For the first second an object is falling, it is traveling around -9.8m/s. By the second second, this is doubled, and so on until it reaches it's terminal of velocity(max rate of speed by mass). So by creating a timer that counts up from zero, you can multiply the timer variable by -9.8 (or whatever rate you want), you will get a realistic gravitational fall.
Offline
mrwalker81 wrote:
RedRocker227 wrote:
mrwalker81 wrote:
not quite... Although this will work for simple gravity-like interactions, it is missing the fact that when gravity acts upon an object, it will continue to accelerate until it reaches it's terminal velocity or lands somewhere. Good clean tutorial though, I think this will get most people where they need to go.
Sorry, could you maybe simplify that a bit?
Haha. yeah. Falling objects accelerate. For the first second an object is falling, it is traveling around -9.8m/s. By the second second, this is doubled, and so on until it reaches it's terminal of velocity(max rate of speed by mass). So by creating a timer that counts up from zero, you can multiply the timer variable by -9.8 (or whatever rate you want), you will get a realistic gravitational fall.
Ah, okay. This was designed for beginners though, so I didn't really wanna make it too complicated. Good suggestion though!
Offline
Is it worth mentioning that the "Y velocity" variable should be set to "this sprite only"?
Offline
blob8108 wrote:
Is it worth mentioning that the "Y velocity" variable should be set to "this sprite only"?
Oh yeah, good suggestion!
Offline
XOuter wrote:
In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0
But then it would be partly underground; you need a 1 so that it rises to the top of the ground.
Offline
XOuter wrote:
I hate how the object just bounces on the ground though, I'm going to figure out how to fix that.
Yeah, but unfortunately that comes hand in hand with an essential part of the script I think you can stop it vibrating using colour sensors, but I've never attempted it.
Offline
RedRocker227 wrote:
I've seen a lot of topics recently asking how to add the effect of gravity in their game. I thought "This is really cluttering up the forums". So from now on, you can just link to this topic whenever someone asks that question!
_______________________________________________________________________________
First, you need to create a variable. To do this, click on the orange tab called 'Variables' in the bottom right corner of the tabs.
http://i41.tinypic.com/wmgdxy.png
Then, click on the 'Make a variable' button, and make a variable called 'Y velocity'. Make sure you check the box saying 'For this sprite only', and not 'For all sprites'.
http://i44.tinypic.com/fc90nt.png
Then, on the sprite that you want to jump, put this script:
http://i42.tinypic.com/35ic3es.png
You can change the 15 in this to another number, depending on how high you wan it to jump:
http://i41.tinypic.com/2mfosk.png
(higher number=higher jump, lower number=lower jump)
What's happening?
If the sprite that's jumping is touching the colour of the ground, then one of two things will happen; if the up arrow key is being pressed (or another key of your choice), it will set the variable 'Y velocity' to 15; this is what makes it jump. If the key isn't being pressed, it will set the variable 'Y velocity' to 1; this is so that the sprite will always rise to the top of the surface, instead of partly being underground. If it's not touching the colour of the ground, it will change the variable 'Y velocity' by -1 (decreasing it). This is what gives the effect of gravity. Finally, the sprite changes it's y position by the variable 'Y velocity'.
_______________________________________________________________________________
Alternatively, if you want the sprite to bounce, you can change the previous script to one like this:
http://i39.tinypic.com/157c3lc.png
What's happening?
This script is almost the same, apart from one part of it. That one difference in the script makes a big difference in the project though. The difference is this:
http://i40.tinypic.com/1okio6.png
Instead of this:
http://i42.tinypic.com/8x2ao0.png
If you don't know what 'abs' means, it's short for 'absolute value', and it basically turns a negative number into it's positive equivalent, but if the number's positive it will stay the same. So abs of -5 = 5, and abs of 5 = 5. Another way to remember the meaning is that it's the distance from zero.
Anyway, back to the tutorial. When the sprite is touching the ground colour, it will set the variable 'Y velocity' to 'abs of Y velocty * 0.8'. You can change the 0.8 to whatever number you want; changing it so that it's higher means that it will take longer to settle on the ground, while decreasing the number will mean it takes less time to settle. Play around with it and see if you can find the perfect amount for you!
And that's it! If you use this method, I don't mind if you don't give me credit in your game, but I'd appreciate it if you do!
Also, tell me if there's any mistakes I need to correct!
I figured it out the exact same way! XD
Offline
RedRocker227 wrote:
XOuter wrote:
In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0
But then it would be partly underground; you need a 1 so that it rises to the top of the ground.
I just realised, You can use invisible sprites to do this job; make an invisible sprite the colour you want the ground to be - and make the ground a shade darker or lighter
That, would probbably make the non-vibrate thingy script work
Offline
DigiTechs wrote:
RedRocker227 wrote:
XOuter wrote:
In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0
But then it would be partly underground; you need a 1 so that it rises to the top of the ground.
I just realised, You can use invisible sprites to do this job; make an invisible sprite the colour you want the ground to be - and make the ground a shade darker or lighter
That, would probbably make the non-vibrate thingy script work
Also, I found another way - Set the Costume Center to the BOTTOM of the costume, and it seems to appear to stand properly..
Offline
RedRocker227 wrote:
XOuter wrote:
In the first script, at the part where it says set Y velocity to 1, change it to Set Y velocity to 0
But then it would be partly underground; you need a 1 so that it rises to the top of the ground.
Hmm... Using variables you may be able to make it change from 1 to 0 if its on the top of the ground, but that would require some color sensing. I'll see if I can do something, or you can change the costume center, but then if you had the player have a sword or something like that, it wouldn't work.
Offline
Woot! Got a perfect working version! No changes of costume center or anything like that, It works perfectly too! You have to make a thin line of a different color at the top of the platform though. I tested it and you can use the smallest marker size so you can make it almost unnoticable. This is it, I'm a new scratcher on this account still so I can't use BBcode: http://i.imgur.com/sM6hn.gif
Offline
No, I wasn't trying to make a bad word, I was trying to make a baffled emoticon. Sorry, RedRocker227, I don't mean to be mean, I was just telling you it doesn't work.
Offline
BOBBYBOB3 wrote:
No, I wasn't trying to make a bad word, I was trying to make a baffled emoticon. Sorry, RedRocker227, I don't mean to be mean, I was just telling you it doesn't work.
Are you sure you put -1 and not 1 in the else bit of 'If touching colour [colour of ground]'?
Offline