Restrict Angular/Linear Velocity to 2D in jBullet

avejidah
Posts: 4
Joined: Fri May 08, 2015 5:01 pm

Restrict Angular/Linear Velocity to 2D in jBullet

Post by avejidah »

I'm working on a game that uses jBullet for the physics engine. It's a race game that is similar to Uniracers or Free Rider 2 and is, loosely speaking, two dimensional. I'm having trouble restricting the engine to two dimensions.

Here's a quick video that shows how the game works, and also demonstrates the issue that I'm trying to resolve. The issue crops up at around 24 seconds where the bicycle falls of the track.

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

The bicycle is a RaycastVehicle, using a CompoundShape and a RigidBody for the chassis. (I wasn't able to find a jBullet example that uses RaycastVehicle per se on the internet, but I was able to find a few examples in Bullet and port these over to Java. FWIW I'm perfectly comfortable with C and C++, so Bullet-specific tips/help are fine.) Right now I'm using a HingeConstraint with "angularOnly" set to true. That works pretty well until there is a major collision.

In the video, the bike lands hard around 20 seconds and the rotation gets skewed slightly it seems. Then the player falls off the track. This is the behavior I'm trying to prevent.

I found that Bullet can act as a 2D engine by setting a RigidBody's linear/angular factor appropriately. In jBullet, however, RigidBody.setLinearFactor and RigidBody.setAngularFactor take float parameters rather than vectors, so it's all or nothing. Rotation and translation can't be restricted on a per-axis basis as far as I can tell.

I also tried calling setAngularVelocity and setLinearVelocity just after stepping the simulation. That seemed to help somewhat, but the issue remains. I speculate that the issue is that the engine is doing some interpolation during the stepSimulation method when the maxSubSteps parameter is greater than 1. In my case the max number of sub steps is set to 10 or so to prevent losing time.

At any rate, how can I keep rotation restricted to rotation about the x axis, and translations restricted to the y-z plane? I feel like the hinge constraint is super close (!!) but I can't figure out why large collisions cause the bike to rotate slightly, or how to fix the problem.

Thank you in advance for the help!