Page 1 of 1

Is it possible to give two different bodies different timesteps in Box2D

Posted: Sun May 07, 2023 10:57 am
by Bobble68
Hello! I've had an idea for a puzzle in my game, in which being in different areas distorts time. The way I was hoping to do this was to just give the different bodies different timesteps, so they either travel faster or slower than normal, without changing things like momentum. However, I'm not able to modify world:update() to my knowledge, and I can't find anything on the wiki that might help. Best I could come up with was having two different worlds with different timesteps, but getting that to work would be incredibly awkward. Anyone have any ideas?

Re: Is it possible to give two different bodies different timesteps in Box2D

Posted: Sun May 07, 2023 11:48 am
by darkfrei
You are need to set the custom dt to each object, for example twice lower means twice slower.
Or extra velocity values and the current time relativity factor.

Re: Is it possible to give two different bodies different timesteps in Box2D

Posted: Sun May 07, 2023 12:23 pm
by Bobble68
darkfrei wrote: Sun May 07, 2023 11:48 am You are need to set the custom dt to each object,
How do I do this? I couldn't find any functions to do that

Re: Is it possible to give two different bodies different timesteps in Box2D

Posted: Sun May 07, 2023 12:36 pm
by pgimeno
You can't. I think darkfrei didn't understand the problem well.

I think your only alternatives are:

- Modify the object in the way you have described, so it behaves like a slower one.
- Make a custom physics engine.

I had the idea to make the object you wanted to update slower static, but I'm not sure if that will work well. Worth trying, maybe. That would go like this:

- Calculate the fraction of dt you want to apply to the object, for example dt/4.
- Grab the velocity and the rotational velocity of the object.
- Set the object to static.
- Update the world by dt - dt/4.
- Set the object to dynamic again. Restore the velocity and the rotational velocity.
- Update the world by dt/4.

Re: Is it possible to give two different bodies different timesteps in Box2D

Posted: Sun May 07, 2023 12:45 pm
by darkfrei
I've never used the Box2D, but I see the hacks to fake it:
https://love2d.org/wiki/Body
1. get velocity https://love2d.org/wiki/Body:getLinearVelocity
2. get position https://love2d.org/wiki/Body:getPosition
3. calculate the dx, dy based on dt and velocity
4. move the body backwards to dx, dy to stop it without changing velocity; to -dx/2, -dy/2 to set the visual velocity twice slower. https://love2d.org/wiki/Body:setPosition
5. If it needed, restore the velocity to the last one https://love2d.org/wiki/Body:setLinearVelocity

But you can always write your on physics, here is my solution for time relativity: https://darkfrei.itch.io/connection-looper

Re: Is it possible to give two different bodies different timesteps in Box2D

Posted: Tue May 09, 2023 11:58 am
by Bobble68
darkfrei wrote: Sun May 07, 2023 12:45 pm I've never used the Box2D, but I see the hacks to fake it:
https://love2d.org/wiki/Body
1. get velocity https://love2d.org/wiki/Body:getLinearVelocity
2. get position https://love2d.org/wiki/Body:getPosition
3. calculate the dx, dy based on dt and velocity
4. move the body backwards to dx, dy to stop it without changing velocity; to -dx/2, -dy/2 to set the visual velocity twice slower. https://love2d.org/wiki/Body:setPosition
5. If it needed, restore the velocity to the last one https://love2d.org/wiki/Body:setLinearVelocity

But you can always write your on physics, here is my solution for time relativity: https://darkfrei.itch.io/connection-looper
Sounds like an interesting idea! I'll give that a try. I don't really want to have to use a new engine at this point, it would be a massive pain. Hopefully sometime soon I'll have something to show off what I'm up to.