Where to use setMassData on a body

  ... the Physics Box2D Extension for AndEngine.

Where to use setMassData on a body

Postby Veslat » Mon Mar 11, 2013 5:52 pm

So, I need to change the mass of a body after its creation; I saw that there is a method setMassData() for a body, so I tried to use that when I needed to and it works, but the app crashes randomly because of that. I though that it could be because I changed something relative to physics outside the onUpdate method of the physics world.

So I overrided it and basically now I have an ArrayList of Runnable and in the onUpdate I check if the array has elements; if it has, I use their run() method; when a body need another mass, I create a Runnable and add it to the array, so that it's fired inside the onUpdate method. Still doesn't work, the mass is changed but it crashes randomly, I don't know what I'm doing wrong...

The code in the onUpdate method looks like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false)
  2. {
  3.       @Override
  4.       public void onUpdate(float pSecondsElapsed)
  5.       {
  6.              super.onUpdate(pSecondsElapsed);
  7.              
  8.             for(int i = 0; i < mRunnableList.size(); i++)
  9.                    mRunnableList.get(i).run();
  10.  
  11.              //I remove all the elements when the for is done
  12.              mRunnableList.clear()
  13.       }
  14. }
  15.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


where mRunnableList is defined as ArrayList<Runnable>.

This is how I add elements to the array:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(bodyHasToChangeMass)
  2. {
  3.       mRunnableList.add(new Runnable(){
  4.        
  5.            @Override
  6.            public void run()
  7.            {
  8.                   MassData massData = new MassData();
  9.  
  10.                   massData.mass = newMassValue;
  11.  
  12.                   body.setMassData(massData);
  13.            }
  14.      });
  15. }
  16.  
Parsed in 0.010 seconds, using GeSHi 1.0.8.4


Can you help me find out where's the problem? Can't I change the mass even inside the onUpdate method of the PhyiscsWorld?
Veslat
 
Posts: 93
Joined: Sat Aug 25, 2012 4:39 pm

Re: Where to use setMassData on a body

Postby RealMayo » Tue Mar 12, 2013 11:30 pm

Don't use ArrayList Runnable.
Instead use JSON and store (body,massdata) pairs inside your JSON.
Then in your onUpdate, have just one Runnable which parses through the JSON and executes whatever it finds in there.
User avatar
RealMayo
 
Posts: 1694
Joined: Sat Sep 03, 2011 9:25 pm
Location: Chicago, IL

Re: Where to use setMassData on a body

Postby Veslat » Wed Mar 13, 2013 11:52 am

I forgot to say that I managed to fix it by using a simple ArrayList of Body objects; when I need to change the mass I add the body there, and in the onUpdate of the physics world I check if the array isn't empty and if it's so, I set the mass (kind of what I was doing before, except the code is directly executed in the for in onUpdate).

The idea of using JSON it's also cool. But still I don't understand, why doesn't it work with runnables?
Veslat
 
Posts: 93
Joined: Sat Aug 25, 2012 4:39 pm

Re: Where to use setMassData on a body

Postby Samdekkol » Thu Aug 13, 2015 8:51 pm

Thank you for the informative posts.
Samdekkol
 
Posts: 15
Joined: Thu Aug 13, 2015 7:21 pm


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 4 guests