Go Back   kirupaForum > Flash > Game/AI Programming

Reply
 
Thread Tools Display Modes
Old 12-07-2004, 12:55 PM   #1
Sammo
Registered User
 
Sammo's Avatar
Platformer physics - hitTests + Gravity

Hey, I figured this would be a popular subject, so i did search the forums, but didn't find anything relevent to my point. Whether I can search correctly, I'm sure i'll be corrected on

Anyway, I'm a novice at AS, but I want to create a platformer (don't worry! I'm not going to ask you lot to do it!), just for fun + practice. It'll be Mario-esque, just for the lack of creativity needed to create it.
I can handle all the controls and DuplicateMovieClip() etc. It's the hitTest + gravity i'm worried about. Here is an example of my hitTests:
Code:
onClipEvent (load) {
	speed = 5;
}
onClipEvent (enterFrame) {
	if (Key.isDown(Key.LEFT)) {
		_x -= speed;
	}
	if (this.hitTest(_root.wall_left)) {
		_x += speed;
	}
	if (Key.isDown(Key.RIGHT)) {
		_x += speed;
	}
	if (this.hitTest(_root.wall_right)) {
		_x -= speed;
	}
	if (Key.isDown(Key.UP)) {
		_y -= speed;
	}
	if (this.hitTest(_root.wall_up)) {
		_y += speed;
	}
	if (Key.isDown(Key.DOWN)) {
		_y += speed;
	}
	if (this.hitTest(_root.wall_down)) {
		_y -= speed;
	}
}
/*wall_up,down,left,right are instance names of a 
movieclip called wall which is on the stage 4 times. 
Boundries, wall_up at the top of the stage, etc.*/
Which is all very simple, but it works, so it's been fine for me.

When I introduce gravity (which I need help with, but will get onto in a moment), The character won't fall with an even pace, so i wont be able to counter the speed as I have done in the code above.

Is there a more technical way of using hitTests to stop a character?

And how would I implement gravity (I have searched and read the forums, and read Poms tutorial but im still not sure) into a game, such as a 2D platformer?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote

Sponsored Links (Guests Only) - Register | Need Help?

Old 12-07-2004, 01:05 PM   #2
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
I taught some people how to make a platform game a while back:

http://www.kirupaforum.com/forums/sh...ad.php?t=69442

It's outdated (cause I no longer use onClipEvents handlers), but it might help. Also, I use this ActionScript for the proper ground setting:

Code:
function collision() { 
    with (hero) { 
        if (limits.hitTest(_x-8, _y-30, true)) { 
            _x+=3; 
        } else if (limits.hitTest(_x+8, _y-30, true)) { 
            _x-=3; 
        } 
         
        if (limits.hitTest(_x-6, _y+5, true) and (jumpPower<=1)) { 
            _y-= jumpPower; 
        } 
    } 
}
I hope that I have helped you .

P.S. You should go to www.gotoAndPlay.it, and look at some of the hitTest articles that they have there.







peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :

Last edited by joMan; 12-07-2004 at 01:18 PM..
JoMan is offline   Reply With Quote
Old 12-08-2004, 03:31 PM   #3
Sammo
Registered User
 
Sammo's Avatar

Thanks JoMan I'm reading some articles now. But I still don't get the whole hitTest thing.

I mean, for all the hitTest examples I've seen they are countering the speed for eg:

Code:
if (Key.isDown (Key.UP)) {
      this._y -= 5;
 }
 if (hitTest(whatever_mc)) {
      this._y += 5;
 }

Right?
But when gravity is involved his falling speed is always raising. So how do you stop him when you can't be sure on his final impact speed?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote
Old 12-08-2004, 05:22 PM   #4
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Oh, here is the ground ActionScript that I forgot to put with the other code:

Code:
for (i=1; i<=50; i++) {
if (!limits.hitTest(_x, _y-i, true) and (jumpPower<=1)) {
_y -= i-3;
break;
} else if (limits.hitTest(_x, _y-i, true) and (jumpPower<=1)) {
jumping = 0;
}
}
It will set the movieClip at the propper ground position once he makes contact with the limits. Also, the basic code:

Code:
if (limits.hitTest(_x, _y, true)) {
// resistance
}
can determine the x or y collision depending on the x or y property that you state, and it can also be determined by resistance, whether its x resistance, or y resistance. I hope I'm not confusing you; If so, tell me.





peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :
JoMan is offline   Reply With Quote
Old 12-09-2004, 11:57 AM   #5
Sammo
Registered User
 
Sammo's Avatar
Sorry, can you explain what that code actually means?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote
Old 12-09-2004, 12:52 PM   #6
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Ok, sure . First, what we have, is the:

