# Quaternions needed for a ode physics jme demo

Big message, try to bear with me…

I have done a physics demo using Open Dynamics Engine and Xith3d. Now I want to do it also with JME, when I get it to work with JME also, I will write a download link for these demos.

My problem: How to set rotation into spatial objects using quaternions (or matrix3f).

Let’s say I have a quaternion (javax.vecmath.Quat4f) and I want to use this for updating jme’s Spatial object’s orientation. How to do this correctly?

I am using a code like:

– CODE STARTS

// Create jme compatible rotation matrix4f

com.jme.math.Matrix3f bodyRot = new com.jme.math.Matrix3f.Matrix3f();

// Create helper matrix3f that accepts quaternions

javax.vecmath.Matrix3f m = new javax.vecmath.Matrix3f();

// Get quaternion from ODE (w,x,y,z)

javax.vecmath.Quat4f q = new javax.vecmath.Quat4f.Quat4f(

ode.floatArray_getitem(f, 1),

ode.floatArray_getitem(f, 2),

ode.floatArray_getitem(f, 3),

ode.floatArray_getitem(f, 0));

// Set quaternion into helper matrix3f

m.set(q);

// Copy helper matrix3f into jme compatible matrix3f

for (int i = 0; i < 3; i++)

for (int j = 0; j < 3; j++)

bodyRot.set(i, j, m.getElement(i, j));

// Rotate jme object

spatial.setLocalRotation(bodyRot);

— CODE ENDS

When the world begins to evolve (simulation steps go forward), initially jme objects rotate as they should, but orientation starts misbehaving after big enough of rotation. At least it seems this way. Finally objects tend to go below floor after a fall, or they rotate quite fast >360 degrees on certain axis etc…, Perhaps this is a problem with jme’s internal representation (eulers) or have I misunderstood some basic 3d logic?

The thing is that code above works with Xith3d, I wanted to be sure so I tested code above with Xith3d successfully. Normally I’m using simpler code (quaternions directly) in Xith3d.

Be sure to check out http://opende.sf.net . This engine is fantastic! Lot’s of features and it’s fast. The results that some people have done with it are truly amazing. Suites fine for making a game physics like Max Payne. I got working Java bindings for this engine. If I get jme’s rotation to work, I’ll release couple ode demos using jme and try to convince you guys to plugin ODE into jme’s entity classes :). For the quick and impatient, check out following windows demos (they use ODE and contain simple exe file):

http://jet.ro/dismount/

BTW Second question (minor), is there a way to create cylinder in jme programmatically? I noticed older jme’s jme.geometry.primitive.Cylinder is not active (glu commands commented out). I need an cylinder to create a car tire, perhaps I need to import it but it seems a bit overkill for a simple object.

Keep up the good work monkey man

First thing, I will be adding a Quaternion class soon so you shouldn’t have to use the vecmath helpers. I was holding off until skeletal animation, but it may be the time to do it now. This will at least keep you from having to do matrix copies from one type to another.

Now, for the error in rotation, is the place where the error occurs consistant? If so, can you track down the values of the rotation matrix? If I can get those values I may be able to determine what the problem is. Right now, I really don’t know what the problem is, as you are the first to really start spinning things. So, I’m excited to see this happening.

All rotation is purely matrix rotation. Which means it shouldn’t be an error due to gimbal lock (Euler) etc. For example, the matrix can be made from an axis and an angle, or Quaternion.

As far as adding ODE for the physics. I am not opposed to that at all. If you are able to make some cool demos (once we handle this bug) showing what can be done, we should be able to add jME wrappers to the handle ODE physics.

Lastly, I should probably add Cylinder, sphere, disk etc. I was hoping LWJGL would add them but I’m sick of waiting for it. Keep bugging me, and I’ll go ahead and get them in.

Thanks for working with jME. I hope it is turning into something usable.

Mark

Above message was written by user janilaakso, for some reason I didn’t get logged in earlier.

Ok, I’ll take a look. Today I am going to add Quaternion to the base line. I’ll let you know when that is finished. At least that will allow you to not have to worry about using two different matrix types and everything. So that will take care of problem one.

I’ll try to figure out the source of the second problem (bad rotations at high angles) after playing with your code.

Quaternions are now in CVS com.math.Quaternion. Matrix3f also now has support for creation from a quaternion.

```Matrix3f matrix = new Matrix3f(); matrix.set(quat);```

That should improve the ease for you some.

Ok, I’ve added lots of convienence methods, you can now rotate Spatials directly from quaternion now. However, I am definately seeing some… oddities… as I rotate a cube. It’s skewing the size of the box, as if the scales are changing during one part of the rotation. This only occurs during a rotation that is NOT about a unit axis. That is, (1,0,0), (0,1,0), and (0,0,1) rotate fine.

I have a math error somewhere and I can’t seem to find it.

I released the package today at a temporary www-site. odajava.dev.java.net seems to take some time to get it up.

Anyway, it is very easy to plug these demos into jme. I hope you get the rotation fixed, I am sorry but I believe that I do not have the knowledge and enough time to help you out with this particular jme problem.

If you wish you can check out the demos (see below). I have the boxdemo using jme also, but didnt add it to the package yet as the rotation is currently somewhat akward.

I made couple physics demos using Xith3d and odejava.

You can check try them out at:

http://cray.tuug.org/~jani/odejava/

Download the odejava-0.1-full.zip if you want to get the easiest way as it contains jogl, xith3d and other 3rd party libraries and jars that are required for executing the demos. Main directory has three bat files, so click those to execute the demos. Sorry, but odejava currently has only windows binaries compiled, others come later I hope.

