#### Archived

This topic is now archived and is closed to further replies.

# Eberly physics book - Rigid Body implementation query

This topic is 4987 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

In his practical Rigid Body implemention, he has a force/torque defined something like...
typedef Vector3 (*Function)
(
Real,                    // time of application
Real,                    // mass
const Vector3&,    // position
const Quaternion&, // orientation
const Vector3&,    // linear momentum
const Vector3&,    // angular momentum
const Matrix3&,    // orientation
const Vector3&,    // linear velocity
const Vector3&     // angular velocity
);  
and then two functions inside the public declaration of the rigid body...
Function Force;
Function Torque;  
Thing is, I don't know what these two functions do? I can't find what they actually do. They take in all that data and return a vector don't they? But how? [edited by - Hybrid on April 22, 2004 7:42:31 PM]

##### Share on other sites
looks like a custom function pointer, that lets things outside the physics system to calculate external forces and torques applied on the object, where you pass stuff internal to the object to the function. For things like force fields, special Collision response, Gravity, buoyancy, ect... Some kind of callback mechanism, like you would have when a collision, or near-collision is registered.

That's how It looks like from here, but I'll have a look in the book tomorrow.

so, you have to provide a function that takes all these parameters to calculate external forces, if you want external forces acting on the object. like gravity for example.

[edited by - oliii on April 22, 2004 9:02:20 PM]

##### Share on other sites
I agree with oliii's analysis, but also can't check the book immediately because it is at home while I am not.

Since you don't seem to be familiar with function pointers, at bit of a lesson..

To expand on what oliii said, those class members, Force and Torque, are function pointers that would have to be populated to be called. First, you'd either have to implement a real function with the same prototype, or use one that Eberly provides. Lets say you right a function to generate a viscous damping force. It'd look like this. Note that the prototype matches *Function in the typedef:

Vector3 MyCalcConstantForce(Real time, Real mass,    const Vector3& pos, const Quaterion& orient,    const Vector3& linmom, const Vector3& angmom,    const Matrix3& orientmat, const Vector3& linvel,    const Vector3& angvel){    Vector3 DampingForce;    float fDampingCoefficient = 0.1f;    DampingForce = -linvel * fDampingCoefficient;    return(DampingForce);}

Then to use it, lets say you have a rigid body "A":

RigidBody A;
A.Force = MyCalcConstantForce;

I'm assuming here that the Force member is public. But there must either be some way to set it, or it should be initialized by the physics system either in the constructor or at the top of the file if its a static member.

Once A.Force is set, other methods of A can call the function like so:

RigidBody::SomeFunction(){  if (0 != Force)  {    TotalForce += Force(time, mass, pos, orient, linmom,                            angmom, orientmat, linvel, angvel);  }}

So, you can see that the function pointer data members in the RigidBody class allow you to substitute any number of different force/torque calculation functions. They get mapped to the data member name within the RigidBody class and can be called with the mapped data member name. Kind of cool and useful!

Graham Rhodes
Principal Scientist
Applied Research Associates, Inc.

[edited by - grhodes_at_work on April 23, 2004 9:48:51 AM]

##### Share on other sites
Thanks for the replies. I think I must have worded my question slightly wrong. I was aware that they were function pointers, however, I was after what these functions do specifically in the case of rigid bodies.

Would the function change a lot during run-time, e.g. sometimes it will be set to the damping function for example, and sometimes it will be set to another physics based calculation?

My initial confusion was caused, because it takes in all those parameters and returns a single vector, and I couldn''t work out what function would use all that data and return a single value, considering both linear and angular properties were passed in. I thought there might have been a specific calculation based on all those parameters, and that I didnt know what it was. But I realise now that all those parameters are there to cover all the possible functions that could be related to force and torque.

Thanks again.

##### Share on other sites
quote:
Original post by Hybrid
Would the function change a lot during run-time, e.g. sometimes it will be set to the damping function for example, and sometimes it will be set to another physics based calculation?

It might change. It could change. Hopefully Eberly at least makes some comments about his intent in the book. Perhaps he has different functions that know how to sum up the force/moments for different types of rigid bodies, e.g., does he have subclasses for bodies that are affected by gravity, that have attached springs, that are affected by wind resistence, etc.? This is an implementation detail in Eberly''s engine design and I haven''t dug into his book enough to know what his intent was.

quote:
Original post by Hybrid
But I realise now that all those parameters are there to cover all the possible functions that could be related to force and torque.

Yes, indeed!

Graham Rhodes
Principal Scientist
Applied Research Associates, Inc.