Fisica's FGearJoint

Hi all,

does anyone have experience with setting up an FGearJoint in Fisica properly?

I have attached a little study in which I try to connect two FRevoluteJoints by an FGearJoint. Unfortunately, the Constructor of the FGearJoint keeps producing a Null Pointer Exception, although, according to the Fisica documentation, everything should be fine.

Could anyone tell me how to modify my code in order to get the FGearJoint going? I’ve been searching around the internet for a working example but couldn’t find anything.

Many thanks in advance & all the best to everybody,
ilinx

import fisica.*;

FBox            wheelA,     wheelB;
FCircle         hubA,       hubB;
FRevoluteJoint  revJointA,  revJointB;

FGearJoint gearJoint;

FWorld world;

void setup() {
  size(400, 400);
  Fisica.init(this);
  world = new FWorld();
  
  // Wheel A /////////////////////////////////////
  wheelA = new FBox(80,80);
  wheelA.setPosition(100,200);
  world.add(wheelA);
  
  hubA = new FCircle(10);
  hubA.setPosition(100,200);
  hubA.setStatic(true);
  world.add(hubA);
  
  revJointA = new FRevoluteJoint(wheelA,hubA);
  world.add(revJointA);
  ////////////////////////////////////////////////
  
  // Wheel B /////////////////////////////////////
  wheelB = new FBox(100,100);
  wheelB.setPosition(300,200);
  world.add(wheelB);
  
  hubB = new FCircle(10);
  hubB.setPosition(300,200);
  hubB.setStatic(true);
  world.add(hubB);
  
  revJointB = new FRevoluteJoint(wheelB,hubB);
  world.add(revJointB);
  ////////////////////////////////////////////////
  
  // Connecting the two Joints ... ///////////////
  gearJoint = new FGearJoint(revJointA,revJointB);
  gearJoint.setRatio(2.0);
  world.add(gearJoint);
  ////////////////////////////////////////////////
  
}

void draw() {
  background(255);
  world.step();
  world.draw();
}
1 Like

I’ve taken a quick look at Fisica’s source code and found out the reason for the NPE is that invoking method getBox2dJoint() on both FRevoluteJoint objects return null:

println(revJointA.getBox2dJoint()); // null
println(revJointB.getBox2dJoint()); // null

But mysteriously after the 1st draw() iteration, those same statements don’t log null anymore.

So I’ve used registerMethod() in order to setup a post() callback which would be run once only and then got unregisterMethod():
Processing.GitHub.io/processing-javadocs/core/processing/core/PApplet.html#registerMethod-java.lang.String-java.lang.Object-

Into that post() callback I’ve moved the code block which creates the FGearJoint.

This time it works b/c method getBox2dJoint() doesn’t return null anymore.

However, the next call to method step() somehow triggers an AssertionError.

As a workaround I’ve forced 1 single call to method step() within a try {} / catch () {} block.

From that on, step() doesn’t trigger any more errors.

This is your code w/ those workaround changes. Good luck: :four_leaf_clover:

// https://Discourse.Processing.org/t/fisicas-fgearjoint/18037/2
// 2020-Feb-23

import fisica.*;

FWorld          world;
FBox            wheelA, wheelB;
FCircle         hubA, hubB;
FRevoluteJoint  revJointA, revJointB;
FGearJoint      gearJoint;

void setup() {
  size(400, 400);

  Fisica.init(this);
  world = new FWorld();

  // Wheel A /////////////////////////////////////
  wheelA = new FBox(80, 80);
  wheelA.setPosition(100, 200);
  world.add(wheelA);

  hubA = new FCircle(10);
  hubA.setPosition(100, 200);
  hubA.setStatic(true);
  world.add(hubA);

  revJointA = new FRevoluteJoint(wheelA, hubA);
  world.add(revJointA);
  ////////////////////////////////////////////////

  // Wheel B /////////////////////////////////////
  wheelB = new FBox(100, 100);
  wheelB.setPosition(300, 200);
  world.add(wheelB);

  hubB = new FCircle(10);
  hubB.setPosition(300, 200);
  hubB.setStatic(true);
  world.add(hubB);

  revJointB = new FRevoluteJoint(wheelB, hubB);
  world.add(revJointB);
  ////////////////////////////////////////////////

  registerMethod("post", this);

  println(revJointA.getBox2dJoint());
  println(revJointB.getBox2dJoint());
}

void draw() {
  background(-1);
  world.step();
  world.draw();
}

void post() {
  unregisterMethod("post", this);

  println(revJointA.getBox2dJoint());
  println(revJointB.getBox2dJoint());

  // Connecting the two Joints ... //////////////////////////////////////
  FGearJoint gearJoint = new FGearJoint(revJointA, revJointB);
  gearJoint.setRatio(2);
  world.add(gearJoint);
  ////////////////////////////////////////////////////////////////////////

  try {
    world.step();
  }

  catch (final AssertionError err) {
    System.err.println(err);
  }
}
2 Likes

Dear GoToLoop,

sorry for my late reply, I was busy with other things and could come back to the FGearJoint issue only now.

Thanks a lot for your attempt that taught me a lot about the pre/post/dispose/… methods, but unfortunately the FGearJoint still doesn’t work. Right after trying the world.step the FGearJoint exists, that’s true. However, in the next frame, it logs as null again. I have messed around with your approach quite a bit (frame by frame) but couldn’t find a proper order to initialize the joints.

Right now, while trying to find workarounds, I am considering to contact Ricard Marxer directly. I’ll keep you posted.

All the best,
ilinx