Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Animation Bug in DBC 1.20 Now Resolved

Author
Message
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 4th Sep 2008 13:41 Edited at: 8th Sep 2008 12:27
First, I must give a special thanks to Latch for all his examples, explanations, and help with the quaternion mathematics. None of this would have been possible without him.

The Problem
As Latch and I discovered when working on our respective animator programs, DB 1.20 is incapable of rotating any limb in the region 90..180 degrees. If the limb is supposed to move from 90 to 180, it will do so by moving the long way round and rotating from 90..0..270..180.

It is possible to force the limb into this 'forbidden region' by setting keyframes with angles up to 119 degrees. However, as soon as the limb strikes this value it will reverse direction and move the long way round.


The Simple Solution
DBC 1.20 is incorrectly interpreting the limb rotations which make up the animation. Recalculating these rotations will make the animation play correctly, and the "Animation Converter.dba" code snippet in the attached Zip file will do just that.

The code makes use of my new QuatMath dll (included in the Zip). The full Delphi source code for the dll is also included (it's the .dpr file, for those of you who don't know Delphi)


The Complex Solution
Limb rotations are stored in .x files as quaternion keyframes (a quaternion is a four float number - one float (w) defines how far to move around an axis defined by the next three floats (x,y,z)). These rotations were calculated from ordinary 0..360 degree angles (Euler angles) but DirectX assumes them to have been wrapped around into the range 180..-180 (so 270 is defined as -90).

This wrapping is important, because different quaternions are generated in each case, and this controls which direction the limb moves in. For example, a quaternion for the rotation 0,0,270 would move the limb +270, but a quaternion for the rotation 0,0,-90 would move the limb -90.

In most (and I think probably all) other programs, there's a built in failsafe used when animating to ensure that the limbs always move the shortest distance - it's called quaternion slerping. This mathematical algorithm starts by comparing two quaternion keyframes and finding Cos(Theta), where theta is the angle bewteen the two. It Cos(Theta)<0, then Theta is greater than 180 degrees and so the limb will be moving the long way round. This gets corrected by inverting the second quaternion (same destination, but reached by moving in the other direction) and using that in the calculation.

DBC 1.20 is unusual in that it does not perform this check for Cos(Theta) being negative. As a result, limbs can be made to move in any direction by simply placing the angles in the range -360..360 instead of -180..180.

To get existing animation to run correctly, I've written a .dba animation converter, along with a QuatMath dll (all enclosed in the attached Zip). The converter takes any ASCII .x file and passes each quaternion keyframe to the dll, where it gets converted to Euler and back to quaternion. The dll also looks at the previous keyframe and checks to ensure that Cos(Theta) between these two keyframes is positive so that the limb will always move the short route. The corrected quaternion keys get written to a copy of the input .x file.

As far as I can tell, the output .x file plays exactly the same in DBC 1.20 as it did in DBC 1.13. Unfortunately, 3ds files can only be corrected if they are first converted to .x files using the 3ds2x command, and .x Binary files can't be corrected at all.

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."

Attachments

Login to view attachments
Robert The Robot
17
Years of Service
User Offline
Joined: 8th Jan 2007
Location: Fireball XL5
Posted: 8th Sep 2008 12:30
Latch spotted and corrected a couple of flaws in my converter - it didn't recognise commented lines or templates in a .x file (either of which would have started the program trying to correct animation data, when there was still information on the 3d object to follow.)

I've also corrected the converter so that it can handle comments within the animation data and also blank lines (before, the program would have locked). The program won't handle animation templates within animation data, but this is unlikely to be an issue.

I've updated the Zip download on my first post to include the revised animation converter.

"I wish I was a spaceman, the fastest guy alive. I'd fly you round the universe, in Fireball XL5..."

Login to post a reply

Server time is: 2024-04-19 06:16:41
Your offset time is: 2024-04-19 06:16:41