Point on the surface of a sphere
Blitz3D Forums/Blitz3D Programming/Point on the surface of a sphere
| ||
I'm trying to work out how to locate the x,y screen co-ordinates of a point on the surface of a sphere. Let's say the sphere is the earth and I know that the longitude and latitude of Milan is -45.3, 9.11. How do I convert those 'tudes into the x,y screen position (assuming that the rotation and tilt of the globe are both zero)? |
| ||
See: CameraProject. |
| ||
Yeah, I've been looking at that, but i can't work out how to locate the exact point on the sphere. |
| ||
Ah, I see. Well, if I understand longitute and latitude correctly, you could try something like this: - Create another sphere that's the same size as the Earth and flip the mesh. Give the sphere an alpha of 0 so it's not actually rendered. - Create a pivot and place it at the centre of the Earth, then point it at the prime meridian. - Set up collisions so that the pivot is collideable with the flipped sphere mesh. Now, to find a point, rotate the pivot yaw by the longitute, then rotate it's pitch by the latitude. Next, just MoveImage the pivot forward by enough so that it collides with the sphere. Then you can read CollisionX/Y/Z to find the point of the collision. Plug those values into CameraProject and voila! (I think :P) Some of the above may need some tweaking, but you get the idea. :) |
| ||
You could do exactly the same by not having the second sphere and just moving the pivot forward the radius of the sphere and then just getting entityx,y,z of the pivot. Much like what I've done in http://www.blitzbasic.com/Community/posts.php?topic=69483 |
| ||
Yeah, good point. |
| ||
I actually took a different route, but thanks for your suggestions guys. :)Local latitude# = 180+(-53.10) ;Wembley Local longitude# = 90-(0.07) Local x# = Cos(longitude) * Sin(latitude) Local y# = Sin(longitude) * Sin(latitude) Local z# = Cos(latitude) CameraProject(camera_globe,x,y,z) That finds my point on the globe (Wembley). If I want to spin the globe I just pivot the camera. :) ![]() |
| ||
Hi Siread, I tried that formula and it only works for locations on or near the prime meridian. I think it should be something like this if your globe is oriented normally with North pointing up the Y axis. latitude# = -53.10 ;Wembley longitude# = 0.07 theta#=90+latitude : phi#=180+longitude z# = Sin(theta) * Cos(-phi) x# = Sin(theta) * Sin(-phi) y# = Cos(theta) This is based on the formula given here http://local.wasp.uwa.edu.au/~pbourke/texture_colour/spheremap/ adjusted slightly for the way the axis are oriented in blitz. |
| ||
Thanks David, but if only you'd been around a week ago. I spent 2 days getting the formula right! :D |
| ||
Oops. Late again. :) Well I started off using a pivot and turning that and then tforming the normal, sort of as suggested above until I got around to trying your more direct approach. I was also farting about with this and that as I do, along the way adding a list of a few hundred locations, which made figuring out the fact that the formula was using different axis easier. It still took me quite a while for it to dawn on me though, I hate this maths stuff. I was also using aligntovector to calculate pitch and yaw and so latitude and longitude again using the pivot so I could display this as I tracked over the globe, but aligntovector gets a bit flaky near the axis so I am now using this, again based on information on Paul Bourkes website and which took a lot less time to figure out knowing the difference in axis. Picked=CameraPick(Camera,MouseX(),MouseY()) If Picked=Globe Then TFormPoint PickedX(),PickedY(),PickedZ(),0,Globe Theta#=90-ATan2(Sqr( TFormedX()^2+TFormedZ()^2 ),-TFormedY()) ; latitude Phi#=ATan2( TFormedX(),-TFormedZ() ) ; Longitude ;... Endif |