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

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

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

Post 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?
Dragon
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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.
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

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

Post 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
Dragon
User avatar
pgimeno
Party member
Posts: 3691
Joined: Sun Oct 18, 2015 2:58 pm

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

Post 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.
Last edited by pgimeno on Sun May 07, 2023 12:49 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1216
Joined: Sat Feb 08, 2020 11:09 pm

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

Post 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
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
User avatar
Bobble68
Party member
Posts: 162
Joined: Wed Nov 30, 2022 9:16 pm
Contact:

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

Post 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.
Dragon
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 7 guests