Rotating objects independantly

Started by
9 comments, last by markookram 16 years, 2 months ago
Basically I want to rotate/move objects in the 3D world independantly from each other, but many tutorials I have read just do this... device.Transform.World = Matrix.RotationZ(angle); ...and rotate the whole world! What is the best way to divide the world into seperate objects that can be manipulated independantly? I'm guessing some sort of vertex array, but then I can't use the rotation matrixes on them?
Advertisement
it works like this. you render one object, using device.Transform.World = Matrix.RotationZ(angle);. then another, but before rendering you set another angle. or matrix or whatever.
But how do I choose which object to rotate?

Could you give me an example or a link to a good tutorial please?
This answer might be similar to my question about rending multiple instances of objects to the screen at the same time when each object has it's OWN render() function to be called. I'm having a hard time grasping how this would be done. Wouldn't this mean that each object would have it's own begin() and endscene() and would just render one object at a time?

I think my above problem is similar to yours. You somehow need to have each object rotate itself with a member function, then pass that information to the renderer. I'm a bit lost. actually lots of bits lost. (thanks i'll be here all week)
every object has a Worldmatrix.

you basically calculate it per object.
like

Object1:
Worldmatrix = Rotationmatrix * Translationmatrix

if you want to rotate that object recalculate the world matrix of that object
and then render your object based on that worldmatrix :)
every object is rendered using whatever transformations you have set. so if you had one cube mesh in memory, and you wanted to render it ten times, but in different places, you would do it like this.

set transformation matrix to position 1
render the cube
set transformation matrix to position 2
render the same cube
set transformation matrix to position 3
render still the same cube
.
.
.


clear?
Quote:Original post by induster
I'm having a hard time grasping how this would be done. Wouldn't this mean that each object would have it's own begin() and endscene() and would just render one object at a time?


Nope, just call BeginScene. Set up your view and projection matricies and then before you call Render, Draw, or whatever on your object you set up the world transform. Each object in your world should have it's own world transform based on it's position, orientation, and scale.

For example.

D3DDevice_->Clear(0,NULL,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,0),1.0f,0);D3DDevice_->BeginScene();SetupViewMatrix();for( unsigned i = 0; i < objects_.size(); ++i ){  objects_->GetWorldMatrix( &matWorld_ );  D3DDevice_->SetTransform( D3DTS_WORLD, &matWorld_ );  DrawModel( objects_->GetModel() );}D3DDevice_->EndScene();D3DDevice_->Present(NULL,NULL,NULL,NULL);


I wouldn't recommend doing it exactly as shown but it works well enough.
Ok, I think I see now. The actual begin and end scene should be located in it's own class. Like a drawscene class that takes in all objects as arguments. Now I'm seeing where it would be important to organize all objects into an array or list to make passing them easier.

thanks everyone
But how do I setup objects so each has it's own Worldmatrix?
Use a class if you mean that?

like:

class Object{   Matrix worldMatrix;   Matrix rotationMatrix;   Matrix translationMatrix;      void Rotate(Vector3 rotation;   {      // calculation here the rotation matrix;   }   void SetPosition(Vector3 rotation)   {      // calculate here the translation matrix;   }   void Render()   {      worldMatrix = rotationMatrix * translationMatrix;      // render calls    }}


a bit like that ;)

This topic is closed to new replies.

Advertisement