TRigidBody


Croquet-Teapot

Comment:

TRigidBody is the core of the rigid body physics engine. It is itself a TFrame. At initialization, it is handed another frame, which presumably is some kind of mesh that can calculate its own inertia tensor. The TRigidBody is then installed into TPhysicsEngine, a physics engine which is responsible for updating it's state. This engine is designed to be independent of object position and orientation. It uses the current state of the frame and the current velocity and angular momentum to determine how the state will change. This does mean that if the object is currently undergoing a physical transform as it is being moved, it will continue to do so. 

Hierarchy:

ProtoObject
Object
TObject
TFrame
TRigidBody

Summary:

instance variables:

angularMomentum centerMass centerMassWorld dampingAngular dampingLinear energy force frame inertiaTensor inverseInertiaTensor inverseWorldInertiaTensor mass momentum oneOverMass torque velocity

methods:

instance class
accessing collision compute initialize testing instance creation

Detail:

instance variables:

angularMomentum
centerMass
centerMassWorld
dampingAngular
dampingLinear
energy
force
frame
inertiaTensor
inverseInertiaTensor
inverseWorldInertiaTensor
mass
momentum
oneOverMass
torque
velocity

instance methods:

accessing
allStop


	self velocity: B3DVector3 new.
	self angularMomentum: B3DVector3 new.
angularMomentum


	^ angularMomentum.
angularMomentum: am


	angularMomentum _ am.
centerMass


	^ centerMass.
centerMassWorld


	^ centerMassWorld.
frame


	^ frame.
velocity


	^ velocity.
velocity: v


	velocity _ v.

collision
collideFloor: floor
 
	^ frame collideFloor: floor
collidePlane: normal offset: offset


	^ frame collidePlane: normal offset: offset.

compute
computeForce: f delta: deltaTime


	force _ force + (f*mass).

"	force _ force - (dampingLinear * mass * velocity).
	torque _ torque - (dampingAngular * angularMomentum)."

	
globalTransformUpdate


	" This is actually called when the globalTransform needs to be updated because the frame has changed. This means that we only recalculate centerMassWorld as necessary."
	centerMassWorld _ globalTransform localPointToGlobal: centerMass.
impulse: impulse at: r


	| rc |
	rc _ r-self centerMassWorld.

	velocity _ velocity + (impulse * oneOverMass).
	angularMomentum _ angularMomentum + (rc cross: impulse).
integrate: deltaTime


	| position orientation av angularVelocity |

"	energy = 0.0 ifTrue:[^ self]."
	
	position _ self translation.
	orientation _ self orientation.

	velocity _ force * oneOverMass * deltaTime + velocity.
	position _ velocity * deltaTime + position.

	angularMomentum _ torque * deltaTime + angularMomentum.

	inverseWorldInertiaTensor _ (orientation orthoNormInverse composedWithLocal:
		inverseInertiaTensor) composedWithLocal: orientation.

	angularVelocity _  inverseWorldInertiaTensor localPointToGlobal: angularMomentum.
	av _ deltaTime * angularVelocity.

	orientation _ orientation + (orientation composedWithLocal: (B3DMatrix4x4 skew: av)).
	orientation _ self orthoNormalize: orientation.

	self orientation: orientation.
	self translation: position.
	torque _ B3DVector3 new.
	force _ B3DVector3 new.
orthoNormalize: mat


	| rmat r1 r2 r3 |

	rmat _ B3DMatrix4x4 new.
	r1 _ mat row1.
	r2 _ mat row2.
	r3 _ mat row3.

	r1 _ r1 normalize.
	r3 _ (r1 cross: r2)normalize.
	r2 _ (r3 cross: r1)normalize.
	
	rmat a11: r1 x.
	rmat a12: r1 y.
	rmat a13: r1 z.
	rmat a21: r2 x.
	rmat a22: r2 y.
	rmat a23: r2 z.
	rmat a31: r3 x.
	rmat a32: r3 y.
	rmat a33: r3 z.
	rmat a44: 1.0.
	^ rmat.
	

initialize
initializeWithFrame: frm mass: ms

	| it |
	super initialize.
" This object can only have one frame associated with it as the rigid body object for now. #addChild: should not be used."
	self singleParent: true.
	frame _ frm.
	frm objectOwner: nil. "This forces the pointer to look at the parent for objectOwner."
	self addChild: frame.
	mass _ ms.
	it _ frame inertiaTensor.
	it ifNotNil:[
		it mass: ms.
		inertiaTensor _ it result.
" The inertiaTensor is not a homogeneous matrix (I don't think), so I can't just flip axes here. Doesn't matter anyway, as this is only done in initialize."
		inverseInertiaTensor _ inertiaTensor inverseTransformation.
		centerMass _ it centerMass.
		oneOverMass _ 1.0/mass.].
	
	velocity _ B3DVector3 new.
	momentum _ B3DVector3 new.
	force _ B3DVector3 new.
	torque _ B3DVector3 new.
	angularMomentum _ B3DVector3 new.
	dampingLinear _ 0.5.
	dampingAngular _2.0.
	energy _ 0.
	self visible: false.
	^self

testing
isRigidBody


	^ true.

class methods:

^top


- made by Dandelion -