Jittering objects in PhysicsExample.java

  ... the Physics Box2D Extension for AndEngine.

Jittering objects in PhysicsExample.java

Postby Selzier » Tue Sep 14, 2010 8:40 am

When you create a physics object from the example and let them fall to the ground, they then begin to bounce up and down (and even rotate) by themselves. Now I've looked over and over the source for what is making this happen but can't find it. I've included a basic physics example with a box which randomly bounces up and down.

-- I'm not referring to Restitution, I see where that is set in >>PhysicsFactory.createFixtureDef- bouncing when the object hits the ground is ok, I'm talking about the random "jittering/bouncing" you see when the object comes to rest on the ground.

What is making this happen?? :oops:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class TestPhysics extends BaseGameActivity implements IAccelerometerListener { //, IOnSceneTouchListener
  2.     // ===========================================================
  3.     // Constants
  4.     // ===========================================================
  5.  
  6.     protected static final int CAMERA_WIDTH = 720;
  7.     protected static final int CAMERA_HEIGHT = 480;
  8.  
  9.     // ===========================================================
  10.     // Fields
  11.     // ===========================================================
  12.  
  13.     private Texture mTexture;
  14.     protected TextureRegion mBoxTextureRegion;
  15.  
  16.     protected PhysicsWorld mPhysicsWorld;
  17.  
  18.     // ===========================================================
  19.     // Methods for/from SuperClass/Interfaces
  20.     // ===========================================================
  21.  
  22.     @Override
  23.     public Engine onLoadEngine() {
  24.         //Toast.makeText(this, "Touch the screen to add objects.", Toast.LENGTH_LONG).show();
  25.         final Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
  26.         return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), camera));
  27.     }
  28.  
  29.     @Override
  30.     public void onLoadResources() {
  31.         this.mTexture = new Texture(64, 64, TextureOptions.BILINEAR);
  32.         TextureRegionFactory.setAssetBasePath("gfx/");
  33.         this.mBoxTextureRegion = TextureRegionFactory.createFromAsset(this.mTexture, this, "box.png", 0, 0);
  34.         this.mEngine.getTextureManager().loadTexture(this.mTexture);
  35.  
  36.         this.enableAccelerometerSensor(this);
  37.     }
  38.  
  39.     @Override
  40.     public Scene onLoadScene() {
  41.         this.mEngine.registerUpdateHandler(new FPSLogger());
  42.  
  43.         final Scene scene = new Scene(2);
  44.         scene.setBackground(new ColorBackground(0, 0, 0));
  45.        
  46.         this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
  47.        
  48.         this.initJoints(scene);
  49.  
  50.         final Shape ground = new Rectangle(0, CAMERA_HEIGHT - 2, CAMERA_WIDTH, 2);
  51.         final Shape roof = new Rectangle(0, 0, CAMERA_WIDTH, 2);
  52.         final Shape left = new Rectangle(0, 0, 2, CAMERA_HEIGHT);
  53.         final Shape right = new Rectangle(CAMERA_WIDTH - 2, 0, 2, CAMERA_HEIGHT);
  54.  
  55.         final FixtureDef wallFixtureDef = PhysicsFactory.createFixtureDef(0, 0.5f, 0.5f);
  56.        
  57.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, ground, BodyType.StaticBody, wallFixtureDef);
  58.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, roof, BodyType.StaticBody, wallFixtureDef);
  59.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, left, BodyType.StaticBody, wallFixtureDef);
  60.         PhysicsFactory.createBoxBody(this.mPhysicsWorld, right, BodyType.StaticBody, wallFixtureDef);
  61.  
  62.         scene.getBottomLayer().addEntity(ground);
  63.         scene.getBottomLayer().addEntity(roof);
  64.         scene.getBottomLayer().addEntity(left);
  65.         scene.getBottomLayer().addEntity(right);
  66.  
  67.         scene.registerUpdateHandler(this.mPhysicsWorld);
  68.  
  69.         return scene;
  70.     }
  71.  
  72.     public void onLoadComplete() {
  73.  
  74.     }
  75.  
  76.     @Override
  77.     public void onAccelerometerChanged(final AccelerometerData pAccelerometerData) {
  78.         this.mPhysicsWorld.setGravity(new Vector2(pAccelerometerData.getY(), pAccelerometerData.getX()));
  79.     }
  80.  
  81.     // ===========================================================
  82.     // Methods
  83.     // ===========================================================
  84.    
  85.     private void initJoints(final Scene pScene) {
  86.         final int centerX = CAMERA_WIDTH / 2;
  87.         final int centerY = CAMERA_HEIGHT / 2;
  88.        
  89.         final FixtureDef objectFixtureDef = PhysicsFactory.createFixtureDef(10, 0.2f, 0.5f);
  90.         final Sprite physicsSpriteBox = new Sprite(centerX, centerY, this.mBoxTextureRegion);
  91.         final Body physicsBodyBox = PhysicsFactory.createBoxBody(this.mPhysicsWorld, physicsSpriteBox, BodyType.DynamicBody, objectFixtureDef);
  92.        
  93.         physicsSpriteBox.setUpdatePhysics(false);
  94.         pScene.getTopLayer().addEntity(physicsSpriteBox);
  95.         this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(physicsSpriteBox, physicsBodyBox, true, true, false, false));
  96.     }
  97. }
Parsed in 0.045 seconds, using GeSHi 1.0.8.4
Selzier
 
Posts: 10
Joined: Sat Sep 11, 2010 7:08 pm

Re: Bouncing faces in PhysicsExample.java

Postby blandonfrank » Tue Sep 14, 2010 6:08 pm

if you use FixedStepPhysicsWorld the jittering should stop. I'm not sure why this happens.
blandonfrank
 
Posts: 298
Joined: Fri Jul 23, 2010 9:46 pm

Re: Bouncing faces in PhysicsExample.java

Postby Selzier » Tue Sep 14, 2010 10:15 pm

You're right, thanks! I changed from a PhysicsWorld:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


To a FixedStepPhysicsWorld and it fixed the random bouncing and rotating.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. this.mPhysicsWorld = new FixedStepPhysicsWorld(30, new Vector2(0, SensorManager.GRAVITY_EARTH), false);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


I've been looking through PhysicsWorld.java and FixedStepPhysicsWorld.java to find out why the physics objects in a normal PhysicsWorld are bouncy/jittery but haven't been able to pick it out.

If anyone has an answer please post it!
Selzier
 
Posts: 10
Joined: Sat Sep 11, 2010 7:08 pm

Re: Bouncing faces in PhysicsExample.java

Postby Nicolas Gramlich » Fri Sep 17, 2010 11:16 am

The reason is most likely that simulations assume that they are run with a constant timestep. Imaging the GC is running and suddenly a frame is 5x longer than all the previous --> jittering.

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

Re: Jittering objects in PhysicsExample.java

Postby mhuggins » Sun Oct 17, 2010 10:04 pm

Thanks for this post and the responses. I had the same problem as the OP, and the issue seemed to disappear when I removed this line from my code:

getEngine().registerUpdateHandler(new FPSLogger());

After switching to a FixedStepPhysicsWorld, I was able to re-enable the above line without any problems.
mhuggins
 
Posts: 17
Joined: Sun Sep 26, 2010 11:37 pm


Return to Physics Box2D

Who is online

Users browsing this forum: No registered users and 7 guests