Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi there, I am very new to Direct3D but I am willing to learn it and i like it despite i'm facing trouble in matrices (the whole thing) I just need help!!

I understand matrices as math and also i got the coordinates thing in Direct3D but i can't find a resource where i can learn how to use matrices in Direct3D programming.

I want to make simple (i think) thing which is:

 

I load a gamefield mesh then i load a number of boxes then i want to move these boxes and (also) i want to move the camera throughout the game field.:)

 

I learnt how to load meshes but i need to put them in specefic places on the gamefield, so i know that i must assign a matrix to each object Right?? then how?? can anybody explain??

 

Also when i try to make the camera move i get: strafe right and left and move ahead and back but how can i rotate it and not mess with the coordinates??

 

thanks in advance:)

Posted

ok, here's the camera part, took me a while to understand it myself so if you have questions feel free to ask. I have this in it's own Class (Camera) and I haven't made it look up and down yet:

 


   Public xPos, yPos, zPos, xView, yView, zView, xUp, yUp, zUp, xStrafe, yStrafe, zStrafe As Single

   Public Sub New(ByVal x As Single, ByVal y As Single, ByVal z As Single, _
                   ByVal xV As Single, ByVal yV As Single, ByVal zV As Single, _
                   ByVal xU As Single, ByVal yU As Single, ByVal zU As Single)
       xPos = x
       yPos = y
       zPos = z
       xView = xV
       yView = yV
       zView = zV
       xUp = xU
       yUp = yU
       zUp = zU
   End Sub

   Private Sub UpdateCamera(ByVal xdir As Single, ByVal zdir As Single, ByVal dir As Single)
       xPos += xdir * dir
       zPos += zdir * dir

       xView += xdir * dir
       zView += zdir * dir
   End Sub

   Public Sub MoveCamera(ByVal amount As Single) 'Amount will be Negative for moving backwards
       Dim xLookDirection, yLookDirection, zLookDirection As Single

       xLookDirection = xView - xPos
       yLookDirection = yView - yPos
       zLookDirection = zView - zPos

       Dim dp As Single = CSng(Math.Sqrt(xLookDirection * xLookDirection + yLookDirection * yLookDirection + zLookDirection * zLookDirection))
       xLookDirection /= dp
       yLookDirection /= dp
       zLookDirection /= dp

       UpdateCamera(xLookDirection, zLookDirection, amount)
   End Sub

   Public Sub StrafeCamera(ByVal direction As Single) 'Negative = Left, Positive = Right
       CalculateStrafe()
       UpdateCamera(xStrafe, zStrafe, direction)
   End Sub

   Private Sub CalculateStrafe() 

       Dim xDir, yDir, zDir As Single
       Dim xCross, yCross, zCross As Single

       xDir = xView - xPos
       yDir = yView - yPos
       zDir = zView - zPos

       Dim dp As Single = 1 / CSng(Math.Sqrt(xDir * xDir + yDir * yDir + zDir * zDir))
       xDir *= dp
       yDir *= dp
       zDir *= dp

       xCross = (yDir * zUp) - (zDir * yUp)
       yCross = (zDir * xUp) - (xDir * zUp)
       zCross = (xDir * yUp) - (yDir * xUp)

       xStrafe = xCross
       yStrafe = yCross
       zStrafe = zCross
   End Sub

   Public Sub RotateCamera(ByVal AngleDir As Single, ByVal xSpeed As Single, ByVal ySpeed As Single, ByVal zSpeed As Single) 'AngleDir will be Negative for rotating left, positive for right
       Dim xNewLookDirection, yNewLookDirection, zNewLookDirection As Single
       Dim xLookDirection, yLookDirection, zLookDirection As Single
       Dim CosineAngle, SineAngle As Single

       CosineAngle = CSng(Math.Cos(AngleDir))
       SineAngle = CSng(Math.Sin(AngleDir))

       xLookDirection = xView - xPos
       yLookDirection = yView - yPos
       zLookDirection = zView - zPos

       Dim l As Single = CSng(Math.Sqrt(xLookDirection * xLookDirection + yLookDirection * yLookDirection + zLookDirection * zLookDirection))
       xLookDirection /= l
       yLookDirection /= l
       zLookDirection /= l

       xNewLookDirection = (CosineAngle + (1 - CosineAngle) * xSpeed) * xLookDirection
       xNewLookDirection += ((1 - CosineAngle) * xSpeed * ySpeed - zSpeed * SineAngle) * yLookDirection
       xNewLookDirection += ((1 - CosineAngle) * xSpeed * zSpeed + ySpeed * SineAngle) * zLookDirection

       yNewLookDirection = ((1 - CosineAngle) * xSpeed * ySpeed + zSpeed * SineAngle) * xLookDirection
       yNewLookDirection += (CosineAngle + (1 - CosineAngle) * ySpeed) * yLookDirection
       yNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed - xSpeed * SineAngle) * zLookDirection

       zNewLookDirection = ((1 - CosineAngle) * xSpeed * zSpeed - ySpeed * SineAngle) * xLookDirection
       zNewLookDirection += ((1 - CosineAngle) * ySpeed * zSpeed + xSpeed * SineAngle) * yLookDirection
       zNewLookDirection += (CosineAngle + (1 - CosineAngle) * zSpeed) * zLookDirection


       xView = xPos + xNewLookDirection
       yView = yPos + yNewLookDirection
       zView = zPos + zNewLookDirection
   End Sub

