Forum Topic: AS: Jump and land perfectly

(303 views • 16 replies)

This topic is 1 page long.

<< < > >>
None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/29/09 11:37 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

AS2

Well, due to so many platformers having the old issue of the character sticking into the ground or so, i decided something has to be done, and so i made this tutorial. Things you need to know: Loops-for... in and have a basic mathematical ability.

1) Gravity
many platoformers use a code like this:

grav ++
char._y += grav

Although this works, it is unlike real gravity's acceleration (9m/s^2)
Its not that complicated, simply put, you need a second gravity variable.

grav ++
yvel += grav
char._y += yvel

HitTesting
This is the bit that made me want to start this tutorial. Hittesting at the moment is not up to scratch in my opinion. At the moment, most platformers use something along these lines:

if(ground.hitTest(char._x, char._y, true)){
grav = 0
}

Works ok, until the character gets stuck in the ground. Now you could make a while loop raise him up, but a character can still completely fall through the ground. I bet you dont know why it does this though do you? here is the reason: Imagine the gravity variable is at 20. This means the character's _y position will be move down 20 pixels. Now, imagine the ground is only 10 pixels below the character, that means the character will en up 10 pixels below the ground and if that ground is only 5 pixels high, it means that the character wont even see the ground there. Now, using for loops we can check every single pixel below the character, but not so far that it extends the gravity variable. Generally not even 100 loops of a basic hittest like this will generate any lagg, and if gravity is reaching that high, you've got terminal velocity issues :].
so a basic code for something like this would look like this.

for (var i:Number = 0; i<yvel; i++) {
	//for loop
	if (ground.hitTest(char._x, char._y+i, true)) {
		//hitTest. Remember, you have to incoparate i into the _y cheak point
		char._y += i;
		//Remember to then change the char._y to what i is at that moment
		yvel = 0;
		grav = 0;
		//dont forget to reset your gravity variable, or it wont make any difference
		break;
		//and add a break; just so when the hittest is actually true that the loop stops.
	}
}

also remember, try to still keep the ground thicker then 10 if you plan on having alot of other code, just for incase.
Example .fla
Example .swf

you can jump using up arrow

Any questions, please feel free to ask

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/29/09 03:58 PM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

sorry, mistake in my code
change this:

for (var i:Number = 0; i<yvel; i++) {
	//for loop
	if (ground.hitTest(char._x, char._y+i, true)) {
		//hitTest. Remember, you have to incoparate i into the _y cheak point
		char._y += i;
		//Remember to then change the char._y to what i is at that moment
		yvel = 0;
		grav = 0;
		//dont forget to reset your gravity variable, or it wont make any difference
		break;
		//and add a break; just so when the hittest is actually true that the loop stops.
	}
}

to this

for (var i:Number = 0; i<yvel; i++) {
	//for loop
	if (ground.hitTest(char._x, char._y+i, false)) {
		//hitTest. Remember, you have to incoparate i into the _y cheak point
		char._y += i;
		//Remember to then change the char._y to what i is at that moment
		yvel = 0;
		grav = 0;
		//dont forget to reset your gravity variable, or it wont make any difference
		break;
		//and add a break; just so when the hittest is actually true that the loop stops.
	}
}

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

BloodSucker150

Reply To Post Reply & Quote

Posted at: 9/29/09 04:01 PM

BloodSucker150 LIGHT LEVEL 18

Sign-Up: 03/20/08

Posts: 1,026

yeah man but now you can't get off the platform you can stuck i nthe air in the same _y of the highest platform..(if you know what i mean..)

BBS Signature

None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/29/09 04:13 PM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

ignore my second post, the code was right.

but heres a new example .fla

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

Happy

Doomsday-One

Reply To Post Reply & Quote

Posted at: 9/29/09 04:14 PM

Doomsday-One EVIL LEVEL 10

Sign-Up: 10/28/05

Posts: 1,387

Nice, simple tutorial. It would take some initiative from readers to make it work with all directions, but that's alright. It encourages independent thought.

The only real problem is how you've treated gravity. In your second code piece (the one with three lines), you are adding one to grav on each run. The acceleration caused by gravity is [pretty much] a constant, so it should never change. You should be adding the same number to yvel on each execution.

Following this, gravity should not be altered in the loop

Again, a wonderful method that not too many programmers know about. Well done.

Doomsday-One, working on stuff better than preloaders.

By the way, I made the 'Create an Animated Sprite' preloader for Sprite TV 3!


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/29/09 04:19 PM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

At 9/29/09 04:14 PM, Doomsday-One wrote: Nice, simple tutorial. It would take some initiative from readers to make it work with all directions

dont want to give them everything, instead encourage them to actually note whats going on and learn what it all does, then being able to impliment it them selves. Much better learning in my opinion :]

The only real problem is how you've treated gravity. In your second code piece (the one with three lines), you are adding one to grav on each run. The acceleration caused by gravity is [pretty much] a constant, so it should never change. You should be adding the same number to yvel on each execution.

true, true. i guess i shouldn't have included it because its down to personal taste, it gives the platformer a much better feel in my opinion.

Again, a wonderful method that not too many programmers know about. Well done.

Thanx :]

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

UnknownFury

Reply To Post Reply & Quote

Posted at: 9/29/09 04:31 PM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

At 9/29/09 11:37 AM, Yambanshee wrote: Although this works, it is unlike real gravity's acceleration (9m/s^2)
Its not that complicated, simply put, you need a second gravity variable.

grav ++
yvel += grav
char._y += yvel

Firstly, gravity on earth is 9.81 so closer to 10 than 9. Secondly, what hell is that code about? Gravity should be constant. You're increasing the acceleration when acceleration due to gravity should be constant.

