Bullet physics with modern openGL lwjgl

Started by
22 comments, last by orange451 5 years, 8 months ago

Maybe you already have, but make sure you've tried both orders (rotate->translate, and translate->rotate). If it still doesn't work, post the code where you build the full transform (that is, show both calls, the one to rotate() and the one to translate()).

Advertisement

Not sure it's a direct help but it's stuff I came across when converting Bullet  C+ to XNA /C# :

Bullet doesn't have any in built dependency on drawing things / renderer. All that is done by an implementation of the DebugDraw interface. It's relatively easy to have your own version that uses VertexBuffers etc rather than immediate mode commands. I haven't got an up to date Java version, but this code in the xna version might be of help.

https://github.com/xexuxjy/bullet-xna

Communication with the internals of bullet is best done via MotionState objects/interfaces. Bullet will write to / query the values there automatically. You might want to write your own implantation of that rather than the default one , it's a very simple class and it lets you keep all your matrix order issues in one place.

You need to use the MotionStates consistently and get the matrix values from them ,rather than getting the Transform directly from the RigidBody - I suspect some of the issues you're getting are because you're using a couple of different ways to get /set the data.

Hope this gets you a bit further.....

 

 

Additionally, seeing as you’re using LWJGL3, you might get more java-related answers on LWJGLs website or the java gaming forums. Both have a lot of programmers that use lwjgl.

8 hours ago, xexuxjy said:

Not sure it's a direct help but it's stuff I came across when converting Bullet  C+ to XNA /C# :

Bullet doesn't have any in built dependency on drawing things / renderer. All that is done by an implementation of the DebugDraw interface. It's relatively easy to have your own version that uses VertexBuffers etc rather than immediate mode commands. I haven't got an up to date Java version, but this code in the xna version might be of help.

https://github.com/xexuxjy/bullet-xna

Communication with the internals of bullet is best done via MotionState objects/interfaces. Bullet will write to / query the values there automatically. You might want to write your own implantation of that rather than the default one , it's a very simple class and it lets you keep all your matrix order issues in one place.

You need to use the MotionStates consistently and get the matrix values from them ,rather than getting the Transform directly from the RigidBody - I suspect some of the issues you're getting are because you're using a couple of different ways to get /set the data.

Hope this gets you a bit further.....

 

 

Unfortunately, It didn't work even when i tried rigidBody.getMotionState().getWorldTransform(...) :( I would really really appreciate if you would give me a link to tutorials on Bullet with modern OpenGL.. thanks in advance!! 

21 minutes ago, Ali Aman said:

Unfortunately, It didn't work even when i tried rigidBody.getMotionState().getWorldTransform(...) :( I would really really appreciate if you would give me a link to tutorials on Bullet with modern OpenGL.. thanks in advance!! 

Someone might be able to recommend a tutorial, but I wouldn't give up on solving the issue in the meantime. I suspect that whatever the issue is, it may be fairly localized and easy to fix.

Here's another diagnostic you could try. Ignore the Bullet rotations for now. Instead, in your own code, apply some sort of constant rotation to your objects, like maybe constant rotation about a world axis. If that works, it will suggest that the problem is in how you're interpreting or applying Bullet's transform data. If it doesn't work, it will suggest it's strictly a problem of how you're dealing with transforms in OpenGL/LWJGL.

I'll have a look, but I agree with Zakwayda, I think you are getting side tracked by the OpenGL stuff for Bullet - it really doesn't have any dependencies on OpenGL except as an optional debug drawer to help visualise the Physics Simulation.If you debug your program you should be able to inspect the fields for the Matrix in your MotionState class, from there it should be fairly obvious which parts make up the position and which are the rotation values.  Once you've looked at that, compare it to the Matrices the rest of your program is using and the worst you'll probably have to figure out is whether it is  AxB or BxA  or if you have to Transpose one of the matrices before you multiply. 

