-
1 Attachment(s)
rotation, should i use matrices??
Hi. I'm currently working on a program to solve the Rubik's cube. I have drawn some slight semblance of the cube in D3D. My question is in the rotation of it. I know that I have two options: rotate the camera, or rotat the world matrices. I have no clue how to rotate the world matrices, as I've tried before and because the origin lies on a corner of the cube and not in the center of it, it doesnt rotate the cube correctly. I have attached my program, and the camera rotation subroutine is in the DX module in the RenderObjects procedure. I'm wondering if I should use matrices for the roatation, and if so how would i position the pivot point to (8,8,8)? I would have to rotat the matrices in Picture1_Mousemove, but thats not a problem. ANY help would be great. Thanx
-
Re: rotation, should i use matrices??
To rotate something about a point that isn;t the origin translate (move) the object first, in the opposite direction.
E.g. move it by -8,-8,-8, then rotate.
-
Re: rotation, should i use matrices??
how do i move the object - its made up of 216 points. I really dont want to have to move them all. So is there a matrix I can move or something? I also have the points being dynamically created and that subroutine is a gigantic mess if i try to mess with it.
-
Re: rotation, should i use matrices??
Yes you have to use a matrix to rotate vertices in DirectX. To rotate an object by its world coordinates, you do this:
VB Code:
Public Sub Rotate_Local_Coordinates(ByVal Angle_X As Single, ByVal Angle_Y As Single, ByVal Angle_Z As Single)
D3DXMatrixRotationX Rotate_X_Matrix, (PI * Angle_X) / 180
D3DXMatrixMultiply World_Transformation_Matrix, World_Transformation_Matrix, Rotate_X_Matrix
D3DXMatrixRotationY Rotate_Y_Matrix, (PI * Angle_Y) / 180
D3DXMatrixMultiply World_Transformation_Matrix, World_Transformation_Matrix, Rotate_Y_Matrix
D3DXMatrixRotationZ Rotate_Z_Matrix, (PI * Angle_Z) / 180
D3DXMatrixMultiply World_Transformation_Matrix, World_Transformation_Matrix, Rotate_Z_Matrix
Direct3D_Device.SetTransform D3DTS_WORLD, World_Transformation_Matrix
End Sub
Public Sub Translate_Local_Coordinates(ByVal Translate_X As Single, ByVal Translate_Y As Single, ByVal Translate_Z As Single)
D3DXMatrixTranslation Translation_Matrix, Translate_X, Translate_Y, Translate_Z
D3DXMatrixMultiply World_Transformation_Matrix, World_Transformation_Matrix, Translation_Matrix
Direct3D_Device.SetTransform D3DTS_WORLD, World_Transformation_Matrix
End Sub
Then in your game loop you rotate first, then translate for every object. Use these two again on the next object, otherwise it'll rotate every other object drawn, rather than rotate independently:
VB Code:
D3DXMatrixIdentity World_Transformation_Matrix
Rotate_Local_Coordinates Object.Angle_X, Object.Angle_Y, Object.Angle_Z
Translate_Local_Coordinates Object.X, Object.Y, Object.Z
Now if you want an FPS style camera, you would do this:
VB Code:
'Camera
If DirectInput_Key_State(DIK_NUMPAD8) Then 'Camera Forward
Camera_Position_X = Camera_Position_X + Fast_Sin(Camera_Angle_Y) * Camera_Speed
Camera_Position_Z = Camera_Position_Z - Fast_Cos(Camera_Angle_Y) * Camera_Speed
End If
If DirectInput_Key_State(DIK_NUMPAD5) Then 'Camera Backward
Camera_Position_X = Camera_Position_X - Fast_Sin(Camera_Angle_Y) * Camera_Speed
Camera_Position_Z = Camera_Position_Z + Fast_Cos(Camera_Angle_Y) * Camera_Speed
End If
If DirectInput_Key_State(DIK_NUMPAD4) Then 'Camera Side Step Left
Camera_Position_X = Camera_Position_X - Fast_Cos(Camera_Angle_Y) * Camera_Speed
Camera_Position_Z = Camera_Position_Z - Fast_Sin(Camera_Angle_Y) * Camera_Speed
End If
If DirectInput_Key_State(DIK_NUMPAD6) Then 'Camera Side Step Right
Camera_Position_X = Camera_Position_X + Fast_Cos(Camera_Angle_Y) * Camera_Speed
Camera_Position_Z = Camera_Position_Z + Fast_Sin(Camera_Angle_Y) * Camera_Speed
End If
If DirectInput_Key_State(DIK_NUMPAD7) Then 'Look Left
Camera_Angle_Y = Camera_Angle_Y - 1
End If
If DirectInput_Key_State(DIK_NUMPAD9) Then 'Look Right
Camera_Angle_Y = Camera_Angle_Y + 1
End If
If DirectInput_Key_State(DIK_PRIOR) Then 'Look Up
Camera_Angle_X = Camera_Angle_X - 1
End If
If DirectInput_Key_State(DIK_NEXT) Then 'Look Down
Camera_Angle_X = Camera_Angle_X + 1
End If
If DirectInput_Key_State(DIK_NUMPAD3) Then 'Go Up
Camera_Position_Y = Camera_Position_Y + Camera_Speed
End If
If DirectInput_Key_State(DIK_NUMPAD1) Then 'Come Down
Camera_Position_Y = Camera_Position_Y - Camera_Speed
End If
-
Re: rotation, should i use matrices??
Here is a good place to look.
I have not looked at your code, but I think you would need them in a matrix to be able to transform and rotate them.
http://forums.microsoft.com/MSDN/Sho...27671&SiteID=1
-
Re: rotation, should i use matrices??
My way was better ;) :thumb:
-
Re: rotation, should i use matrices??
wow Jacob.....im a newb.. i have no idea what most of that code does..and i really like to understand what im doing with d3d as i want to make more programs in the future.....i dont know what an fps style camera is either...maybe if you took a look at my code?... its in vb6.. i would have a clue as to what your talking about..sorry to be a pain.. i hate being a newb at things..:-P
-
Re: rotation, should i use matrices??
actually looking mroe at your code it seems u actually have an object.. to create my object i use the following
m_D3DDevice.DrawPrimitive D3DPT_TRIANGLELIST, D3DFVF_VERTEX, _
m_Vertex((i - 1) * 4 + 1), 3, D3DDP_DEFAULT
becaues i have an array with all ov my vertices in it, therefore.. i know know how to name the object i want to move/rotate....
-
Re: rotation, should i use matrices??
I have a couple sample progs in my site that I've made that should help you out ;)
www.angelfire.com/fl5/memorydll/index.html
And an FPS (First Person Shooter) style camera allows you to move forward/backward, side step, and look up/down at any angle you are facing on the Y axis. Makes it move like you are in the real world.
-
Re: rotation, should i use matrices??
jacob
i hate to be a pain, but im on dial up and that code will take forever to dl.. i just spent some time studying the code that u posted however...and perhaps if i provide a bit more detail as to how my program works you could explain to me exactly where to place that code
a rubiks cube consists of 6 faces (as any cube would)...these faces have what i like to call 9 facelets (the small squares that make up a rubik's cube face).... to map out to coordinates and type them in for 9 facelets x 6 faces x 4 points per facelet...
i did it on paper instead and noticed a pattern...therefore, I made a subroutine that takes only the coordinates for the first facelet, and the face that facelet is contained on, and appends to the vertex array...i need to call this sub 6 times (once for each face)
the rear lower left point of the cube is on (0,0,0) and each face is 5 units wide, separated by a .5 unit gap...making the whole cube 16 units wide, and the center at (8,8,8)...
in my render loop i clear the viewport, change the angle of the matrix, update the angle, and graph each facelet. in order to display each of the facelets properly, i need to look and the vertex array and use a loop to make the DrawPrimitive method only look at 3 vertices (a square is two triangles)... then graph the other triangle for that square....this loop lasts 216 iterations. after which i draw the result..
my guess is that when i am updating my matrix, should i use your code to rotate the matrix, the move it by (-8,-8,-8)?..... i think i can handle the trig to figure out how to rotate x and z dependsing on the angle of rotation around y..im just curious if thats what you mean with the code you wrote above
-
Re: rotation, should i use matrices??
Well since the rubix cube consists of 9 cubes, you should treat each of them as objects, which means you are gonna need 9 of these in your game loop, each pasted before every cube drawn, only this time, translate before rotating to allow the outer cubes to rotate far from the origin:
VB Code:
D3DXMatrixIdentity World_Transformation_Matrix
Translate_Local_Coordinates Object.X, Object.Y, Object.Z
Rotate_Local_Coordinates Object.Angle_X, Object.Angle_Y, Object.Angle_Z
And what I've done for the games was make a sub called Control with the camera stuff in there, and had that FPS camera code located above within it, then in the game loop, you simply call the sub Control before anythings drawn. I also forgot to copy/paste one more function you are gonna need:
VB Code:
Public Function DirectInput_Key_State(Key_Code As Long) As Long
Keyboard_Device.GetDeviceStateKeyboard Keyboard_State
DirectInput_Key_State = Keyboard_State.Key(Key_Code)
End Function
-
Re: rotation, should i use matrices??
27 cubes I think you'll find, unless its a rubik hypercube and nobody's been able to manufacturer one of those in this universe. :D :lol:
-
Re: rotation, should i use matrices??
No wossy, it's 9 cubes. Have you ever played it before?
-
Re: rotation, should i use matrices??
http://www.rubiks.com/
Sure does look like 3 * 3 * 3 (27) cubes to me!!
-
Re: rotation, should i use matrices??
Whoops. My bad. It was early in the morning. My brain must have been off in the wrong direction. :p
-
Re: rotation, should i use matrices??
i just drew the faces that are visible.. so all that wouled be drawn would be the colored faces..and I attempted to draw the faces behind it that would make up the "cubes"... but i couldnt get those faces to appear behind the others...so i just left it as a shell. i dont know how to create objects... i just use the drawprimitive command to graph the points of that shell and leave it at that....im not sure if there is something im missing to create objects - this is actually my first d3d attempt.
GAH ! CONFUSION
-
Re: rotation, should i use matrices??
Upload the project you have and i'll fix it for ya. ;)
-
Re: rotation, should i use matrices??
look up^^theres a remark in the game loop... it refers to a textbox.. i used to that troubleshoot my vertex creating sub... just ignore ... any changes you make could you remark out what i have so that i can see what your doing?...and if its not too much trouble.. give some kind of explaination..thanx :-D:-D:-D
-
Re: rotation, should i use matrices??
I see that you are using DirectX7 to do Direct3D. You should have been using DirectX8 cause they made it way easier, so I'm gonna tweak your code a bit to get it going on that.
-
1 Attachment(s)
Re: rotation, should i use matrices??
I'm not through with your project just yet, but your basic game engine setup should be like this:
-
Re: rotation, should i use matrices??
Quote:
You should have been using DirectX8 cause they made it way easier
that was....easier.. i looked at the engine u set up and im extremely confused..lol... it took me 3 different tutorials where all of the sites used d3d7. i have no idea about d3d8..but it looks like you have about 7 modules...and.. i have...3... i think thats where i get confused...perhaps also because i was using the extreme barebones of d3d to get the result, and not accounting for anything else. I see that you are setting every parameter, and accounting for everything in d3d8. Perhaps you could refer me to a d3d8 for dummies website?.. :sick: :D
-
1 Attachment(s)
Re: rotation, should i use matrices??
I made some progress so far but still have a while to go. I broken it down for ya by separating the DX stuff from Direct3D, DirectInput, the Game Engine, etc, in separate modules, rather than cramming most of it all in one module (which you did :rolleyes: ). MS made D3D easier to work with in DirectX 8, and even easier in 9 with more features, only it's managed. Here's a nice tutorial on DirectX8
http://directx4vb.vbgamer.com/Direct...T_DX8Start.asp
No you do not need all that code to initialize it, but what I did was called enumeration. It obtained all the resolutions from your video card, and if the resolution you selected matched one of them, it switched to it, otherwise it uses the same resolution you have. And I also have a hybrid initialization, to where you have a choice of having windowed or fullscreen mode. Here's an update on it so far that I uploaded. I'm gonna be in Cocoa beach this weekend so if you have any more questions, I'll be back to answer them, and even finish up seting up that rubiks cube. ;)
-
Re: rotation, should i use matrices??
ok ill study that and see if i cant come up with an understanding...also.. i dont know if you noticed how i had the reference to the colors for the cube...(a 2d array of fce, facelet).... but if possible id like you to keep it that way... because i need that array to be able to make turns and then i can develop the AI and such and instruction sets on my own.. for the solving part
-
Re: rotation, should i use matrices??
Don't worry about that. I'll show ya how the colors will be done in a couple days. I got work tommorow, and leaving for the weekend afterwards, and it's bedtime, so you are just gonna have to study the code in the meantime. I may texturemap the cubes instead to give it that realistic non primitive look. ;)
-
Re: rotation, should i use matrices??
i think you have just given me one of the biggest headaches of my lfie...remember this is the first dx project ive done...EVER...this is the first attempt at anything ive ever done...lol
-
Re: rotation, should i use matrices??
Just a note - it's really 26. There's no cube in the center - take one apart and you will see it's a spiky thing that lets the cublettes all move. Just thought I'd mention... ;)