Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411431 Posts in 69363 Topics- by 58416 Members - Latest Member: JamesAGreen

April 20, 2024, 03:38:21 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)Box2D eureka moment
Pages: [1]
Print
Author Topic: Box2D eureka moment  (Read 813 times)
bigbear
Level 0
**


View Profile
« on: January 10, 2017, 04:16:00 PM »

Just had a Eureka moment working with Box2D in java. I have a multiplayer client/server model and in order to perform accurate dead reckoning, I actually keep a non-authoritative duplicate parallel Box2d World object. While determining the source of a heinous memory leak I decided to learn a bit more about memory management. Java seems to handle it all for you. I'm not 100% that I understand my solution, hopefully one of you can help explain. For various reasons it was easier to make a new World object (and populate it with the X/Y coordinates sent from the server) than it was to clear it out. Part laziness, part breaking other things. What I think was going on was doing
world = new World(new Vector2(0, 0), true);
every 100ms was leaking a crazy amount of memory that the java garbage collector could not/would not clean up. I was of the understanding there is no java equivelant of free() and dispose() but I know Box2D is a thin java wrapper over C++. When I added world.dispose(); before that line, it fixed my memory issue. Am I right to think that I had to do this because Box2D is written in C++?
Logged
oahda
Level 10
*****



View Profile
« Reply #1 on: January 10, 2017, 04:32:14 PM »

Seems likely. But it seems weird to me that you would keep creating and destroying worlds seemingly every frame to have this leak happen, and not just keep a persistent world object. How come you're doing this, if that is indeed what you are doing, and if not, what are you doing exactly?
Logged

bigbear
Level 0
**


View Profile
« Reply #2 on: January 10, 2017, 05:01:14 PM »

The main world object is stored server side and every 100ms is stepped forward. The server takes out all of the positions and velocities and sends them to connected clients. the clients enter in this position/velocity data into its own local world and then uses it to predict the future (i am working on using your actual calculated latency to make it more exact, but its pretty damn close). by the time the next time sync packet comes, the data in the world is useless because it was non-authoritative dead reckoning. it did not take into account other players altering their velocities by thrusting. So really I am only using box2d world clientside to do a quick prediction of the future.

edit: to more fully answer your question, the reason I chose to not make it persistent is that clearing all of the bodies out of it was more work than resetting the entire object
Logged
oahda
Level 10
*****



View Profile
« Reply #3 on: January 10, 2017, 05:41:51 PM »

Ah, now I get it. Sounds potentially performance-heavy to clear out and reset everything all the time, but I take it your game world is small enough that it works fine?
Logged

bigbear
Level 0
**


View Profile
« Reply #4 on: January 10, 2017, 07:25:05 PM »

Maybe if the server was doing it, it might get heavy, but the server cherry picks what to send the client (based on proximity) so the client is only adding whats nearby to its engine. I have a very old slow laptop (Acer 5733Z) that has onboard graphics and dual-core. The app runs very well now that Box2D isn't out of control and with everything going full blast takes up about 200mb of memory. If my shitty laptop can handle it, I'm sure I have plenty of room to grow.
If ever clearing box2d becomes a hassle, I will use what I know from sync packets to just update instead of delete. But I don't foresee that ever being my bottleneck.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic