Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9006789
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:30:27+00:00 2026-06-16T01:30:27+00:00

I am using Box2D in AndEngine (for Android). My purpose is to create a

  • 0

I am using Box2D in AndEngine (for Android).

My purpose is to create a Force joint whenever 2 objects collide with each other.

When I try to create a Mouse Joint between 2 objects (bodies) during ContactListner process. The application will hang for some time then exit, without any error, just a notification of threads ending.

The Joint creating is OK when I call mEnvironment.CreateForceJoint(..) outside the ContactListener – somewhere while app is running in some physics.UpdateHandler().

Please help me to solve the problems, or find out the reason. Thanks for any help!

This is my code:

public class MyActivity extends SimpleBaseGameActivity {
    private final String DEBUG_TAG = "MyActivity";
    private GEnvironment mEnvironment;
    private PhysicsWorld mPhysicsWorld;

    private MyFixture FIXTURE_PLANET = GlobalSettings.FIXTURE_PLANET;
    private MyFixture FIXTURE_VACUUM = GlobalSettings.FIXTURE_VACUUM;

    // CODE TO CREATE RESOURCES and ENGINE OPTIONS....

    @Override
    protected Scene onCreateScene() {
        Scene scene = new Scene();
        scene.setBackground(new Background(0.8627f, 0.9020f, 1.0f));

        //CODE: creating physic world
        //.....

        //creating game environment
        mEnvironment = new GEnvironment(mPhysicsWorld, scene, mEngine);

        //CODE: creating objects, register and attach them into scene
        GMediaPlanet sunZone = mEnvironment.CreatePlanet(x1, y1, sunTextureRegion, FIXTURE_PLANET);
        GMediaPlanet earthZone = mEnvironment.CreatePlanet(x2, y2, earthTextureRegion, FIXTURE_PLANET);

        // enable contact listener, detect collision between bodies
        mPhysicsWorld.setContactListener(new PlanetContactHandler());

        return scene;
    }

    // ----------------------------------------------------
    // Handling collision between letter cubes
    // ----------------------------------------------------
    /**
     * Handling the collision of GMediaPlanets
     */
    public class PlanetContactHandler implements ContactListener {
        private final String DEBUG_TAG = "CONTACT";

        // if there's one collision, do not handle others or re-handle it
        private boolean mIsColliding = false;

        @Override
        public void beginContact(Contact contact) {
            if (mIsColliding)
                return;

            //-----------------------------------------------
            //suppose colliding objects to be sunZone and earthZone
            //-----------------------------------------------
            Object aTag = contact.getFixtureA().getBody().getUserData();
            Object bTag = contact.getFixtureB().getBody().getUserData();

            if (aTag != null && bTag != null) {
                GMediaPlanet box = null;
                GMediaPlanet cube = null;

                if (aTag instanceof GMediaPlanet
                        && bTag instanceof GMediaPlanet) {
                    box = (GMediaPlanet) aTag;
                    cube = (GMediaPlanet) bTag;
                }

                if (box != null && cube != null) 
                {
                    //!!!!!!!-----------------------------------------------------
                    //This code will HANG the app when called inside contact listner:
                    MouseJoint mTestJoint = mEnvironment.CreateForceJoint(box, cube);
                    //!!!!!!!-----------------------------------------------------

                    Vector2 target = Vector2Pool.obtain(box.GetLocation());
                    mTestJoint.setTarget(target);
                    Vector2Pool.recycle(target);
                }
            }
            mIsColliding = true;
        }

        @Override
        public void endContact(Contact contact) {
            Log.d(DEBUG_TAG, "end colliding!");
            mIsColliding = false;
        }

        @Override
        public void preSolve(Contact contact, Manifold oldManifold) {

        }

        @Override
        public void postSolve(Contact contact, ContactImpulse impulse) {

        }
    }
}

public class GMediaPlanet 
{
    protected IAreaShape mSprite = null;
    protected Body mBody = null;

    public GMediaPlanet()
    {   }

    public Vector2 GetLocation()
    {
        mBody.getPosition();
    }

}//end

public class GEnvironment 
{
    private PhysicsWorld mWorld;
    private Scene mScene;
    private org.andengine.engine.Engine mEngine;

    public GEnvironment(PhysicsWorld pWorld, Scene pScene, org.andengine.engine.Engine pEngine)
    {
        mWorld = pWorld;
        mScene = pScene;
        mEngine = pEngine;
    }

    /** the constructor is hidden, available within Appearances packet only */
    public GMediaPlanet CreatePlanet(float pX, float pY, ITextureRegion pTextureRegion, MyFixture pFixture) 
    {
        GMediaPlanet entity = new GMediaPlanet();
        entity.mSprite = new Sprite(pX, pY, pTextureRegion, mEngine.getVertexBufferObjectManager());
        entity.mBody = PhysicsFactory.createCircleBody(mWorld, entity.mSprite, BodyType.DynamicBody,
                pFixture.GetDef(), GlobalSettings.PIXEL_2_METER);

        mScene.attachChild(entity.mSprite);

        entity.mSprite.setUserData(entity.mBody);
        entity.mBody.setUserData(entity);

        mWorld.registerPhysicsConnector(new PhysicsConnector(entity.mSprite, entity.mBody, true, true));

        return entity;
    }

    // -----------------------------
    // Creating JOINTS
    // -----------------------------
    /**
     * Creating a force joit based on type of MouseJointDef
     * 
     * @param anchorObj
     *            the static object in the mTestJoint (anchor base)
     * @param movingObj
     *            object to move forward the target
     */
    public MouseJoint CreateForceJoint(GMediaPlanet anchorObj, GMediaPlanet movingObj)
    {
        ChangeFixture(movingObj, GlobalSettings.FIXTURE_VACUUM);

        MouseJointDef jointDef = new MouseJointDef();

        jointDef.dampingRatio = GlobalSettings.MOUSE_JOINT_DAMP;
        jointDef.frequencyHz = GlobalSettings.MOUSE_JOINT_FREQ;
        jointDef.collideConnected = true;

        Vector2 initPoint = Vector2Pool.obtain(movingObj.mBody.getPosition());
        jointDef.bodyA = anchorObj.mBody;
        jointDef.bodyB = movingObj.mBody;
        jointDef.maxForce = (GlobalSettings.MOUSE_JOINT_ACCELERATOR * movingObj.mBody.getMass());

        // very important!!!, the initial target must be position of the sattelite
        jointDef.target.set(initPoint);
        MouseJoint joint = (MouseJoint) mWorld.createJoint(jointDef);
        Vector2Pool.recycle(initPoint);

        // very important!!!, the target of the joint then changed to target anchor object
        Vector2 targetPoint = Vector2Pool.obtain(anchorObj.mBody.getWorldCenter());
        joint.setTarget(targetPoint);
        Vector2Pool.recycle(targetPoint);

        return joint;
    }

    public void ChangeFixture(GMediaPlanet entity, MyFixture pFixture)
    {
        Filter fil = new Filter();
        fil.categoryBits = pFixture.categoryBit;
        fil.maskBits = pFixture.maskBits;
        if(entity.mBody != null)
        {
            entity.mBody.getFixtureList().get(0).setFilterData(fil);            
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:30:28+00:00Added an answer on June 16, 2026 at 1:30 am

    You can not modify the world in Step()-Call of Box2D because the World is locked! you should get an exception. You have to Remember which objects are colliding and do stuff after beginContact.. for example in an update function.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using AndEngine and Box2d to develop a game. I'm applying a force to
I'm using Box2D on my iPhone app using Cocos2D. I'm using mouse joint to
I'm developing a little Android game in Java, using AndEngine for graphics and Box2D
I'm using AndEngine/Box2D to create a game. When the game loads, an array of
I'm developing 2D Side scroll Android Game, using AndEngine and its BOX2D extension. I
How to make a pendulum in box2d and AndEngine using Joint? Can set an
I'm using AndEngine/Box2d to develop a game. I have a ball that bounces around
I'm developing a physics game using AndEngine with box2d physics engine. I have a
I'm using Box2D through Libgdx to create a scene. I have a scenario where
I am new to Android game development and I'm using AndEngine GLES 2 Java.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.