This is what I ended up having for the XNA DefaultMotionState :

https://github.com/ousttrue/bullet-xna/blob/master/BulletXNA/DefaultMotionState.cs

which if I remember had a different matrix order to the standard bullet one , so needed to do some conversions.

 

On 8/1/2018 at 12:13 AM, Zakwayda said:

Maybe you already have, but make sure you've tried both orders (rotate->translate, and translate->rotate). If it still doesn't work, post the code where you build the full transform (that is, show both calls, the one to rotate() and the one to translate()).

I have tried both orders but still it doesn't work. Here is the code for the transform: 


Matrix4f transformationMatrix = new Matrix4f();
javax.vecmath.Vector3f ballPosition = body.getWorldTransform(new Transform()).origin;
Quat4f rotation = body.getWorldTransform(new Transform()).getRotation(new Quat4f());
			
transformationMatrix.rotate(rotation.x, new Vector3f(1, 0, 0));
transformationMatrix.rotate(rotation.y, new Vector3f(0, 1, 0));
transformationMatrix.rotate(rotation.z, new Vector3f(0, 0, 1));
	
transformationMatrix.translate(new Vector3f(ballPosition.x, ballPosition.y, ballPosition.z));
			
shader.enable();
shader.loadMatrix(transformationMatrix, "transformationMatrix");
shader.disable();

 

22 hours ago, Zakwayda said:

Someone might be able to recommend a tutorial, but I wouldn't give up on solving the issue in the meantime. I suspect that whatever the issue is, it may be fairly localized and easy to fix.

Here's another diagnostic you could try. Ignore the Bullet rotations for now. Instead, in your own code, apply some sort of constant rotation to your objects, like maybe constant rotation about a world axis. If that works, it will suggest that the problem is in how you're interpreting or applying Bullet's transform data. If it doesn't work, it will suggest it's strictly a problem of how you're dealing with transforms in OpenGL/LWJGL.

It can be what your saying. And I have tried all those solutions but it wouldn't budge. What I am surprised about is that there are very less resources on Bullet Physics with OpenGL >3.0. There has to be somewhere :/

Quote

What I am surprised about is that there are very less resources on Bullet Physics with OpenGL >3.0. There has to be somewhere

I think the reason you're not finding much is that OpenGL and Bullet (and physics in general) are largely orthogonal concerns. Any connection between them is more or less incidental, so resources specifically about connecting the two may be hard to find.

Anyway, as I suspected might be the case, it looks like there's a simple error.

I can't guarantee this is the problem (or the only problem), but it's the first thing I noticed. You're treating the elements of a quaternion as Euler angles, which won't work, generally speaking. You need to match the rotation representation between Bullet and the math library you're using. It doesn't matter what you use - axis-angle, quaternion, Euler angles, matrix - it just needs to be the same representation (or you need to perform an appropriate conversion). If you end up using angles in any way, make sure you perform any degree<->radian conversions as needed.

There could be other problems, so that might not necessarily fix it, but that's probably the first thing to address.

This is a method I wrote that worked in my game engine:


public Matrix4f getWorldMatrix() {
	Transform transform = body.getWorldTransform(new Transform());

	float[] fMat = new float[16];
	transform.getOpenGLMatrix(fMat);
	org.joml.Matrix4f worldMatrix = new org.joml.Matrix4f();
	worldMatrix.set(fMat);

	return worldMatrix;
}

 

20 hours ago, orange451 said:

This is a method I wrote that worked in my game engine:



public Matrix4f getWorldMatrix() {
	Transform transform = body.getWorldTransform(new Transform());

	float[] fMat = new float[16];
	transform.getOpenGLMatrix(fMat);
	org.joml.Matrix4f worldMatrix = new org.joml.Matrix4f();
	worldMatrix.set(fMat);

	return worldMatrix;
}

 

I think your not using LWJGL. Are you using JOGL? 

 

This topic is closed to new replies.

Advertisement