#HELP NEEDED: Player resisting to momentum

88 messages · Page 1 of 1 (latest)

rigid lotus
#

Hi, i need help with something slightly complicated. I've got my player, who moves with acceleration (very short acceleration). The player can press a certain key, and send out a vine. if the vine catches a pole, it will pull the player towards it with momentum. Everything is fine, except when i want to cancel the momentum, and i press left key to face the other direction, the player negates it (as intended), but than, the second i leave the key, his speed goes back to the momentum speed, (which looks weird). I already know the source of the problem, and it is in the line where i set the player horizontal speed (hsp):

hsp = ((key_right - key_left) * SPD_WALK) + round(vineMomentum);

This is because we are SETTING the hsp, and we aren't lowering the vineMomentum. I wonder if there is a way to fix this, i know i probably have to redo how the hsp works, im guessing i have to do += but stuck on how to make it work. Help would be much appreciated!!!

keen mesa
#

Could try something like this maybe?

var horizontalInput  = key_right - key_left;

if (horizontalInput != 0 && sign(horizontalInput) != sign(vineMomentum)) vineMomentum = 0;
#

Though it would probably look better if you rapidly reduced vine momentum rather than just instantly setting it to 0.

#

Maybe something like:

var horizontalInput  = key_right - key_left;

if (horizontalInput != 0 && sign(horizontalInput) != sign(vineMomentum)) vineMomentum = vineMomentum *.1;
if (vineMomentum !=0 && abs(vineMomentum) < someNumber) vineMomentum =  0;
rigid lotus
keen mesa
#
hsp = ((key_right - key_left) * SPD_WALK) + round(vineMomentum);

Still this, right? You could just zero out vineMomentum so that it's no longer affecting the hsp calculation.

Assuming I'm understanding where the issue is coming from.

Right now you are adding two numbers, SPD_WALK which is affected by inputs, and vineMomentum which is not. So when you hold left, SPD_WALK is negative so it is acting against vineMomentum, which in your example video is positive. But it's not reducing the numerical value of vineMomentum.

So when you let go of the button it's no longer acting against vineMomentum, so you start accelerating to the right.

#

This looks to see if your input direction is moving you in the opposite direction of vineMomentum and if you are it sets it to 0, so that when you let go of the direction key you should just drop, since vineMomentum has been set to zero.

var horizontalInput  = key_right - key_left;

if (horizontalInput != 0 && sign(horizontalInput) != sign(vineMomentum)) vineMomentum = 0;
#

At least that's how it's working in my head?

rigid lotus
rigid lotus
keen mesa
#

So, vine momentum is gonna be negative if you are vining to the left and positive if you are vining to the right, yeah?

So an easy way to have it approach zero rather than snap to it is to multiply it by a fraction. But you want it to reach zero eventually. And if you're always, for example, multiplying by .10 it will never actually reach zero. It'll just keep getting smaller and smaller.

So you look at the absolute value of vine momentum, check to see if it is smaller than some very small number, and, if so, set it to 0.

rigid lotus
#

so Somenumber shoud be like 1 or something right?

#

or 0.1 etc

keen mesa
#

IDK what code you have that may be affecting vine momentum in the background. That was just a quick way of making sure it gets to 0 eventually.

#

Yeah.

rigid lotus
#

ok, thats great, but for example i want the resistance to be weaker, i up the * .1 to * .99 right?

keen mesa
#

Absolutely.

#

Just play with the numbers until you get the feel you want. Movement rarely works well on the first go.

#

(for me lol)

rigid lotus
rigid lotus
# keen mesa (for me lol)

its kinda hard to see but do you notice how the player speed goes back up again the second i stop resisting?

keen mesa
#

What if you do .1? Still getting the unexpected movement?

rigid lotus
#

no, but thats because the resistance is way too large

keen mesa
#

Well 1% per frame is pretty close to nothing.

#

So you'll need to play with the numbers until you get the effect you want.

rigid lotus
#

because it feels weird that i press, than leave, and suddenly my speed goes back up

keen mesa
#