On the BoxApp you can change simulations by pressing 1,2 or 3. Keys q,w,e,a,s,d change box sizes (select new simulation after this). Mouse drag with left mouse button adds some spin to boxes and plain left click to bottom or top of the screen gives big force to last object (try it with tower simulation as all boxes are connected to each other, snaky).

On the CarApp you can try to drive around with a,d,w,s keys. b is handbrake (on/off) and keys 1,2,3 change the camera position. Space resets the simulation. Ode’s parameters are bit hairy, fix if you want

runOdeHelloWorld.bat runs console based test with couple objects falling to a ground, with no rendered attached to it, so this is a bit dull demo.

Hope people get a spark for developing odejava after seeing these hilarious demos

Cheers, Jani

Ok, I think I may have the issues figured out, but not yet resolved.

1. Skewing when rotating, caused my either my Quaternion fromAxisAngle method or toRotationMatrix method. Will fix the broken one. You didn’t see this because you are using vecmath quats and matrices.

2. It’s rotating about the minimal point rather than the center of the box. Meaning I set the center incorrectly. This could be causing your boxes to not rotate how you’d expect them to.

Now they are discovered, I’ll be fixing them today.

By the way, the demos are a blast! Hopefully, before too long we’ll get working jME versions.

"mojomonk" wrote:
Now they are discovered, I'll be fixing them today.

Yes, this makes sense, it seems that the center point was misaligned.

Tell me when this is fixed and I'll add an odejava demo using jme.

Ok, first… rotation center is not an internal jme issue. It sets the center to the min point and max point’s center. So, during creation of the geometry the user defines the center. So, my examples just happened to set the center to (5,5,5) instead of (0,0,0).

Therefore, to fix the rotation, create a box with a min of something like (-5,-5,-5) and a max of (5,5,5). This will set the center of the geometry data to the center of the node.

I will be sure to discuss this in detail in the Users Guide I’m slaving away at currently.

As far as the problem with the quaternion, it is definiately in the fromAxisAngle method. I’ve requested help from the javagaming.org folks.

Ok, I figured out the issue with my quaternion. I forgot to normalize the axis before using it. So, now everything should be fixed.

EDIT: Do you use the CVS code and build your own jars, or will you need me to make a jar for you?

"mojomonk" wrote:
EDIT: Do you use the CVS code and build your own jars, or will you need me to make a jar for you?

Great that rotational problems are fixed!

I use CVS and I'll build an snapshot jme.jar for odejava full release, after all the demos are not too critical. I will try to catch up some jme demos using odejava today, odejava's API changed a bit in the last few days before I decided to make an prerelease out of it.

When jme demo(es) are included in the package, I'll post a message into new thread (Download jme physics demo).
"mojomonk" wrote:
Ok, first... rotation center is not an internal jme issue. It sets the center to the min point and max point's center. So, during creation of the geometry the user defines the center. So, my examples just happened to set the center to (5,5,5) instead of (0,0,0).

Yes, this solved all rotation issues. Now it behaves good.
"mojomonk" wrote:
As far as the problem with the quaternion, it is definiately in the fromAxisAngle method. I've requested help from the javagaming.org folks.

I am now using Quaternions, they work without a flaw.

BTW I am using com.jme.*, if it's simple matter, can you post me a code on how to add an nice looking plane with textures (ground would be nice on this demo).

I’m afraid that initially, it’s going to have to look a bit plain. com.jme.* does not have many visually exciting things yet. I’ve been focusing on getting a solid foundation down. However, one possiblity is you can create a very large box to act as the “ground”.

More interesting environments are coming (model loaders, terrain, etc) just need to get all the behind the scenes stuff done first.

Can’t wait to see the results. First that someone is doing something real with the new core.

Well now it works, actually I’d had it working earlier if I’d not misunderstood the centerpoint issue on boxes.

I try to snatch an hour from my weekend and I’ll add jme demos to the package along with jme.jar (cvs snapshot) and other 3rd party libraries.

I can leave current (newer com.jme) demo in any case to the package, although it is very simple. Perhaps I’ll abuse older jme’s test.general.TestMain class in order to get nicer demo

Great, glad to hear it works. Anyway, can I get ahold of a copy? Or do you need to package it? I have the current zip from your website.

"mojomonk" wrote:
Great, glad to hear it works. Anyway, can I get ahold of a copy? Or do you need to package it? I have the current zip from your website.

Check the website, it has now jme demo also. Please add more or boost up the graphics by plugging sample OdeCar class to older jme :) I didn't bother to find out how to apply two different textures, I am sure it is simple matter, if you can fix it send me the class file.

PS. This one is using the newer jme.

The change will be made to Matrix3f and placed into CVS. Thanks for catching that.

Yeah, there’s not a lot of visual power right now in jME, but that’s because I’m focusing more on it’s guts. As I add new visual features, I’ll let you know and maybe you can update the demo to make it look a bit better.

I haven’t had a chance to download the demo yet and check it out, but I’m pretty excited to see it. My main concern is that it looks accurate and runs fast. If not, I need to track down the issue and take care of it.

assume it was a thing waiting to happen, perhaps I was the only one experimenting fast rotations with new Jme

Yep, you sure were. :)

BTW I really like your API, I'd only hope more developers for jme, but the again, who wouldn't

Thank you. I'm really impressed by your use of it. I'd love to somehow tie the two (your javaode and jme) closer together in the future.