Click to See Complete Forum and Search --> : spherical coordinates??? (to make a camera's 1st person view)
Ultimasnake
Feb 20th, 2003, 02:49 PM
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
Zaei
Feb 20th, 2003, 09:00 PM
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:
x = r * sin(p) * cos(t)
y = r * sin(p) * sin(t)
z = r * cos(t)
Z.
kedaman
Feb 21st, 2003, 06:14 AM
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.
Ultimasnake
Feb 21st, 2003, 09:19 AM
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 :(
Zaei
Feb 21st, 2003, 10:58 AM
The values of p and t are in radians, so a full circle is 0..2pi, instead of 0..360.
Z.
Ultimasnake
Feb 21st, 2003, 01:27 PM
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
Zaei
Feb 21st, 2003, 01:29 PM
Post some code snippets =)
Z.
Ultimasnake
Feb 21st, 2003, 01:30 PM
ok hangon please stay here :P
Ultimasnake
Feb 21st, 2003, 01:32 PM
key down
Case 201 ' page up
t = t + 0.1
player.lookatx = r * Sin(p) * Cos(t)
player.lookaty = r * Sin(p) * Sin(t)
player.lookatz = r * Cos(t)
Case 209
t = t - 0.1 ' page down
player.lookatx = r * Sin(p) * Cos(t)
player.lookaty = r * Sin(p) * Sin(t)
player.lookatz = r * Cos(t)
Every loop it does this
engine_d3d.camera_position player.x, player.y, player.z
engine_d3d.camera_lookat player.lookatx, player.lookaty, player.lookatz
which is a link to this in the dll
Public Sub camera_position(x As Single, y As Single, z As Single)
camera.posx = x
camera.posy = y
camera.posz = z
End Sub
Public Sub camera_lookat(x As Single, y As Single, z As Single)
camera.atx = x
camera.aty = y
camera.atz = z
End Sub
the code it really takes place
D3DXMatrixLookAtLH matView, MakeVector(camera.posx, camera.posy, camera.posz), MakeVector(camera.atx, camera.aty, camera.atz), MakeVector(0, 1, 0)
D3DDevice.SetTransform D3DTS_VIEW, matView
D3DDevice.SetTransform D3DTS_WORLD, matWorld
Zaei
Feb 21st, 2003, 01:42 PM
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.
Ultimasnake
Feb 21st, 2003, 01:51 PM
er so how should i put in the program? veye , vat and vup what are they precisely :S
Zaei
Feb 21st, 2003, 02:36 PM
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.
Ultimasnake
Feb 21st, 2003, 02:39 PM
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
riis
Feb 23rd, 2003, 02:31 AM
Originally posted by Zaei
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.
Zaei
Feb 23rd, 2003, 04:59 PM
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.
Ultimasnake
Feb 26th, 2003, 01:30 PM
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
Zaei
Feb 26th, 2003, 02:27 PM
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.
riis
Feb 27th, 2003, 05:03 AM
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.
Ultimasnake
Feb 28th, 2003, 01:51 PM
ok i am still reaaally confused and STILL CANT SEEM TO MAKE IT WORK :'(
Case 201 'pageup
p = p + 0.01
player.lookatx = player.x + (r * Sin(p) * Cos(t))
player.lookaty = player.y + (r * Sin(p) * Sin(t))
player.lookatz = player.z + (r * Cos(p))
Case 209 ' pagedown
p = p - 0.01
player.lookatx = player.x + (r * Sin(p) * Cos(t))
player.lookaty = player.y + (r * Sin(p) * Sin(t))
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
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.