stephengold / Libbulletjme

A JNI interface to Bullet Physics and V-HACD
https://stephengold.github.io/Libbulletjme/lbj-en/English/overview.html
Other
84 stars 10 forks source link

Gear Joint Constraint #17

Closed elmfrain closed 2 years ago

elmfrain commented 2 years ago

Gear Joint Constraint

Implemented Bullet's btGearConstraint in Java with the GearJoint class.

Why I Added This

I am using this library to simulate a vehicle for a future project, and I am modifying it so it can be suited for that. The gear constraint is crucial to simulate the gearbox, axles, and differential. Also, since this feature is mentioned on the "Whats Missing" section, I took it as an opportunity to contribute.

Demo

In my application, there is a block (called 'testGear') on top of the vehicle that is hinged to the chassis and gear constrained to the front right wheel.

PhysicsRigidBody testGear = vparts.get("testGear").body;
PhysicsRigidBody frWheel = vparts.get("frWheel").body;

GearJoint gearJoint = new GearJoint(frWheel, testGear, Vector3f.UNIT_Z, Vector3f.UNIT_Y, 1.0f);
space.add(gearJoint);

And here is the result:

https://www.youtube.com/watch?v=rAgXH9XIMvE

YanisBDF commented 2 years ago

Hello, I saw that you create Minecraft Mods.

Are you planning to use this on a Minecraft project ?

elmfrain commented 2 years ago

Haha yeah! I would like to create an offroading kart that simulates the suspension of a real vehicle. It's a dream of mine since I started to learn to code.

YanisBDF commented 2 years ago

Ah great ! Good luck !

Do you have en email or a discord so we can chat ?

elmfrain commented 2 years ago

Yeah my email is elmfer10@gmail.com.

stephengold commented 2 years ago

@elmfrain: I appreciate your contribution and will study it soon.

In the future, contact me before writing code you plan to contribute. I'd hate to accidentally duplicate your efforts!

elmfrain commented 2 years ago

Sure thing! I'll let you know next time I plan to contribute another feature.

stephengold commented 2 years ago

@elmfrain: I'd like to request a couple changes, please:

  1. The additions to ".gitignore" are unrelated to the new feature and should be backed out prior to integration.
  2. Rather than validating vectors as having unit length, I prefer to validate them as non-zero. That way there's no arbitrary boundary (between unit vectors and non-unit vectors) at which roundoff errors crash the app.

What do you think?

elmfrain commented 2 years ago

@stephengold: I did some tests on what bullet will do if you passed in a non-unit vector in setAxis(). It turns out that a non-unit vector essentially multiplies its magnitude with the gear ratio of the constraint, but nothing crazy happened. This is undesirable, but I think we can agree that the user is responsible to make sure to pass in a unit vector.

Additionally, I do agree about your concern about the arbitrary boundary between unit vectors and non-unit vectors because the margin for error is small; a small rounding error can unnecessarily crash an entire application.

stephengold commented 2 years ago

@elmfrain Thanks for your contribution to the project!