# Fisica's FGearJoint

**URL:** <https://discourse.processing.org/t/fisicas-fgearjoint/18037>\
**Category:** Libraries\
**Created:** [February 23, 2020, 6:19am UTC](https://discourse.processing.org/t/fisicas-fgearjoint/18037 "2020-02-23T06:19:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ilinx](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@ilinx](https://discourse.processing.org/u/ilinx)\
**Post date:** [February 23, 2020, 6:19am UTC](https://discourse.processing.org/t/fisicas-fgearjoint/18037/1 "2020-02-23T06:19:07Z")

</div>

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](http://www.ricardmarxer.com/fisica/reference/fisica/FGearJoint.html), 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

```auto
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();
}

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [February 23, 2020, 8:28am UTC](https://discourse.processing.org/t/fisicas-fgearjoint/18037/2 "2020-02-23T08:28:09Z")

</div>

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`:

```auto
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-](http://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: 🍀

```auto
// 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);
  }
}

```

---

<div class="post-metadata">

**Author:** ![ilinx](https://avatars.discourse-cdn.com/v4/letter/i/7cd45c/32.png) [@ilinx](https://discourse.processing.org/u/ilinx)\
**Post date:** [March 8, 2020, 9:56pm UTC](https://discourse.processing.org/t/fisicas-fgearjoint/18037/3 "2020-03-08T21:56:01Z")

</div>

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
