2D Bounciness and Friction, how is it calculated?

So i’ve only worked on 3D desktop games before and now i’m doing a couple of 2D mobile games (no IAP bullshit, lol) and i’m pretty clueless on how Box2D works.

more to do with bounciness, but goes hand in hand with friction, in 3D you can define how it’s calculated (min, max, mul, avg), in 2D not only can i not find such an option i can’t even find what is used.

I tried doing some testing to figure out how it’s calculating and I can only rule out multiplication - sadly the one I want, pretty sure it’s using averaging, either that or some very weird and unexplained ruleset.
is there a way to control it or am I gonna have to implement my own thing here?
or am I just not looking in the right place?

2D physics is provided by Box2D so you can browse the source yourself. The mixing of friction and restitution can be found here.

1 Like

I see, thank you.

this means there is no user control over this settings, correct? (tried re-doing my google searches with restitution instead of bounciness)
assuming so, will setting bounciness to 1 on everything and applying my own force work?
or for example in the OnCollision callback can i get my own bounce value and multiply it with rb.velocity?

*obviously both things are possible, i just wonder if there is some underlying issue with this approach that is not immediately visible

edit: maybe I should ask what is the best place to apply this kind of force?

It’s a fixed function as I linked in Box2D so you cannot select a new function. I don’t recall this being requested before so it’s never been a priority to add other functions.

I don’t follow what you’re asking unfortunately. The callback you get is after a collision response has occurred so you cannot override it in any form.

1 Like

Well, my game design requires the bounce to be regular multiplication (restitution1 * restitution2), now seeing as setting bounciness to 1 all around the board makes everything bounce with perfect energy conservation - what if i just do that and apply my own force over that to adjust.

luckily this is a small game where only the player needs to do, I just wonder where and how I should apply it.
first thing that comes to mind is to do it on the collision callback, just take (myBounce * otherBodyBounce) and apply relevant force to the rigidbody (player is the only dynamic body) something like this:

    void OnCollisionEnter2D(Collision2D collision) {


        rb.velocity *= (myBounce * collision.bounce);//not actual game code
}

I know from 3D that directly setting .velocity can introduce some weird behavior(even though the manual rarely adresses it, lol), is this the case with 2D too?
The 2D AddForce function isn’t fleshed out as the 3D counterpart to allow for easy control, it feels weird accounting for mass only to have it accounted for and discarded.

edit: i did some testing and setting bounciness to 1 and friction to 0 conserves energy 100%

I can only speak for 2D but it’s not that it introduces “weird” behaviour, it’s that it’s obviously the job of physics to produce collision responses in the form of impulses to affect velocity as well as gravity doing the same. If you stomp over velocity you affect this It’s not weird in the sense of undefined behaviour, it’s simply that you’re overriding it but there’s no other side-effects at all apart from you waking the body from sleep. For instance, turn off gravity and apply it yourself by adding the mass-independant acceleration and it’s identical to what’s done by the 2D physics system, guaranteed.

The AddForce exposes exactly what’s available in Box2D. In the end, all it does is modify velocity as a helper method and is absolutely no different than modifying velocity yourself. I don’t follow what you mean by mass being discarded though. In Box2D both force and impulse are scaled by (inverse) mass. Here’s where the accumulated forces are applied during solving (gravity + accum forces scaled by mass). Impulse is the same but that modifies velocity when you call it (impulse). If you want to modify velocity without mass then just modify velocity.

Whilst you should get a good approximation of energy conservation, there’s not enough accuracy and certainly not the intent to do so in Box2D. Also, positions can drift because of position correction which is a stability mechanism which is more important than accuracy in Box2D.

Anyway, hope some of that info helps. Good luck!

1 Like

In the PhysicX the ForceMode have 2 more modes that doesn’t take mass into account (Acceleration and VelocityChange), to my understanding they are there because manipulating .velocity directly can have subtle side effects (that’s what I meant by weird, not undefined - unexpected).
I meant that if I want to apply a “force”(yes, i know it’s not the proper word, but that’s how the function is named, AddForce) directly to the velocity like this(https://docs.unity3d.com/ScriptReference/ForceMode.VelocityChange.html) I would need to factor in the body’s weight when since 2D always takes mass into account in AddForce, so just asking if modifying .velocity is perfectly okay in Box2D

I know it wouldn’t hold for a literal 100%, but bouncing around in a peggle-style enviroment for a few minutes without speed loss(measured to 3 decimal points) i consider a win here, thanks for the tip on the position drift (;

What I ended up doing is setting bounciness to 1 and friction to 0 on all objects and OnCollisionEnter2D just multiplies the player .velocity with the object’s “bounceMod” variable, works as expected on first glance, tomorrow will do some more testing to see if and quirks emerges from this.

Am I correct in assuming the execution order is
FixedUpdate->OnColEnter->OnColStay->OnColExit->FixedUpdate
(assuming the object bounced off in a single frame) and it’s fine to modify velocity in Enter or is it
FixedUpdate->OnColEnter->FixedUpdate->OnColStay->FixedUpdate->OnColExit->FixedUpdate
and I should modify the speed at Exit?
I only put it on Enter because I have a callback system with a LUT hooked there so all the object don’t have scripts themselves and the player just says “Yo, I collided with this thing, find out what it needs to do” basically. the game is like a mix between Peggle and Icy Tower (upwards scroller/platformer) incase you wonder.

sorry for the text wall and thanks for the replies :slight_smile:

Yes, I’m aware. You can do body.velocity += new Vector2(0f, 1f) for instance. That’s an impulse. You can also scale it by the simulation step time to make it a force. There’s no reason at all why you can’t do this. I’m not sure what these side effects are that you’re referring to in physx though but for 2D it’s fine. Maybe it’s just what I said and it’s become incorrect common knowledge that you’ll get undefined behaviour.

It’s not FixedUpdate-> it’s SimulationStep-> because collision callbacks happen at the end of the simulation step. It’s true if you’re using the default of allow physics to run during the fixed-update and not manually simulating or running per-frame. Thought that was important to point out. If you enter/exit in a single update then you get enter/exit. There’s no reason you’d get a stay because that’s when a contact persists from a previous simulation step.

1 Like

https://discussions.unity.com/t/729543/13

just from a quick search.

But what’s said there is what I said. That’s not “weird” at all.

1 Like

I don’t think so…

misunderstanding? language barrier?(english is not my first language) does it even matter? lol

anyways, thanks for the all the help.

Yeah, I don’t follow your point. Anyway, good luck with your project.

1 Like