ENIGMA Forums

Outsourcing saves money => Programming Help => Topic started by: edsquare on July 17, 2014, 12:21:35 pm

Title: Make sprite run through a loop (like sonic)
Post by: edsquare on July 17, 2014, 12:21:35 pm
Already went to google for some help but I cant seem to find anything (maybe my search is faulty).  :ohdear:

The goal is to make a character/sprite run on a surface that loops without the character falling if it has the right speed, I suposse it would have to be done by a mathematical formula in the script.  :o

Trouble is to find said formula and then to adapt it to the script. I only find references to a logical loop and that is not what I need and besides I already know how they work.  :)
Title: Re: Make sprite run through a loop (like sonic)
Post by: Darkstar2 on July 17, 2014, 01:52:30 pm
I'm sure you know about the angle and direction, speed and traction aspect to achieve this.  The way I would do it is if a certain speed is not achieved the character cannot go past the bend, so they would not be able to loop around in the first place......  IF the speed is right the character continues past the bend and angles up the loop, without falling down providing momentum is maintained, I normally use flags when doing stuff like that (onground, inair, isjumping, is falling, isfastenough, etc.etc.)  :P
Title: Re: Make sprite run through a loop (like sonic)
Post by: TheExDeus on July 17, 2014, 01:53:12 pm
Usually you have to use more sophisticated physics simulations if you want this to work correctly (like Box2D), but you can of course just code a simple code where there is only a speed threshold involved. Like if the guy is running 20 pixels per frame (speed), then when hitting the loop, do either a sprite animation or a image_angle thing together with lenghtdir_x/y for placing him on radius. If the speed is lower than make him end running proportionally faster.
Title: Re: Make sprite run through a loop (like sonic)
Post by: Darkstar2 on July 17, 2014, 01:58:20 pm
Usually you have to use more sophisticated physics simulations if you want this to work correctly (like Box2D), but you can of course just code a simple code where there is only a speed threshold involved.

Well of course you can do that with physics, but for those who don't know how, the DIYS method is simpler.  I personally would use the lazy approach and do it myself rather than use physics, given there is no IDE controllable physics in LGM, you have to do everything yourself, would probably be more time consuming than speed/traction/angle/direction along with some variable manipulation and condition checks :D

Quote
Like if the guy is running 20 pixels per frame (speed), then when hitting the loop, do either a sprite animation or a image_angle thing together with lenghtdir_x/y for placing him on radius. If the speed is lower than make him end running proportionally faster.

Why not an image_angle combined bound to direction, I personally never used lengthdir_x/y, what does that do ?
Title: Re: Make sprite run through a loop (like sonic)
Post by: TheExDeus on July 17, 2014, 02:02:30 pm
http://enigma-dev.org/docs/Wiki/Lengthdir_x
Title: Re: Make sprite run through a loop (like sonic)
Post by: Darkstar2 on July 17, 2014, 02:06:30 pm
lol! Yes I know about the docs Harri I am still a little confused on its uses though as there are other ways of achieving the same, I've always used angle, speed, direction, traction. (movement), what are some examples of use with length_dir, in english terms, not just one example in the docs. :D

Regarding the sonic example and what OP is asking, how would length_x/y fit into the equation in terms of movement ?

Title: Re: Make sprite run through a loop (like sonic)
Post by: TheExDeus on July 17, 2014, 03:11:05 pm
lengthdir_x/y returns a position length "len" away at direction "dir". So using this you can make rotation around point. These functions allow you to take the loop radius, it's center and make the guy go around it. I use it basically in every possible project, because it has millions of uses - from making something shoot at certain distance, to making a tornado.
Title: Re: Make sprite run through a loop (like sonic)
Post by: edsquare on July 17, 2014, 04:08:11 pm
@Darkstar2: Thanks mate! not sure about the implementation yet but will give it a shake trying your tips.   :eng101:

@TheExDeus: Thanks bro! Will have to check that x/y stuff myself, and also box2d.  :eng101:

As you may have guessed this is for my remake of Open Surge on Enigma.
Title: Re: Make sprite run through a loop (like sonic)
Post by: TheExDeus on July 17, 2014, 04:22:57 pm
You can also look at this - https://www.youtube.com/watch?v=Q9WJDy02LUs (https://www.sendspace.com/file/wbh4kw)
It doesn't have loops though.
Title: Re: Make sprite run through a loop (like sonic)
Post by: edsquare on July 17, 2014, 05:20:50 pm
You can also look at this - https://www.youtube.com/watch?v=Q9WJDy02LUs (https://www.sendspace.com/file/wbh4kw)
It doesn't have loops though.

That game doesn't have loops true but sonic runs around a circle, from the outside!

Downloaded the engine, its a gmk, how can I see the code or D&D they used in it?
Title: Re: Make sprite run through a loop (like sonic)
Post by: TheExDeus on July 17, 2014, 05:46:41 pm
You can just load a gmk. It's a GM file format.
Title: Re: Make sprite run through a loop (like sonic)
Post by: edsquare on July 17, 2014, 05:56:04 pm
You can just load a gmk. It's a GM file format.

yea I know that, what I want to know is how to see what they did to make it work.

As when you open a C++ file, if you know the language you can read it (If it's well commented)  ::)
Title: Re: Make sprite run through a loop (like sonic)
Post by: Josh @ Dreamland on July 17, 2014, 10:36:25 pm
If you are looking to calculate the speed you must be going, you can do that by comparing the centripetal acceleration to your gravitational constant. The centripetal acceleration is given by [snip]sqr(speed) / loop.radius[/snip]. So assuming you're using a gravity constant of one pixel per frame per frame, all you want is [snip=edl]if (sqr(speed) / loop.radius < 1) path_end(loop.path);[/snip], or whatever logic you need to stop following the loop.

You'll probably find that this calculation is too realistic for your actual game to be able to use it, (it turns out you have to be moving pretty damn fast to stay on a loop by your own force), so feel free to tone down the gravitational constant for that equation. :P
Title: Re: Make sprite run through a loop (like sonic)
Post by: edsquare on July 17, 2014, 10:57:09 pm
If you are looking to calculate the speed you must be going, you can do that by comparing the centripetal acceleration to your gravitational constant. The centripetal acceleration is given by [snip]sqr(speed) / loop.radius[/snip]. So assuming you're using a gravity constant of one pixel per frame per frame, all you want is [snip=edl]if (sqr(speed) / loop.radius < 1) path_end(loop.path);[/snip], or whatever logic you need to stop following the loop.

You'll probably find that this calculation is too realistic for your actual game to be able to use it, (it turns out you have to be moving pretty damn fast to stay on a loop by your own force), so feel free to tone down the gravitational constant for that equation. :P

Thank you!

You sir a a true scholar and a gentleman!  :)