## Day 72: The acceleration in the z direction shouldn't really be -9.8 right?

Today an amazing thing happened to me. I was following along with episode 72 and in this one, Casey Muratori adds a maximum velocity for entities in the sim region. Then he adds an assert to ensure nothing is going to exceed this velocity and hence the logic for gathering entities into the sim_region will not skip any fastmoving/big entities.

This assert immediately fires. Now, for the first time ever I predicted the exact line he wrote next to fix the issue before he said it 😀. Because I had seen in the debugger that it was just the z-component that was exceeding each time. So I just went and zeroed out the velocity after the collision loop, which he did right afterwards too. It was an awesome feeling 😀 when he did the same later.

But also, this makes me think that really that z acceleration shouldn't even be there. Its not like any object on the ground always has that acceleration, it has a force acting downward due to gravity and then a normal reaction force from the ground, making the total acceleration 0. Now, I know this is throw away code and he is probably going to correct it anyways. I still had to post this though, because of me guessing that one line of code haha.

One thing I do wonder is if later on in the solid simulation code there would be forces or whether it would just be accelerations. Off the top of my head, the only way to compute the vertical acceleration of the objects when there is gravity would be to include the normal force equations as well. But then one would need to maintain some other kind of state which would say whether or not an entity is touching the ground or not.... I'm getting ahead of myself though.

The collision/physics system will not evolve much, and at some point the game will be switched to tile based movement for the player.

If you are speaking of applying forces to rigid bodies like in a physics engine (e.g. specifying an impulse), it's in my experience, not a good way to control a character (you never feel in control), and is not covered in handmade hero.

You generally want to keep track or have some way to query if an entity is on the ground. A common case is just to know if the player can jump.

Edited by Simon Anciaux on

I see. I was thinking more about constraints than impulses. Like you already said, one would want to know if the entity is touching the ground.

Also, I was thinking something like:

1. Add the z component of acceleration to the player when the jump button is pressed
2. Zero out the z component of acceleration when the entity's P.Z is 0 (ground collision)

So right now the acceleration is added to the player in `MoveEntity` like this

And I was thinking something like adding it in the controller processing code here

This is not technically an impulse equation. But it is still going to immediately apply an acceleration to the player on button press. Will this method be included in the impulse category and hence not a good way to control the player?

The 2nd point above is what makes me think its a constraint rather than an impulse equation. Basically the constraint equation of the system is that when Z is zero all dZ and ddZ can only be positive or 0 and if it is negative its immediately set to 0.

Edited by Gaurav Gautam on
Replying to mrmixer (#26731)

What I meant with impulse was using the physics engine to apply force to a "generic" rigid body and let the engine compute drag, deceleration ...

The way you described is perfectly fine. Each game does things a bit differently. It's up to you to find what works for your game, and that may evolve over time. Just make sure that it works consistently at different frame rates (that why it's recommended to use fixed time-step for "physics simulation") and be careful if you have already designed some level that the changes don't affect playing those levels.

If you haven't, look at the Tommy Refenes interview from HandmadeCon 2015 where is talks about the controls in Super Meat Boy.

Edited by Simon Anciaux on

Yes I have seen that video. Ill rewatch.

Replying to mrmixer (#26734)