Multiplayer game in AS3 using Box2D

Hello,

I am attempting to create a multiplayer game that utilizes Box2D as a physics engine. At my current stage of development, my goal is to simply have one player's screen 'mirror' the other player's screen.

On the 'Controller' screen, individual bodies stacked in a column can be destroyed, and I want the resulting collapses to properly display on both the Controller screen and the Spectator screen. To accomplish this, I am building the same level for both players, and then attempting to pass the body that has been destroyed by the Controller to the Spectator so that it can destroyed there too.

The issue is that the method I am using to deliver the target body to the spectator is not working. I am using remote shared objects, and the body I pull out of the shared object on the spectator side does not seem to function properly.

Below is the code I use on the Controller side to destroy bodies:
Code:
if (world.GetBodyList().IsAwake() == false)
{
   Main.targettingSO.setProperty("body", body);
   world.DestroyBody(Main.targettingSO.data.body);
}


Obviously targettingSO is my shared object, which has already been properly configured at this point. This DOES work. However, the code below on the Spectator end of things does not work:
Code:
if (targettingSO.data.body != null)
{
   currentLevel.world.DestroyBody(targettingSO.data.body);
}


I have tried a number of variations of this, such as casting targettingSO.data.body as a b2Body object and passing that to the DestroyBody method, but no dice. No matter what I do, it actually seems to cause the program to hang, as alert statements after that call to DestroyBody never execute. It was my assumption that a body passed like this between two different instances of the same program would be usable in this way, but my best guess now is that the body on the Controller end is not identical to the body on the Spectator end, even though they are in the sense of the game.

Could anyone offer any advice as to how I might accomplish this transfer of information from one client to the other?
Instead of referencing a body directly( which they will never be the same ) store an index or body ID in the shared object and look up the body using that. You can attach the id to your userData, or to the class you use as an 'actor' in your userData.

Incidently, the reason they will never be the same is because everything in flash is a reference to an object, not the actualy object. You never directly handle the object that is in memory, only references to it.
Thank you VERY much for revealing my (rookie) error. As a consequence of this, I've totally redesigned what I'm doing, while following along with the tutorial videos at http://www.kerp.net/box2d/index.php (while taking into account the changes from 2.0 to 2.1). I think what I'm doing now is far more robust and 'proper', so to speak.

However, I am experiencing a new issue that perhaps someone could enlighten me on.

The problem is that the DestroyBody function doesn't seem to be functioning properly, due to SOMETHING I have done or forgot to do. I have endeavored to make sure that this method is only ever called at the proper time, to avoid trying to destroy bodies while the physics world is updating, however this appears to be unrelated to that.

The best way I can describe the error is this:
1) I have a world populated with some bodies. The general idea is that clicking on a body should destroy it.
2) When I click on a body, this occurs properly: the body and associated sprite graphic are removed (debugdraw demonstrates that the body is well and truly gone)
3) However, clicking on a SECOND body produces...nothing.

I have narrowed this down to the world not being recognized as sleeping. Before I allow a body to be destroyed, I have been checking if gameWorld.GetBodyList().IsAwake() == false. After destroying the first body, this is always coming back as true, no matter what.

Any help would be GREATLY appreciated...I'm so close to success! 🙂
i don't see why a body would need to be awake to be destroyed. however, there is a function to wake up bodies manually.
Sorry, I should have been more clear. I'm imposing that restriction for game mechanics purposes. Essentially I don't want more than one piece destroyed at a time, before the level has had a chance to 'settle' from whatever movements occurred from the first destruction.
mWorld.GetBodyList() returns a linked list of bodies that are attached to the physics world. So getting mWorld.GetBodyList().isAwake() just tells you if the first body is awake or not. Box2D's b2World class initializes with a body that is reference through the m_GroundBody variable and exposed through the GetGroundBody() accessor. I'm not sure what wakes this body up, or puts it to sleep... but clearly something is. You might need a different way to determine if your simulatio is 'at rest' than checking a single body for its awake status.

If I have misread this I apologize in advance but what I would suggest is to iterate over the entire body list and sum up the kinetic energy( Ke = m*v^2 / 2 ) and checking this against a threshold value. I suggest this since under certain circumstances you may get occilating bodies that don't sleep as quickly as you'd like. Once the total kinetic energy of the system is low enough, re-enable your destruction algorithm.
Hmm, foolish error for me to make given that I CORRECTLY used GetBodyList elsewhere.

With that corrected, however, I still have the issue...I will attempt to devise a new way to determine if the world is at rest. In the meantime if anyone knows what might be waking the GroundBody up, please let me know.

Original thread: https://box2d.org/forum/viewtopic.php?t=4859