g = 1.5;
// ...
vy += grav;
char.y += vy;

And your use of a for loop is silly, what's wrong with a while loop?

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


Questioning

evan210

Reply To Post Reply & Quote

Posted at: 9/29/09 05:19 PM

evan210 FAB LEVEL 25

Sign-Up: 05/14/07

Posts: 3,680

I've been working on a platformer-type thing for the past few days, but I'm having problems with slopes.
Here's the file.
Is there some universal little code to fix it, or would you need to see the .fla?


None

zrb

Reply To Post Reply & Quote

Posted at: 9/29/09 06:49 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,562

@Yambanshee:
Really ? Accelerating gravity ?

At 9/29/09 05:19 PM, evan210 wrote: I've been working on a platformer-type thing for the past few days, but I'm having problems with slopes.

Go make your own thread >:C

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

Questioning

evan210

Reply To Post Reply & Quote

Posted at: 9/29/09 06:50 PM

evan210 FAB LEVEL 25

Sign-Up: 05/14/07

Posts: 3,680

At 9/29/09 06:49 PM, zrb wrote:
At 9/29/09 05:19 PM, evan210 wrote: I've been working on a platformer-type thing for the past few days, but I'm having problems with slopes.
Go make your own thread >:C

No.


None

kizza0

Reply To Post Reply & Quote

Posted at: 9/29/09 09:19 PM

kizza0 NEUTRAL LEVEL 08

Sign-Up: 10/13/07

Posts: 276

Or...
if(ground.hitTest(char._x, char._y+y_vel, true)){
char.y=ground.y; or change y_vel to the distance between the ground and the player. That way it wont bug when you try get off the ground.

BOOM HEADSHOT

BBS Signature

None

Stannous

Reply To Post Reply & Quote

Posted at: 9/29/09 10:07 PM

Stannous DARK LEVEL 04

Sign-Up: 10/08/08

Posts: 34

Yeah, since gravity is accelerating (i.e. jerk != 0), it's not very much like real gravity, as it takes much less time falling then rising, whereas in the real world it takes almost the exact same time (less than milliseconds difference due to changes in wind/earth's rotation, but they're basically negligible) to rise and fall.

It's fairly easy to make it so you don't get stuck by limiting gravity (if(grav>10){grav=10;}... doesn't really make it look bad considering at 10 considering the percent increase from 10 to 15 is less than 5 to 10, etc.), and having you hittest test for the _y coord minus or plus a few pixels depending on the shape of your character. It may possibly make a slight gap in between your character and the ground, in which case you could set the char's _y to a certain value so that he's always standing at the same point (i.e. if the hit test is true for 4 pixels away, set _y to 4 pixels closer). This doesn't work for slopes due to changing y coords, but usually a shapeflag hittest should allow your character to walk up and down slopes fairly easily, unless your speed is much higher than gravity (in which case he'll walk off in one direction)


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/30/09 02:54 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

I did say that i shouldn't have included the thing about gravity due to it being more opinion, but you can take it or leave it, its a god damn tutorial on another method of doing things, not nessacery the right method, but the method i prefer and wanted to share with the rest.

At 9/29/09 05:19 PM, evan210 wrote: I've been working on a platformer-type thing for the past few days, but I'm having problems with slopes.

You could use a while statement to raise your player's _y position when hes in the ground but i dont think thats the problem. do you have a wall hittest? if so how low is the hittest's _y point because i think thants stoping you at the moment

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

UnknownFury

Reply To Post Reply & Quote

Posted at: 9/30/09 04:50 AM

UnknownFury EVIL LEVEL 26

Sign-Up: 08/10/05

Posts: 6,027

At 9/30/09 02:54 AM, Yambanshee wrote: I did say that i shouldn't have included the thing about gravity due to it being more opinion, but you can take it or leave it, its a god damn tutorial on another method of doing things, not nessacery the right method, but the method i prefer and wanted to share with the rest.

"Although this works, it is unlike real gravity's acceleration (9m/s^2)"

You were telling people the first code isn't like gravity, when it is, hence suggesting that your method was right/more realistic.

Portfolio(Under construction): UnknownFury.com |
Msn: giant_ak_47@msn.com | Contact: me@unknownfury.com
Follow me on twitter!


None

Yambanshee

Reply To Post Reply & Quote

Posted at: 9/30/09 05:13 AM

Yambanshee DARK LEVEL 11

Sign-Up: 10/05/08

Posts: 1,607

ahh fuck it, ill just delete the post and deprive people from learing alot of new stuff because theres some whiny babys going on about a mistake i made!

AS2||AS3||Motox
Thanks to hdxmike for the sig :]

BBS Signature

None

zrb

Reply To Post Reply & Quote

Posted at: 9/30/09 05:20 PM

zrb LIGHT LEVEL 11

Sign-Up: 08/08/06

Posts: 4,562

At 9/30/09 05:13 AM, Yambanshee wrote: ahh fuck it, ill just delete the post and deprive people from learing alot of new stuff because theres some whiny babys going on about a mistake i made!

That's a pretty big mistake and now your being a baby about it :\

School Sux ! || As :Main || As3: Main || Animation: Main || Flash Tutorials ||

BBS Signature

None

mkmetalhead

Reply To Post Reply & Quote

Posted at: 9/30/09 06:17 PM

mkmetalhead NEUTRAL LEVEL 07

Sign-Up: 08/09/08

Posts: 35

If you're making a game about a small italian plumber riding a lizard-donkey, i'm not sure whether the gravity would have to be 100% true to real life.

I like the feel the code gives a game. Thanks for the tut.


All times are Eastern Standard Time (GMT -5) | Current Time: 11:42 PM

<< Back

This topic is 1 page long.

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!