Loading...
Logo
Processing Forum
Hello, 
I am trying to visualiza the flow of tweets and retweets on a a 2D world map. 
Something of this kind.


Just Landed - 36 Hours from blprnt on Vimeo .


Till now I have achieved this. 



Can someone help me out in implementing a 3D interactive camera on this 2D map so that viewers can rotate, zoom etc on the map to see the visualization in a better way. I have tried using Pessay cam but its I am facing severe difficulties due to versions and compatibility issues with mac. 


Edit Note: @amnon.owed: I am sorry for the inconvenience. I don't know why it was not appearing for you because I can see it very well. In any case I have uploaded the pic to this site only from the insert image option rather than using an URL link for the image. Hope you can see it now. 


Thanks. 

Replies(10)

After you say " Till now I have achieved this." I only see a blank space. Are you saying you are starting from zero or was there supposed to be an image and/or some code there?

For now I have moved this to general discussion as no relevant code is included. The general answer is that you can either use camera() or translate(), rotate() and scale() for this type of 3D movement. Personally I prefer the latter, because it will allow much finer control over the movement of objects within your view. I believe peasyCam uses the former to move the camera around a given center (which is great for quickly moving around a centered object but is limited for more advanced movement). Note that there is also the proscene library for much more advanced camera movement within a 3D world.
Thanks for the reply. 
I have also changed the way in which I was inserting the image in the post. Hope you will be able to see it now. 

Can you provide me some further details on how to do this task like an example code or something. 

Thanks a lot. 
Ah, the image is visible now indeed.

With regard to examples, I remember there are some examples on the forum where people want to zoom in and move around an image/canvas bigger than the sketch window. Those are relatively close to what you're doing. You might wanna search for those to find some inspiration.

Here is a basic implementation of a camera (but actually the world moves) based on translate, rotate and scale which can be completely controlled through the mouse (left button, right button, scrollwheel).

Code Example
Copy code
  1. // left mouse button + mouse drag = rotate
  2. // right mouse button + mouse drag = translate
  3. // mouse scroll wheel = scale
  4.  
  5. import processing.opengl.*;
  6. import java.awt.event.*;
  7.  
  8. PVector position = new PVector(450, 450);
  9. PVector movement = new PVector();
  10. PVector rotation = new PVector();
  11. PVector velocity = new PVector();
  12. float rotationSpeed = 0.035;
  13. float movementSpeed = 0.05;
  14. float scaleSpeed = 0.25;
  15. float fScale = 2;
  16.  
  17. void setup() {
  18.   size(900, 900, OPENGL);
  19.   stroke(255, 255, 0);
  20.   strokeWeight(2);
  21.   fill(150, 200, 250);
  22.   addMouseWheelListener(new MouseWheelListener() {
  23.     public void mouseWheelMoved(MouseWheelEvent mwe) {
  24.       mouseWheel(mwe.getWheelRotation());
  25.   }});
  26. }
  27.  
  28. void draw() {
  29.   if (mousePressed) {
  30.     if (mouseButton==LEFT) velocity.add( (pmouseY-mouseY) * 0.01, (mouseX-pmouseX) * 0.01, 0);
  31.     if (mouseButton==RIGHT) movement.add( (mouseX-pmouseX) * movementSpeed, (mouseY-pmouseY) * movementSpeed, 0);
  32.   }
  33.   velocity.mult(0.95);
  34.   rotation.add(velocity);
  35.   movement.mult(0.95);
  36.   position.add(movement);
  37.  
  38.   background(0);
  39.   lights();
  40.  
  41.   translate(position.x, position.y, position.z);
  42.   rotateX(rotation.x*rotationSpeed);
  43.   rotateY(rotation.y*rotationSpeed);
  44.   scale(fScale);
  45.  
  46.   box(90);
  47.   line(-100, 0, 0, 100, 0, 0);
  48.   line(0, -100, 0, 0, 100, 0);
  49.   line(0, 0, -100, 0, 0, 100);
  50. }
  51.  
  52. void mouseWheel(int delta) {
  53.   fScale -= delta * scaleSpeed;
  54.   fScale = max(0.5, fScale);
  55. }
@ amnon.owed:
Thanks for the example. I am using PeasyCam for the project and I also integrated your code but both of them give me a similar problem i.e. is my 2D map breaks when i move it around. 

I have already posted a screenshot of what I get when I run my program. Now when I implement PeasyCam or you code and click and drag the mouse this is what happens. 




Do you have any idea why is this happening ?

I just want my camera to move around in a rotation around my flat map and nothing should happen to my map and the curves on it. 

Here is some part of my code. 

In setup()
Copy code
  1.   size(800, 600, P3D);  
  2.   smooth();  
  3.   camera = new PeasyCam(this, 400, 300, 400, 50);

  4.   worldMapImage = loadImage("world.png"); 
  5.   mercatorMap = new MercatorMap(800, 600);

In draw()

Copy code
  1. image(worldMapImage, 0, 0, width, height);
  2.   noStroke();
  3.   fill(255, 0, 0);
  4.   stroke(255, 0, 0);

Same thing happens if I change P3D to OPENGL
What happens when you add the following line to the top of your draw() loop:
Copy code
  1. background(0);
Yup that worked to a great extent 
Thank you. 

Why do you need to do  background(0); ?

Here is a screenshot:





As you can see the whole map moves. I don't want such independent movement. All I want is to ability to revolve the camera around the map so that I can see the map from all the side by rotating it. Something similar to done in the video. How can I achieve this ?

Thanks a lot. 
Without a background call the drawing is cumulative. When you are in 3D this works a little differently, but it still applies.

What you see in the video can be achieved with the methods in the example. You just need to tweak them (and you actually need less degrees of freedom to move around), especially the order. For example change the point of origin to the center of the map, then rotate around it over one axis while the other axis is slightly tilted etc.
Where do you refer when you say methods in examples ?


For example change the point of origin to the center of the map, then rotate around it over one axis while the other axis is slightly tilted etc.


Can you point me how to do this. I am quite new to processing and clueless about how to approach it. 
The methods (translate, rotate and scale) I've been talking about all the time and the code example I posted above. Try them one by one and also in a different order (which will matter for the final result). With Processing it's easy to experiment with code and see the visual result. You may even use println, text() or frame.setTitle() to see the exact numbers of what you're doing in realtime. To change the origin you can use translate. The center of the object is in the middle of the map. Once you've translated by (mapwidth/2, mapheight/2) then the origin is centered in the map. When you rotate then the rotation will be around this point of origin. Just experiment to see the difference.
Thanks a lot for the reply. 
I am was using PeasyCam till now but I think to obtain desired results I should switch to the code above and then do the tweaking. 

Thanks a lot :)