Results 1 to 19 of 19

Thread: spherical coordinates??? (to make a camera's 1st person view)

  1. #1

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172

    spherical coordinates??? (to make a camera's 1st person view)

    Ok i want to make a game in directx8 and everything works really nice up to now.. but i really want to try and creating a way to make a first person kind of view and zaei told me to look up spherical coordinates on google , so i did and read some stuff about it .. but it all looks like big mumbo jumbo to me.. i got good grades at maths but didnt ever cover those kind of things ... can anybody please help me with that? :S
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  2. #2
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    For this application, all you are really interested in is coversion from spherical to cartesian coordinates. A point defined in spherical coordinates has 3 values, r, p, and t. r is the distance from the origin, p is the lattitude angle, t is the longitude angle. For looking around, you simply modify the p and t values of a spherical coordinate (the angles that define the direction you are looking). r will stay constant at 1. To conver, use this formula:
    Code:
    x = r * sin(p) * cos(t)
    y = r * sin(p) * sin(t)
    z = r * cos(t)
    Z.

  3. #3
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    in games where your camera is fixed horizontally, enabled to rotate along its center (1st person view) or an arbitrary center (3rd persion view) along vertical axis (corresponding to angle t in code zaei posted) and vertically in your camera's pov around the center, that is axis normal to horizontal pov (corresponding to angle p in zaei's code). In in flightsims or spacesims you'd rahter not want your camera restricted this way, but be able to rotate freely around any axis so you use quaternions. For a rotated vector vrot of v around axis u you have
    in flightsims or spacesims
    vrot=(u.v)u + cos(theta)(v-(u.v)u)+sin(theta)(u x v)
    theta is the angle to rotate, . here is scalar product and x is the vector product.
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  4. #4

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    it is ment to be used for a first person shooter kinda look. but i cant seem to make it work correctly (zaei's code) it works a bit but it moves left and right when just adding 1 to the numbers :S cant seem to really let it work perfectly sigh
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  5. #5
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    The values of p and t are in radians, so a full circle is 0..2pi, instead of 0..360.

    Z.

  6. #6

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok something is going wrong now :s it is only moving up a bit and then down again instead of going in a circle am sure i am doing something wrong :S
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  7. #7
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Post some code snippets =)

    Z.

  8. #8

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok hangon please stay here :P
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  9. #9

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    key down
    VB Code:
    1. Case 201 ' page up
    2.   t = t + 0.1
    3.  player.lookatx = r * Sin(p) * Cos(t)
    4.  player.lookaty = r * Sin(p) * Sin(t)
    5.  player.lookatz = r * Cos(t)
    6.  Case 209
    7.   t = t - 0.1 ' page down
    8.   player.lookatx = r * Sin(p) * Cos(t)
    9.  player.lookaty = r * Sin(p) * Sin(t)
    10.  player.lookatz = r * Cos(t)

    Every loop it does this
    VB Code:
    1. engine_d3d.camera_position player.x, player.y, player.z
    2.  engine_d3d.camera_lookat player.lookatx, player.lookaty, player.lookatz

    which is a link to this in the dll

    VB Code:
    1. Public Sub camera_position(x As Single, y As Single, z As Single)
    2.  camera.posx = x
    3.  camera.posy = y
    4.  camera.posz = z
    5. End Sub
    6.  
    7. Public Sub camera_lookat(x As Single, y As Single, z As Single)
    8.  camera.atx = x
    9.  camera.aty = y
    10.  camera.atz = z
    11. End Sub


    the code it really takes place
    VB Code:
    1. D3DXMatrixLookAtLH matView, MakeVector(camera.posx, camera.posy, camera.posz), MakeVector(camera.atx, camera.aty, camera.atz), MakeVector(0, 1, 0)
    2.  D3DDevice.SetTransform D3DTS_VIEW, matView
    3.  D3DDevice.SetTransform D3DTS_WORLD, matWorld
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  10. #10
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Ok, the lookat coordinate is going to be the converted spherical coordinate added to the player's position. Also, if you want to look up and down, modify the p value, not the t value. t is for rotation around the y axis.

    Z.

  11. #11

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    er so how should i put in the program? veye , vat and vup what are they precisely :S
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  12. #12
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    veye == camera position
    vat == where camera is looking
    vup == vector that is pointing "up"

    You have the matrix generation correct, just not how you calculate your lookat coordinate. It should be the spherical coordinate plus the camera position (since the spherical coordinate is defined in relation to the origin, you want it in relation to your camera position).

    Z.

  13. #13

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok i am feeling more stupid every time you post :S it might be the fact that i am dutch and i dont get the level of the words .... but that high calculation of maths wasnt that great :S.. can you help me with an example? also i have to give the line a D3dvector but how can i convert that to a correct vector then or something ? :S
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  14. #14
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Originally posted by Zaei

    Code:
    x = r * sin(p) * cos(t)
    y = r * sin(p) * sin(t)
    z = r * cos(t)
    I think the formula for z is wrong. It should be r * cos(p). This formula is valid only for a right handed coordinate system (with the y axis pointing into the screen and the z axis pointing up).
    For a left handed coordinate system (in which most games are, I think), the values for y and z should be swapped.

  15. #15
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    Originally posted by riis
    I think the formula for z is wrong. It should be r * cos(p). This formula is valid only for a right handed coordinate system (with the y axis pointing into the screen and the z axis pointing up).
    For a left handed coordinate system (in which most games are, I think), the values for y and z should be swapped.
    ooh, good eye =). It should be cos(p).

    Z.

  16. #16

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok you figured that out :P but err still i dunno how to apply these values. since you told me i shouldnt change it to a d3dvector but that is the only thing it accepts in the position part , what do i have to aply the values too? :S
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

  17. #17
    Frenzied Member Zaei's Avatar
    Join Date
    Jul 2002
    Location
    My own little world...
    Posts
    1,710
    position == player position
    lookat == player position + converted spherical coordinates.

    Imagine a big sphere around yourself. Any direction you look, you are loking at a point on that sphere, given by 2 angles. Howver, the center of that sphere is at your position, NOT the origin, so you need to move the sphere (adding the players position)

    Z.

  18. #18
    Fanatic Member riis's Avatar
    Join Date
    Nov 2001
    Posts
    551
    Those angles are equivalent with latitude and longitude (breedte- en lengtegraad in dutch) on a globe. You (actually your eyes) are at the center of the globe, not somewhere on the outside. So, you look onto the inside of the globe's surface.

  19. #19

    Thread Starter
    Frenzied Member Ultimasnake's Avatar
    Join Date
    Feb 2002
    Location
    Amsterdam, holland
    Posts
    1,172
    ok i am still reaaally confused and STILL CANT SEEM TO MAKE IT WORK :'(

    VB Code:
    1. Case 201 'pageup
    2.   p = p + 0.01
    3.   player.lookatx = player.x + (r * Sin(p) * Cos(t))
    4.   player.lookaty = player.y + (r * Sin(p) * Sin(t))
    5.   player.lookatz = player.z + (r * Cos(p))
    6.  Case 209 ' pagedown
    7.   p = p - 0.01
    8.   player.lookatx = player.x + (r * Sin(p) * Cos(t))
    9.   player.lookaty = player.y + (r * Sin(p) * Sin(t))
    10.   player.lookatz = player.z + (r * Cos(p))

    but as soon as i press any of those keys the screen gets black.. WHAT THE HELL AM I DOING WRONG


    darn plllllzzzz help me
    For my PC and MS Smartphone 2003 software visit
    http://www.ultimasoftware.nl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width