dil4VB
11-30-2004, 09:57 AM
I am designing a simple 3d maze like game
when i press the -> or <- arrows i want the "World" around the player to rotate around the axis passing vertically through the position of the player - as in any other 3d game that is... (At present let's skip upward and downward rotation)
how can i do it?
should i manipulate the World matrix or the Projection matrix or anything else? :confused:
'These functions will help you and use this format
'for transform every world
D3DDevice.SetTransform TransformMatrix(X, Y, Z, Ax, Ay, Az, Sx, Sy, Sz)
'for transforming one point (X,Y,Z)
TransformGeometry X, Y, Z, TransformMatrix(0, 0, 0, Ax, Ay, Az, Sx, Sy, Sz), NewX, NewY, NewZ
'Ax = X Angle
'Ay = Y Angle
'Az = Z Angle
'Sx = X Scale
'Sy = Y Scale
'Sz = Z Scale
Const Pi = 3.14159265358979 'equal to 4 * Atn(1)
Const Rad = Pi / 180
Function TransformMatrix(X As Single, Y As Single, Z As Single, Ax As Single, Ay As Single, Az As Single, Sx As Single, Sy As Single, Sz As Single) As D3DMATRIX
Dim TempMatrix As D3DMATRIX
D3DXMatrixIdentity TransformMatrix
TransformMatrix.m11 = Sx: TransformMatrix.m22 = Sy: TransformMatrix.m33 = Sz
D3DXMatrixRotationX TempMatrix, Ax * Rad: D3DXMatrixMultiply TransformMatrix, TransformMatrix, TempMatrix
D3DXMatrixRotationY TempMatrix, Ay * Rad: D3DXMatrixMultiply TransformMatrix, TransformMatrix, TempMatrix
D3DXMatrixRotationZ TempMatrix, Az * Rad: D3DXMatrixMultiply TransformMatrix, TransformMatrix, TempMatrix
D3DXMatrixIdentity TempMatrix
TempMatrix.m41 = X: TempMatrix.m42 = Y: TempMatrix.m43 = Z: D3DXMatrixMultiply TransformMatrix, TransformMatrix, TempMatrix
End Function
Sub TransformGeometry(X As Single, Y As Single, Z As Single, Mat As D3DMATRIX, OutX As Single, OutY As Single, OutZ As Single)
OutX = (X * Mat.m11) + (Y * Mat.m21) + (Z * Mat.m31) + Mat.m41
OutY = (X * Mat.m12) + (Y * Mat.m22) + (Z * Mat.m32) + Mat.m42
OutZ = (X * Mat.m13) + (Y * Mat.m23) + (Z * Mat.m33) + Mat.m43
End Sub
dil4VB
12-08-2004, 09:17 AM
Thanks for the help...
nice bit of code...:-)
Duckeroo
12-14-2004, 03:42 PM
The above shows how to rotate the WORLD matrices around your character, but I wouldn't do it that way. I would prefer to keep my world relatively static and move the camera around. Doing it this way would leave you a stable coordinate system for moving your character through your game. If you actually rotate the world, then you have to do a lot of math to keep track of where "north" is. Moving the camera (DirectX calls this the "view" matrix) lets you keep "north" in the same mathematical position while you choose to rotate the camera to look "east."
So I'd do something similar to this:
Dim Angle as Single
'make this global so that when you call your update
'routine, it doesn't get reinitialized (yes, I know you can
'set up your routines so that the variable isn't
'reinitialized, but I'm lazy)
.
.
.
In your game updating loop/subroutine
Dim Character_World as D3DMATRIX
'I'm assuming you want to see your character, so you have give him a
'world matrix to move him
Dim Camera_View as D3DMATRIX 'your "camera" you'll look though
Dim CameraX As Single 'where your camera is located in your
Dim CameraY as Single 'coordinate system
Dim CameraZ as Single
Dim CameraRadius as Single
'how far the camera is away from your character
Dim Theta as single 'use greek letter to represent angle in radians
Const Pi =3.14159
UpdateMovingObjects
'call this subroutine to for all the moving objects in
'your game. it will update their WORLD matrices
UpdateCharacter Character_World
'you will need your character's WORLD matrix in order to point your camera
Angle = Angle + KeyCheckRoutine
'there are a lot of ways to check whether a key is pressed or not, this
'assumes that KeyCheckRoutine will return +1 or -1 and update Angle
'appropriately. (If this moves your camera to fast or slow, then decrease or
'increase the return from KeyCheckRoutine - or your equivalent method of
'checking keys)
If Angle < 0 then Angle = Angle + 360
If Angle > 360 then Angle = Angle - 360
'keep angle between 0 and 360 degrees
Theta = Angle * Pi / 180
'Change Angle to radians
'Next we calculate where your camera is sitting in space
'in the WORLD matrix, .m41 = x coordinate, .m42 = y coord, .m43 = z coord
'These define a circle around your character
CameraX = Character_World.m41 + CameraRadius * Cos (Theta)
CameraY = Character_World.m42 + CameraRadius * Sin (Theta)
'So that your camera is not level with the feet of your character (and not on
'the ground!
CameraZ = Character_World.m43 + 1.8
'assumes your coordinates use meters and the avg person is 1.8m tall, makes
'the camera level with the character's head (change as necessary for your
'game)
'Now call the D3D routine that will update the view matrix
With Character_World
D3DMatrixLookAtLH Camera_View, Vec3(CameraX, CameraY, CameraZ), _
Vec3(.m41, .m42, .m43 + 1.8), Vec3(0, 0, 1)
End With
D3DDevice.SetTransform D3DTS_VIEW, Camera_View
.
.
.
update rest of your scene and render as normal
In the above, Vec3() is a helper function that turns 3 coordinates into a D3DVector (there are a lot of examples out there). In this case the camera is located on a circle, as defined by the radius, around your character and level with his head (first Vec3 values). The camera is pointed at your character, so you're always looking at him to see the world around him (second Vec3 values). The third Vec3 values assume that "up" in your coordinate system is the z-axis.
Hope this helps.
PS. I haven't figured out formating with this message board yet. So the indentations and stuff I'd have done for readability are missing. I've tried to clean it up as much as I can.