# Transforms/Entity System?

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

## Recommended Posts

I'm about to code the base entity transform system for my engine, but I'm wondering... Does Managed DX provide enough maths functionality to encapsulate this sort of set up, or will I have to suppliment it with my own Matrix class to provide higher level functionality? Do I need a seperate matrix set up for the camera entity? Basically, I want to be able to do the following (Psudeo code)

Camera Cam = new Camera()

//Both classes inherit the Pivot Class.

Ship.Position( 20,20,20 )

Camera.Position( 20,50,40 );
Camera.LookAt( Ship );

Ship.LookAt( Camera );
Ship.Turn(0,45,0);//turn based on current orientation, not absolute.
Ship.Turn(45,0,0);
Ship.Move( 0,0,10 ); //Move along current heading 10 units. Relative to current oreintation.

So any advice on how to implement the above methods using dx matrixes? Any links/tutorials? I'd really like to avoid having a specialised camera as I need the look at, relative move functions for entities too. Also, does DX matrixes have the ability to transform a vector3 class by the current matrix? This would solve the move problem. And I also need it for sub entities(I.e imagine a swoard attached to the player's hand. When the player rotates. The swoard position needs to be transformed by the player's new rotation matrix to correlate)

##### Share on other sites
Entities? Pivots? Sounds like someone's coming from Blitz3D!

The built-in matrix functions in D3DX are more than capable of anything you want to do. I have a Blitz3D-esque scene graph in my own engine, and it's very easy to handle all the matrices. Each Node/Entity/Pivot/Whatever has, mainly, a local matrix and a global matrix. The local matrix holds the transform data from the position/rotation/scaling; the global matrix is the local matrix concatenated with the parent's matrix (if any). If you'd like some help with any of the math, I'll be glad to.

##### Share on other sites
Yeah, drake. I remember you from the Blitz boards.

So yeah basically something like b3d's is what I want. I don't like engines that force the user to do any math work.
I actually don't like engines that force the engine coder to do maths work either..What turned me onto dx matrixes in the first place.

So any help would be great. Thanks.

##### Share on other sites
Alright then..

Quote:
 Do I need a seperate matrix set up for the camera entity?

No, not at all. I'm assuming you have a single base class Entity from which all other entity types derive. Entity would hold function such as Position, Rotate, Scale, Move, LookAt, Tranlate, and Turn, as well as the hierarchy functions like Parent. The Entity class would also have internal functions to traverse the hierarchy of Entities and update all the matrices.

From Entity you would derive, say, an EntityMesh class and an EntityCamera class. The EntityMesh class wouldn't have to do anything special with its matrix; once its global matrix is calculated, you set that to the device's world matrix and draw it. The EntityCamera, on the other hand, only has to do one thing to its global matrix - invert it with Matrix.Invert. Then that inverted global matrix can be set to the device's view matrix. This way, you can position and rotate the camera just like any other Entity without having to worry about things like Look/Up/Right vectors or any of that crap.

Quote:
 Also, does DX matrixes have the ability to transform a vector3 class by the current matrix?

You can use the Vector3.TransformCoordinate function to do that.

Quote:
 And I also need it for sub entities(I.e imagine a swoard attached to the player's hand. When the player rotates. The swoard position needs to be transformed by the player's new rotation matrix to correlate)

This is accomplished by multiplying the Entity's local transform matrix by its parent's matrix to get the combined world matrix for the entity.

For example, let me explain how I have my hierarchy system set up. Each Entity has a dynamic array of children and a single "parent" entity pointer (which is null if the entity has no parent). The only public method of the Entity hierarchy system is Parent. There are also two protected methods, AddChild and RemoveChild. When you set an entity's parent with Parent, it will do the following:

1. If the new parent is the same as the current parent, the function just returns.
2. If the entity already has a parent, it calls RemoveChild on its parent with itself as the parameter.
3. The entity's parent pointer is set to the new pointer.
4. If there is a new parent (i.e. new parent isn't null), it calls AddChild on the parent with itself as the parameter.

AddChild just tacks on the new child to the entity's children array. RemoveChild searches for the child in the array and removes it.

Now we have a little hierarchy system. Each entity has a few matrices: matLocal, which is its local matrix built from its position, rotation and scale, and matGlobal, which will hold the parent's matrix multiplied by the local matrix. To update the matrices, I loop through all entities in existence with a static method in my Entity class, like so:

foreach(Entity e in allEntities){    if(e._parent == null)        e.UpdateMatrix();}

The allEntities array is a private static array in Entity that holds pointers to all entities in existence. This is accomplished by tacking on "this" to the array in the Entity constructor, and searching for and removing "this" in the destructor.

Notice that in this code, UpdateMatrix is only called on base-level entities; that is, entities with no parent. This is because the UpdateMatrix function also calls UpdateMatrix on all of its children.

The UpdateMatrix function looks something like:

// Create _matLocal using position, rotation, and scaleif(_parent != null){    _matGlobal = _parent._matGlobal * _matLocal;}else{    _matGlobal = _matLocal;}foreach(Entity e in _children){    e.UpdateMatrix();}

In my engine there are a few other steps (I only update matrices if they need to be updated), but this is the basic idea.

Hope this helps, and if you have any more questions, just ask!

##### Share on other sites
If you watching for any system, how to make 3rd person camera (for camera look at ship), then you can use position and rotation of the ship and with this vectors make matrix for camera.

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 11
• 15
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
634151
• Total Posts
3015826
×