Game Development Reference
In-Depth Information
I have added a matrix to the class to hold the current transform matrix for the
object. This matrix is useful for rendering the object and will be useful at various
points in the physics too, so much so that it is worth the storage space to keep a copy
with the rigid body.
It should be derived from the orientation and position of the body once per frame
to make sure it is correct. We will not update the matrix within the physics or use it in
any way where it might get out of synch with the orientation and position. We're not
trying to store the same information twice: the position and orientation are in charge;
the transform matrix member just acts as a cache to avoid repeatedly recalculating this
important quantity.
I call this “derived data,” and it is the first of a handful we'll add to the rigid body.
If you are working on a highly memory-starved machine, you may want to remove
these data: they are only copies of existing information in a more convenient form.
You can simply calculate them as they are needed. The same is true for all the derived
data I will add to the rigid body.
Let's add a function to the class to calculate the transform matrix and a function
to calculate all derived data. Initially calculateDerivedData will only calculate the
transform matrix:
Excerpt from include/cyclone/body.h
class RigidBody
{
// ... Other RigidBody code as before ...
/**
* Calculates internal data from state data. This should be called
* after the body's state is altered directly (it is called
* automatically during integration). If you change the body's
* state and then intend to integrate before querying any data
* (such as the transform matrix), then you can omit this step.
*/
void calculateDerivedData();
};
Excerpt from src/body.cpp
/**
* Inline function that creates a transform matrix from a position
* and orientation.
*/
static inline void _calculateTransformMatrix(Matrix4 &transformMatrix,
const Vector3 &position,
const Quaternion &orientation)
{
transformMatrix.data[0] = 1-2*orientation.j*orientation.j-
2*orientation.k*orientation.k;
Search Nedrilad ::




Custom Search