Code:
for (i=1; i<=50; i++) {
The 'i' will become our resistance and propper ground setting. Now, when we add:

Code:
if (!limits.hitTest(_x, _y-i, true) and (jumpPower<=1)) {
    _y -= i-3;
    break;
This simply states that if the ground is not being touched (the '!' declares a negotive response), then the movieClip will slowly fall. The falling will not be a part of the gravity, but it is needed for the propper ground collision (instead of the movieClip landing above or in the ground ). Please reply for the next part.





peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :
JoMan is offline   Reply With Quote
Old 12-09-2004, 01:02 PM   #7
Sammo
Registered User
 
Sammo's Avatar
Yup got that, is limits a variable you defined in some other place?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote
Old 12-09-2004, 01:18 PM   #8
Inferno
´'`\_(ò_Ó)_/´'`
 
Inferno's Avatar
cool new avatar joman

__________________
Inferno is offline   Reply With Quote
Old 12-09-2004, 02:12 PM   #9
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Quote:
Originally Posted by FlashNewbi3
cool new avatar joman
thanks .

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :
JoMan is offline   Reply With Quote
Old 12-09-2004, 02:13 PM   #10
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Quote:
Originally Posted by Halikidiki
Yup got that, is limits a variable you defined in some other place?
No, 'limis' is the name of my terrain (ground) movieClip.

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :
JoMan is offline   Reply With Quote
Old 12-09-2004, 05:22 PM   #11
Sammo
Registered User
 
Sammo's Avatar
Okay, thanks for this joMan.

Think you could go on with the code?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote
Old 12-09-2004, 06:42 PM   #12
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Quote:
Originally Posted by Halikidiki
Okay, thanks for this joMan.

Think you could go on with the code?
Sure, but it will have to wait until 10:00 tonight, because I have a fencing class... ENGARDE! Seeya then!






peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :

Last edited by joMan; 12-09-2004 at 06:56 PM..
JoMan is offline   Reply With Quote
Old 12-09-2004, 10:55 PM   #13
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Ok, I'm back! Sorry for the delay. Ok, so now to declare the actual collision:



Code:
} else if (limits.hitTest(_x, _y-i, true) and (jumpPower<=1)) {
jumpPower = 0;
fallSpeed = 0;
}
}
}
}
Now, for my game, the jumpPower is the ammount of -well- jump power and the fallSpeed is the ammount of falling speed, so I declared -in this code- that both speeds will be set back to zero. Pretty basic stuff. Reply when you read this.






peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :

Last edited by joMan; 12-09-2004 at 11:01 PM..
JoMan is offline   Reply With Quote
Old 12-10-2004, 11:42 AM   #14
Sammo
Registered User
 
Sammo's Avatar
Yo, that's ok...I think...

So would I do:
Code:
onClipEvent (enterFrame) {
      if (Key.isDown (Key.UP)) {
           jumpPower = 10;
      }
 }
If yes, how and why?

If no, how would I do it?

__________________
Build a man a fire and he is warm for a day.
Set a man on fire and he is warm for the rest of his life.
Sammo is offline   Reply With Quote
Old 12-10-2004, 12:39 PM   #15
JoMan
JoMan Studios
 
JoMan's Avatar
Location ...And why do YOU want to know?!

Posts 1,456
Well, the jumping code will go like this:

Code:
function mcJump() { // or an onClipEvent handler, depending on type of programming
if (Key.isDown(Key.UP) and (!jumping)) { // if the UP key is being pressed, and jumping is false
jumpPower = 15;
jumping = true;
}
if (jumping) { // if jumping is true
jumpPower--;
if (jumpPower>=15) { // if jumpPower is more than 15
jumpPower = -15;
}
mc._y -= jumpPower;
}
}
And in the ground collision code, just state that jumping is 'false', and jumpPower is set to '0'. Please reply when you read this.






peace

__________________
.: Let your Imagination Soar! :. -JoMan.
: Kirupa Nintendo Cult Member 31 :

Last edited by joMan; 12-10-2004 at 12:47 PM..
JoMan is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i need gravity [uber] Game/AI Programming 3 08-11-2004 05:52 PM
Gravity Function cr125rider Game/AI Programming 3 07-19-2004 10:07 PM
Platformer - GRAVITY elion Game/AI Programming 2 05-17-2004 09:15 AM
Actionscripting (physics help!) wolf_man ActionScript 2 (and Earlier) 12 10-06-2003 08:17 PM
FMX gravity problems coreyem ActionScript 2 (and Earlier) 12 07-06-2003 09:58 PM


All times are GMT -4. The time now is 11:12 PM.

SHARE:

SUPPORTERS:

cdn
content delivery network (cdn)

Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd. Copyright 2010 - kirupa.com Copyright 2010 - kirupa.com