PDA

View Full Version : [AS2] Getting character running up and down hills


The_Hams
02-22-2009, 04:51 PM
For the past couple weeks, I've been surfing ActionScript help forums, seeking the answer to a simple question: how can I get my character's body rotation to mirror the angle of the ground he's on? That is, if he's on a 45 degree incline, his body should rotated 45 degrees as well. I just have no idea how to accomplish this... Someone suggested placing two dots on my character's feet, and if only the left one is touching, then rotate my character clockwise; and if only the right, counterclockwise. I've tried to make a simple demo game using this concept with a skateboard that you control with the A and D keys. I've attached it to this post.

Please. I'm very dedicated to this game. I'll consider any help you can give. I plan on making up my own fake physics to go along with my game, so I don't want to use a physics engine, nor do I want to create one. I just want my character running up and down hills smoothly.

kkbbcute
02-23-2009, 03:09 AM
Okay, I have a really retarded workaround that should work as long as you don't have really jagged terrain that your character is supposed to walk on. Here's the idea, sample the gradient of the incline of your hill or whatever ground your character is on, then work out the rotation of a tangent to the hill you have there, and snap your character to the tangent using the same degree of rotation. You sample the gradient by placing two MCs at the feet of your character like you said and working out the gradient from there.

The_Hams
02-26-2009, 04:09 PM
Would you mind helping me this code, kkbbcute? I added this code:

height = (leftist.y - rightist.y);
width = (rightist.x - leftist.y);

And when both dots are touching, it does this:

plank._rotation = Math.atan(height / width);

But now my program is even more jittery and seizurey than before D:

bluemagica
02-26-2009, 04:18 PM
I think i was the one who suggested you the two mc thing(actually i thought i said to use 3 mc...http://www.actionscript.org/forums/showthread.php3?t=195082), anyway, you would really be better off using an actual physics engine!

oh and in your code, it will be leftist.x for width!

kkbbcute
02-27-2009, 06:51 AM
oh and in your code, it will be leftist.x for width!

Wait a minute! The thread title says [AS2], so shouldn't it be ._x instead of .x? Correct me if I'm wrong.

bluemagica
02-27-2009, 07:05 AM
oh right...i never really saw the tag, but his code block!

kkbbcute
02-27-2009, 07:27 AM
oh right...i never really saw the tag, but his code block!

Anyway testing from three points is needlessly complicated, I think two points should work fine for a small character.

Side Note, maybe the .x was why his code didn't work!

bluemagica
02-28-2009, 04:46 PM
http://megaswf.com/view/0a12eaa411aaf8fe4020dc9eaa867941.html

The_Hams
02-28-2009, 11:04 PM
No, the .x and .y are working fine. I'm using them instead of ._x and ._y because earlier in the code I have this little bit:

var leftist:Object = {x:plank.leftdot._x, y:plank.leftdot._y}
plank.leftdot.localToGlobal(leftist);

var rightist:Object = {x:plank.rightdot._x, y:plank.rightdot._y}
plank.rightdot.localToGlobal(rightist);

So is a physics engine my best bet? I'm not interested in realistic physics, like, at all...but if it's what I need to get my character rotating his body the right way, I'll try it. Do you know of any good AS2 physics engines? I searched for some a few weeks ago and I could only find one, an engine called Flade. I downloaded it but couldn't find any tutorial for it.

Thanks for the example, bluemagica, did you make it? Is there any way I could see some of its code? I'm trying to make something more like this (http://armorgames.com/play/553/the-fancy-pants-adventure-world-2).

bluemagica
03-01-2009, 04:25 AM
eh sorry, i did make that example for you but last night when i was posting, the power went off and i couldn't post it!

Anyway, i am attaching the fla for you to see mainly the rotation! I made that thing within 15 mins, so the code is kinda sloppy, so don't bother looking everywhere, all the stuff you need to understand most is in the "eframe" function!

Also, unlike fancy pants, my movement will be quite jumpy.....whats the reason?
1) I am rotating based on center axis, while i should be rotating based on the axis of the nested movieclip touching the ground.
2) In the while loops i am moving it only along y, while it should be moved both on x and y, that is, in the direction opposite to which it is touching ground!


just try a bit looking around and you can do both of those (a bit of trigonometry needed)! good luck!

The_Hams
03-02-2009, 09:20 PM
Thanks, bluemagica, but whenever I try to open your FLA it crashes my Flash MX >_<

bluemagica
03-03-2009, 02:51 AM
ermm, oops, i forgot you had as2, i made it with as3.....

The_Hams
03-03-2009, 08:39 PM
ah poop. Is AS3 that different? If I stared at the code, do you think I'd get it? If the code is in one big chunk, could you post it here or send it to me or something?

Or maybe you could just explain it a little. Are you using the "three dot" method to find rotation? What exactly is the purpose of the middle dot? Are you using a sine function for y movement and cosine for x movement?