Problem with moving a body using a thread

  ... the Physics Box2D Extension for AndEngine.

Problem with moving a body using a thread

Postby refuse » Thu Jun 09, 2011 1:49 pm

Hello,

I have a problem with threading. In my game I'd like to move Body objects using setTransform function. I want those objects to move smoothly so I created a thread, which is responsible for that. What happens inside the thread is a for loop calling setTransform and changing the position of a body by small units. The object changes its position eventually but it jumps from a current position to the new one when the thread finishes. What can cause such a behaviour?

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 scene.registerUpdateHandler(new IUpdateHandler(){
  2.                         @Override
  3.                         public void onUpdate(float pSecondsElapsed) {
  4.                                 c+=1;
  5.                                 t = new Thread(new Runnable() {
  6.                                         @Override
  7.                                     public void run() {
  8.                                                 while(c % 20==0){
  9.                                                 int i = generator.nextInt(GameTrafficS.size());                                                
  10.                                                 changeLane(GameTrafficS.get(i),GameTraffic.get(i));
  11.                                         }
  12.                                     }
  13.                                   });
  14.                                 if(c % 20 ==0)
  15.                                         runOnUpdateThread(t);
  16.                                 //t.start();
  17.                                 //runOnUiThread(t);                            
  18.                                 Vector2 Coordinates = carBody.getPosition();
  19.                                
  20.                                 if(buttonLeft.state == 1 && Coordinates.x>MinPos)
  21.                                 {
  22.                                         Vector2 position = new Vector2(carBody.getPosition().x - 8.0f / PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT,carBody.getPosition().y);
  23.                                         carBody.setTransform(position, 0);
  24.                                 }
  25.                                 if(buttonRight.state == 1 && Coordinates.x<MaxPos)
  26.                                 {
  27.                                         Vector2 position = new Vector2(carBody.getPosition().x + 8.0f / PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT,carBody.getPosition().y);
  28.                                         carBody.setTransform(position, 0);
  29.                                 }
  30.                                 if(buttonUp.state == 1 && speed > -10.0f)
  31.                                 {
  32.                                         speed -= 1.0f;
  33.                                 }
  34.                                 if(buttonDown.state == 1 && speed < -2.0f)
  35.                                 {
  36.                                         speed += 2.0f;
  37.                                 }
  38.                                 carBody.setLinearVelocity(new Vector2(0,speed));
  39.                                 mPhysicsWorld.onUpdate(pSecondsElapsed);                               
  40.                                 mCamera.setCenter(240, Coordinates.y*PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT-150);
  41.                                 mBackgroundTextureRegion.setTexturePosition(0, (int)(Coordinates.y*PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT));
  42.                                
  43.                                 moveTrafficCar(scene);
  44.                         }
  45.                        
  46.                         @Override
  47.                         public void reset() {
  48.                                 // TODO Auto-generated method stub
  49.                         }});
  50.  
Parsed in 0.015 seconds, using GeSHi 1.0.8.4


Here's the function I call inside my thread :

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         private synchronized void changeLane(Traffic carSprite,Body carBody)
  2.         {
  3.                
  4.                 int count = GameTrafficS.size();
  5.                 int lane = 0;          
  6.                 Boolean contains = false;
  7.                
  8.                 if(carSprite.lane == 1)
  9.                         lane = 1;
  10.                 else if(carSprite.lane == 4)
  11.                         lane = -1;
  12.                 else
  13.                         lane = generator.nextBoolean() ? 1 : -1;
  14.                
  15.                 carSprite.lane += lane;
  16.                 float posX = 70+(carSprite.lane-1)*115;
  17.                 for(int i = 0; i < count; i++)
  18.                 {
  19.                         if(GameTrafficS.get(i).contains(posX, carSprite.getY()))
  20.                                 contains = true;
  21.                 }
  22.                
  23.                 if(!contains)
  24.                 {
  25.                         float diff = Math.abs(carSprite.getX()-posX);
  26.                         for(int i=0;i<diff;i++)
  27.                         {
  28.                                 carBody.setTransform(new Vector2(carBody.getPosition().x+lane/PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT,carBody.getPosition().y), 0);
  29.                         }
  30.                 }
  31.         }
  32.  
Parsed in 0.014 seconds, using GeSHi 1.0.8.4
refuse
 
Posts: 1
Joined: Thu Jun 09, 2011 1:13 pm

Re: Problem with moving a body using a thread

Postby Nicolas Gramlich » Sat Jun 11, 2011 2:18 am

Hi,

(I didn't read through your whole code, but) creating a thread does not really make sense, as why would you update the physics more often than it actually steps :?:

Also a thread won't make anything smooth here, as it's a Thread and not guaranteed to run now or maybe a bit later. (Use pSecondsElapsed to make things smooth.)

Also you should be diviging by PhysicsConnector.PIXEL_TO_METER_RATIO_DEFAULT and not multiply with it, as you are now making things 1000 bigger than they naturally are. (Can cause totally weird physics!)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: Problem with moving a body using a thread

Postby vikingorrino » Sat Oct 13, 2012 9:09 pm

Hi! I've been having the same problem with moving a body with physics. If you use mouseJoint (AndEngineExamples has an example for it) you can make your body move smoothly I think.
vikingorrino
 
Posts: 7
Joined: Tue Jul 31, 2012 8:49 pm

Re: Problem with moving a body using a thread

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

I really liked this information.
Samdekkol
 
Posts: 15
Joined: Thu Aug 13, 2015 7:21 pm


Return to Physics Box2D

Who is online

Users browsing this forum: Google [Bot] and 5 guests