I haven't done a lot of platforming stuff, so I don't know that I could give you a formula that's going to be smooth right off the bat. If you want to send me a YYZ, I'd be happy to play with the numbers.

rigid lotus
keen mesa
#

Yep. Zip file of the package.

rigid lotus
# keen mesa Yep. Zip file of the package.

ok, but the file is too large to send on discord, i tried earlier removing unnecessary stuff from the package but because of my crappy code a lot of the objects rely one on another (definitely going to remove a lot of objects and code once im done with the movement)

#

187,839 kb

keen mesa
#

That's pretty big. If you want to toss it on a hosting service or whatever, I'll download it.

#

Google drive, github, whatever works.

rigid lotus
keen mesa
#

That works.

#

No problem. Again, platformers aren't really what I've mostly worked on... so no promises. But hopefully I can find a working solution.

rigid lotus
rigid lotus
keen mesa
#

Yeah, one or the other.

#

I'll just download a copy and work on that once I can get to it.

rigid lotus
keen mesa
#

yeahchris

rigid lotus
#

ok i sent you an invite

keen mesa
#

Well, this feels better. Give it a try:

    var horizontalInput  = key_right - key_left;

    if (sign(horizontalInput) == -sign(vineMomentum)) vineMomentum = vineMomentum *.98;
    if (vineMomentum !=0 && abs(vineMomentum) < 1) vineMomentum =  0;
    if (vineMomentum == 0)hsp = ((key_right - key_left) * SPD_WALK); // * vineMomentum
    else hsp = vineMomentum;
#

Not 100% sure it's what you want.

#

It feels a lot better if you wait for vineMomentum to be fully negated before allowing inputs to add/subtract SPD_WALK from hsp.

#

Though this change also stops them from adding to their speed if they press move towards the vine pull. But that can be rectified if you want that to happen.

rigid lotus
rigid lotus
keen mesa
#

Yeah, that should just be a matter of tweaking the initial value of vine momentum wherever that gets initially set.

rigid lotus
#

sorry for being so nitpicky

keen mesa
#

Lemme check what you mean.

#

I'm not seeing that? This is if you're holding the movement key in the direction that you are vine whipping?

rigid lotus
keen mesa
#

Oh, okay. I think I get what you mean. You just want to allow movement slightly earlier if you are landing on the ground?

But without allowing for a mid-air trajectory that looks like this:

rigid lotus
#

yes, exactly

#

but it also works if i change the snapping to 3

#

something like that

keen mesa
#

How are you doing collision? Not finding the code right away.

rigid lotus
#

move_and_collide

keen mesa
#

Tiles or objects?

rigid lotus
#

objects, with oWall

keen mesa
#

Cool.

#

I was thinking just put a check to see if there's a floor a few pixels away using an OR statement.

rigid lotus
#

it's in a function called FallAndCollide(), there i handle gravity, jumping and collisions, since i want those to be called by multiple states

keen mesa
#

Makes sense.

rigid lotus
keen mesa
#

Naturally.

rigid lotus
keen mesa
#

Distance meaning vineMomentum or something else?

keen mesa
#

Well, you have a vineMomentumMax variable. If its getting larger than you want that means somewhere in the code where you are setting the initial value of vineMomentum you are probably not clamping it to max out at vineMomentumMax.

#

Where do you set up vineMomentum?

#

Found it.

rigid lotus
#

sorry

#

fsr i just deleted a line of code that reset freeHsp and vineAddedMomentum

keen mesa
#

Oh, okay. So all good now?

rigid lotus
keen mesa
#

Like, at all? You get stuck?

#

Trying to recreate it, but not having much luck.

#

You could try just zeroing out vineMomentum with that same check to see if you're about to land.
(though this may create an issue if you intend to let your player zip at high speed just over land using the vine whip. It might be better to do it in your collision code, just before the move_and_collide.)

#

I can't recreate the error with the copy I have downloaded. Not sure if the bug doesn't exist in my copy or whether the timing is just hyper-precise.

rigid lotus
rigid lotus