Hi...
I'm working on mapping Cartesian coordinates (Latitude and Longitude) onto Spherical coordinates, and I'm a little bit lost...
I thought to reference the methods in Jer Thorp's "Good Morning"
sketch - and made sure I went through
this tutorial first - and saw that he was using toxilibs... so I copied the methodology, and
looked at the javadocs for those functions...
I'm not sure if I'm calling the functions in the right places, but I've tried to reference what Jer did as much as possible - though his sketch in a little more complicated. I've pared this down to the simplest example :
- import processing.opengl.*;
- import toxi.geom.*;
- float currentLatitude = 52.507244; // Berlin
- float currentLongitude = 13.454436; // Berlin
- float globeRadius = 200;
- float rotationY = 0;
- void setup() {
- size(400, 800, OPENGL);
- }
- void draw() {
- background(0);
- renderGlobe();
- }
- void renderGlobe() {
- Vec2D latLon = new Vec2D();
- latLon.x = currentLatitude;
- latLon.y = currentLongitude;
- Vec3D spherePos = new Vec3D();
- spherePos = latToSphere(latLon, globeRadius);
- Vec3D cartPos = new Vec3D();
- cartPos = sphereToCart(spherePos);
- rotationY += 0.01;
- noFill();
- stroke(155, 100);
- strokeWeight(1);
- pushMatrix();
- translate(width/2, height/2, -globeRadius *4);
- rotateY(rotationY);
- beginShape();
- sphere(400);
- endShape();
- popMatrix();
- pushMatrix();
- translate(width/2, height/2, -globeRadius *4);
- rotateY(rotationY);
- stroke(255, 155, 55);
- strokeWeight(10);
- point(0, 0, spherePos.z * 2);
- stroke(55, 155, 255);
- point(0, 0, 0);
- line(0, 0, 0, spherePos.x * globeRadius, spherePos.y * globeRadius, spherePos.z);
- point(spherePos.x * globeRadius, spherePos.y * globeRadius, spherePos.z);
- popMatrix();
- }
- float degreesToRadians(float d) {
- return((d/180) * PI);
- };
- Vec2D toRadians(float lat, float lon) {
- Vec2D r = new Vec2D();
- lat = currentLatitude;
- lon = currentLongitude;
- r.y = degreesToRadians(-lon) - PI/2;
- r.x = degreesToRadians(-lat) + PI/2;
- return(r);
- };
- Vec3D latToSphere(Vec2D ll, float r) {
- Vec3D v = new Vec3D();
- ll = toRadians(ll.x, ll.y);
- v.x = ll.x;
- v.y = ll.y - PI/2;
- v.z = r;
- return(v);
- };
- Vec3D sphereToCart(Vec3D ll) {
- Vec3D v = new Vec3D();
- v.x = ll.z * cos(ll.y) * sin(ll.x);
- v.y = ll.z * sin(ll.y) * sin(ll.x);
- v.z = ll.z * cos(ll.x);
- return(v);
- };
Now, I am in Berlin, and am using the approx. gps coordinates of that city (52, 13)... and as far as I can see, the point where it intersects the sphere's surface does not see to be Berlin (in fact, I have a textured example as well, and it seems to be in Greenland...)
I know that there's a lot of variables that could be causing this, and I've messed around with them a fair bit and can't seem to grasp it, so if anyone has any ideas, or any experience in dealing with this, that would be appreciated...
cheers,
~ J
1