• 13
• 27
• 9
• 9
• 20

# Mixing Physics + Scenegraph

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

## Recommended Posts

Hey, I've seen this asked before via my many Google's, and even indeed on this site - but have yet to come across an answer that is suitable for my particular scenario :) It's the fairly standard question of "How do I integrate my really simple physics engine with my not so simple scene graph". I've spent a lot of time on my rendering engine and I'm having a little difficulty seeing the forest for trees :/ The way I see it: Each scene graph node's full transform is the result of concatenating the matrices from all parents. That's great and works just fine. I don't place any limitations on where renderable items can be placed (leafs, root, internal, it's all the same) and I don't place any constaints on the local transforms of each node. I was considering going down the route of having this scene graph as part of the overall engine design and being able to 'attach' a physics controller to a node which would then be capable of communicating both ways. ( Setting the node transform manually would override the current physics controller transform, and after a frame update, if necessary, the physics controller transform would modify the node's transform ). Ah, and there's the problem! The physics system has the full transform for the object - how else would it function? The physics system works on the overall world comparing each object's collision region transformed by the concatenated transform. This works fine in theory because the physics system does not care about the scene graph, it doesn't know about local and world transforms, it only has a global perspective. When the engine asks the physics engine to update and then attempts to synchroninse the scene graph transforms with the physics transforms I run into a problem. I can't set the world transform of a scene node? How could I? I can only set the local transform? And yet, the physics system only has the world transform! Am I completely missing something here? Have I gone about this the wrong way? (If so, I don't mind changing things that are 'wrong'). How should I update the transforms on my renderables to match those of the physics objects? Thanks. EDIT: After further thinking etc, why the hell am I trying to set the transform directly? :) :) The PhysicsObject should have properties on it for say "Velocity", and when Physics System has been updated, the PhysicsObject should be able to say "Yeah, I need to move in this direction or rotate this much or etc etc" in order to be up to date. The engine should then be able to update the scene graph from the physics system quite happily. I think anyway, feel free to respond otherwise if I'm completely off base here! [Edited by - Derobrash on April 21, 2007 4:05:54 AM]

##### Share on other sites
I've never had to implement a full fledged scene graph but I have had to implement hierarchical object entities some of which are controlled by animation, physics or game code. I've always just skipped concatenation in my code when an object has an explicit worldspace position:

I have a class contained by each node that is virtualized:

class TransformController{public:	virtual GetTransformInWorldSpace( Matrix& transform ) = 0;};

For simple objects with a local to parent transform:

class SimpleTransformController : public TransformController{public:	virtual GetTransformInWorldSpace( const Matrix& parent, Matrix& transform )	{		// concatenate local with parent		transform = Concantenate( GetTransformInLocalSpace(), parent );	}		protected:		const Matrix&	GetTransformInLocalSpace() const { return m_matrixLocalSpace; }		Matrix		m_matrixLocalSpace;};

For objects driven by physics just use the world matrix:
class PhysicsTransformController : public TransformController{public:	virtual GetTransformInWorldSpace( const Matrix& parent, Matrix& transform )	{		// ignore parent matrix 		transform = GetMatrixFromPhysicsObject( );	}		protected:		const Matrix&	GetMatrixFromPhysicsObject() const { return m_physicsObject->GetMatrixWorldSpace(); }};

Here's my render code:

DrawObjects( const Node& node, const Matrix& parentTransform ){	Matrix transform;		GetTransformInWorldSpace( parentTransform, transform );		object->Render( transform );		for ( std::size_t i = 0; i < GetNumChildren(); i++ )	{		DrawObjects( GetChild( i ), transform );	}}

Can you do something like that? I'm sure you could cache the data each frame or each time a branch changes if you need to - not too clear on the details of a scene graph here.

Just my experiences, though.

/S

##### Share on other sites
I've gone with something like what you describe in the end it seems.

I was approaching this from the wrong way, wondering how to integrate my physics with what was essentially a mechanism for efficent rendering instead of trying to write a system that utilised both as seperate entities.

I think having a couple of scene management structures - one for semi-static (IE, none physics dynamic) objects such as the traditional hierarchical scene graph, and one for dynamic objects which are going to be moving around a lot irrespective of parent physics (although still with some level of child/parent interaction for constraints) works quite well.

When I say two systems of course, they're still just the one, but there is a small level of distinction between the two despite sharing mostly the same code.

Bonus being of course I can just use the physics transforms to update the node transforms when necessary and not worry about the static scene.

I'm not sure if I explained myself properly here, but I know what I've got works, and works quite well so you're not going to seem me complaining ;)

##### Share on other sites
Would you mind posting more about your system? I am currently working on the same thing in my engine and am wondering how others are accomplishing the tasks.