Jump to content
A 2021 backup has been restored. Forums are closed and work in progress. Join our Discord server for more updates! ×
SoaH City Message Board

360 Degree movement pseudo-code tutorial


ClawHaMMer

Recommended Posts

Hello guys, been lurking here for a while now and picking up information here and there - I've always been a big fan of sonic games since as was a kid and used to play fan games from way back before even static engines were around and people still used CC and TGF :P

I found a very nice tutorial by Damizean a while back (it was here I believe: http://www.academiarpg.com/personal/damizean/tutorial) which went through all the basics of angle detection, jumping in the 360 engine and collisions etc. I was hoping to find something similar that someone could point me to or if Damizean had the tutorial stashed away somewhere that would be awesome!

I realise alot of the basics are in the tutorial section in the form of GM and MMF files but I'm currently coding in blitz basic (2D) as I've been using that language for a few years, so was hoping for something which explains the concepts and from there I should be able to come up with code.

At the moment I'm developing a sonic engine for fun, so far I have angle detection using the classic two points underneath the player and ground detection, but I'm curious about how current engines in use for modern fangames work with slopes (gaining/losing speed) and jumping. Also I'm not sure how to handle angles when in the air and how to apply gravity correctly.

For now Im just setting the players angle to zero and using different counters for speed in air than speed on the ground but this presents problems, and I'm sure there will be a much neater, more efficient way of doing it.

tl;dr - can I haz 360 tutorials plz??? :D

Thanks for any help you can offer!

Link to comment
Share on other sites

== Slope Gravity Acceleration ==

Acceleration from slope gravity is a simple trigonometric formula based on projection:

-sin( angle - gravity_direction) * gravity;

angle is the angle of Sonic where 0 = flat floor, etc.

gravity_direction is the angle of the vector that gravity is pulling in (in general, it's 270 or 3*PI/2 meaning straight down)

gravity is the acceleration from gravity in pixels / second^2. Although I believe it's generally multiplied by 0.8 to work well with the other accelerations (i.e. so it's not too powerful to run in loop-de-loops).

You simply add that to your horizontal speed once per step and you got slope acceleration.

== Jumping and Landing ==

As for jumping, angle is generally kept at 0 while you're in the air. And the speed when jumping is converted into x/y form instead of magnitude/direction (i.e. the way you're doing it). The formula to convert magnitude/direction to x/y including the jumping off of the slope you're on is:

Vx =  jump_power * cos(angle + PI/2) + speed * cos( angle);
Vy = jump_power * -sin(angle + PI/2) + speed * -sin(angle);

And to convert them back (i.e. when landing), it's (make sure you change angle to the slope of the curve you're landing on):

speed = cos( arctan2( -Vy, Vx) - angle) * sqrt(Vx*Vx + Vy*Vy);

  • Like 1
Link to comment
Share on other sites

No problem. Here are some essential trigonometric functions which come up very frequently in working with game physics. And they're pretty much verbatim the solutions to these problems, so I thought they were worth mentioning:

trig1.png

arctan2( -(y2-y1), x2-x1) - will give you the angle of the vector going from (x1,y1) to (x2,y2).

sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)) or sqrt(sqr(x1-x2) + sqr(y1-y2)) - will give you the distance from (x1,y1) to (x2,y2)

cos( angle) * distance - will give you the x-component of a vector whose magnitude is distance and whose direction is angle

-sin( angle) * distance - will give you the y-component of a vector whose magnitude is distance and whose direction is angle

trig2.png

cos( angle2 - angle1) * distance2 - will give you the projection of

This last one is used whenever your progress is impeded by an angle or something. This is the amount of your old vector gets transfered into your new speed.

Link to comment
Share on other sites

Trig stuff

Thanks again, I'm pretty decent with basic trig but those diagrams are a nice reference thanks :)

Ive implemented slopes affecting speed now along with proper jumping, your post was very helpful thanks again :D

Welcome back, it's nice to see an old member return. Hope you stick around!

Thanks! I was never actually a member to the site but spent waay too much time fiddling with TGF and playing fangames, its interesting to see how far along things have come since then.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...