Fisica's FGearJoint

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