Posted
I really appreciate your help but i just still don't get it. First I program in c# but i managed to convert your code (thanx):) but that was not the thing i need. What i needed is to learn how to use matrices in Direct3D and make things and cameras move. Although i need it to be simple with explanation (I'm still new you know). Another thing is that I saw your Camera calss but i don't know how to use it with the view matrix (i think). So I need help:confused:
Posted

oh ok. That's the easy part :) (if you're bad at that kinda math like me) well, for the camera part here's what I do (using the afore mentioned Camera class):

 

d3dDevice.Transform.View = Matrix.LookAtLH(New Vector3(View.xPos, View.yPos, View.zPos), _
                                                   New Vector3(View.xView, View.yView, View.zView), _
                                                   New Vector3(View.xUp, View.yUp, View.zUp))   'set the view transformation
       d3dDevice.Transform.Projection = Matrix.PerspectiveFovLH(Convert.ToSingle(Math.PI) / 4, 1.0F, 1.0F, 100.0F) 'and finally set the projection matrix

 

Now explaining, Device.Transform is the group of matrices that are in the device, modify these, modify the world.

LookAtLH is simple take the camera position (the second argument) and point it at the camera view Vector (the second argument) The final argument is simply which way is up, in the form of a Vector.

Then we come to the Projection Matrix, like I said I'm not good at this kind of math so I usually don't change the values from the above (if it works, it works right?) In laymens terms the first argument is field of vision (requires complicated math, I don't tinker with the above equation) the second I just don't understand lol, the third is the closest coordinate you can see and the final argument is the farthest coordinate you can see.

 

If you need more help, I'm always willing

Posted

Hi there. Thanx for your help. I think i'm beginning to get it. About the arguments in the projection matrix they are:

1st: Field (angle) of view (usually set to 90 degrees (pi/4) just to resemble the human eye) if you increase this angle you will see more of the view but also there will be more render and if you decrease this angle you will see less of the view.

2nd: Aspect ratio (Ratio of width over height of the field of view) this is usually set to 4/3 because the nature of the monitor is rectangle (not square) and this will make you see the square or cube as it is in reality

3rd: The most close distance your camera will view (i think that the closest is the better. I set it to 0.1)

4th: The mose far distance your camera will view (the less is the better for rendering optimization but not too less) We are usually not interested in viewing very far objects so this helps us not to draw them.

 

About me: I'm doing well with matrices and now i can load a complete view and i think i'll be able to move through it soon (thanx):)

 

I have another question if you can help:

actually they are two:

1- How can i detect collision?? :confused:

2- How can i draw shadows?? (I do lighting but there is no shadows):confused:

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...