Joint and Screen Movement Help!

I've been having extreme difficulty getting joints to be deleted. I'm making my first (sort-of) box2D game. The player is a ragdoll, and what I'm trying to do right now is give the player the ability to launch his arms on command. Yes, it is funny...
My code is organized into functions, so there's one function that creates a ragdoll at a specified position, and the code for the ragdoll is similar to the testBed ragdoll example. I haven't been able to delete joints from the ragdoll outside of the ragdoll function, however if I enter "m_world.DestroyJoint(jdLArm2);" inside of the createRagDoll function, the joint is destroyed. How can the joint be deleted from outside of the function?

Also, a question I had earlier, but probably posted in the wrong place... :
recursive wrote:
So... How would you make it so that instead of having the player always at the screen's center, the screen will move with the player only if he moves close to the boundaries? My code almost works, but it's not quite right (the player can't get back to the center of the screen unless he is at the world's center). It's basically this:


Code:
if(RagDollTorso.GetWorldCenter().x > 450/physScale)
   {scrollX = (450/physScale - RagDollTorso.GetWorldCenter().x) * physScale;}
   else if(RagDollTorso.GetWorldCenter().x < 100/physScale)
   {scrollX = (100/physScale - RagDollTorso.GetWorldCenter().x) * physScale;}


(RagDollTorso is the element of the player that is centered, and 450 and 100 are 100 pixels away from the screen's boundaries)
DestroyJoint won't work inside event handlers as the world is locked. But it should be fine in between steps. Are you passing a reference to the joint?

Also, here's some basic scrolling code.
Code:
var screenBorder = 100;
var x = RagDollTorso.GetWorldCenter().x*phyScale;
var screenX = m_sprite.x + screenWidth/2;
if( x + screenBorder< screenX) screenX = x + screenBorder ;
if( x - screenBorder > screenX) screenX = x - screenBorder ;
sprite.x = screenX - screenWidth/2;
Thank you!

But if removing the joint won't work inside the event handler, then where should the code be?
recursive wrote:
Thank you!

But if removing the joint won't work inside the event handler, then where should the code be?


You could use a boolean and then check it in between steps.

(pseudo code)

Code:
var deleteJoint:Boolean = false;

///// in the event handler//////

if (joint deletion parameters are met)
{
    deleteJoint = true;
}
//////////////////////////

function jointDeleter():void
{
    if(deleteJoint)
    {
          destroy the joint;
          deleteJoint = false;
     }
}
   
Yes! Success!! Thanks!

I put the call to the deleteJoint function in the keyDown event handler, so the arm flying off is linked directly to the a key being pressed. Is that the best way to do it?
BorisTheBrave wrote:
Also, here's some basic scrolling code.
Code:
var screenBorder = 100;
var x = RagDollTorso.GetWorldCenter().x*phyScale;
var screenX = m_sprite.x + screenWidth/2;
if( x + screenBorder< screenX) screenX = x + screenBorder ;
if( x - screenBorder > screenX) screenX = x - screenBorder ;
sprite.x = screenX - screenWidth/2;


This code confuses me...
What is sprite.x and m_sprite.x? And is screenX the variable you use in the userData updating loop?
Sorry, to clairify, the world is locked during Box2D events (i.e. Add, Perist, Remove, Result). Mouse and Key events should be fine.

m_sprite == sprite, that's a typo. 😉
The sprite is meant to be the containing sprite that you draw all your other graphics in, by offsetting it, you make everything scroll. In the testbed, this sprite is called m_sprite, but you'll need to put your own reference in. screenX is meant to represent the x co-ordinate of the center of the screen, and would not be used outside the code given. I think I also missed out a minus sign, so to correct myself:

Code:
var screenBorder = 100;
var x = RagDollTorso.GetWorldCenter().x*phyScale;
var screenX = m_sprite.x + screenWidth/2;
if( x + screenBorder< screenX) screenX = x + screenBorder ;
if( x - screenBorder > screenX) screenX = x - screenBorder ;
m_sprite.x = -screenX - screenWidth/2;
BorisTheBrave wrote:
Sorry, to clairify, the world is locked during Box2D events (i.e. Add, Perist, Remove, Result). Mouse and Key events should be fine.

m_sprite == sprite, that's a typo. 😉
The sprite is meant to be the containing sprite that you draw all your other graphics in, by offsetting it, you make everything scroll. In the testbed, this sprite is called m_sprite, but you'll need to put your own reference in. screenX is meant to represent the x co-ordinate of the center of the screen, and would not be used outside the code given. I think I also missed out a minus sign, so to correct myself:

Code:
var screenBorder = 100;
var x = RagDollTorso.GetWorldCenter().x*phyScale;
var screenX = m_sprite.x + screenWidth/2;
if( x + screenBorder< screenX) screenX = x + screenBorder ;
if( x - screenBorder > screenX) screenX = x - screenBorder ;
m_sprite.x = -screenX - screenWidth/2;


So is m_sprite.x the same thing as m_world.x? Or should I use that to individually move each sprite?
Maybe you can set the position of the ground body in m_world.GetGroundBody()?
Oh, sorry, I think a reply of mine never got submitted. Whoops 😳

No, m_world is a b2World object, m_sprite a MovieClip. You should be creating all your graphics stuff inside a single sprite, rather than directly on the stage, precisely so you can have some global control over all the sprites.

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