• fullscreen
  • rotationTest.pde
  • //camera fields
    PVector camPosition;
    PVector camFocus;
    float focusAngle = - PI / 2; 
    
    //time elapsed since last call to draw()
    int lastTime = 0;
    int timeElapsed = 0;
    
    //side-effect fonction : given focusAngle, set the new camFocus.
    void computeCamFocus() {
      
      camFocus.x = camPosition.x; 
      camFocus.y = camPosition.y - 50;
    
      /*Math here !
       If you rotate point (px, py) around point (ox, oy) by angle theta you'll get:
       
       p'x = cos(heta) * (px-ox) - sin(theta) * (py-oy) + ox
       p'y = sin(theta) * (px-ox) + cos(theta) * (py-oy) + oy
       */
       
      float diffX = camFocus.x - camPosition.x; 
      float diffY = camFocus.y - camPosition.y;
      float sinAngle = sin(focusAngle); //Little optimisation: do not recompute twice the cos and sin value.
      float cosAngle = cos(focusAngle); //Little optimisation: do not recompute twice the cos and sin value.
    
      camFocus.x = cosAngle * diffX  - sinAngle * diffY + camPosition.x;
      camFocus.y = sinAngle * diffX  + cosAngle * diffY + camPosition.y;
    }
    
    void setup() {
    
      size(800, 600);
      frameRate(30);
    
      smooth();
      
      camPosition = new PVector(167, 120);
      camFocus = new PVector();
    }
    
    void keyPressed() {
    
      float camShift = timeElapsed / 10.0;
    
      if (keyCode == UP) {
        camPosition.y -=camShift;
      } 
      if (keyCode == DOWN) {
        camPosition.y +=camShift;
      }
      if (keyCode == LEFT) {
        camPosition.x -=camShift;
      } 
      if (keyCode == RIGHT) {
        camPosition.x +=camShift;
      }
    }
    
    void mouseDragged() {
      int diffX = pmouseX - mouseX;
      int diffY = pmouseY - mouseY;
    
      focusAngle -= (diffX * PI / 40f) ;
    }
    
    void draw() {
    
      background(125);
      timeElapsed = millis() - lastTime;
      lastTime+=timeElapsed;
    
      computeCamFocus();
    
      fill(255, 122, 122);  
      ellipse(camPosition.x, camPosition.y, 7, 7);
    
      fill(122, 122, 255);
      line(camPosition.x, camPosition.y, camFocus.x, camFocus.y);
    }
    
    

    code

    tweaks (0)

    about this sketch

    This sketch is running as Java applet, exported from Processing.

    license

    advertisement

    urzq

    FPS-like camera, in 2D

    Add to Faves Me Likey@!
    You must login/register to add this sketch to your favorites.

    A quick and well written (^^) sketch to test rotation formulas.
    Also show how to compute "precise" character movement.

    INSTRUCTIONS:
    > Move the camera with arrows.
    > Move the focus by dragging the mouse

    frankie zafe
    28 Dec 2012
    i tried this some time ago, the problem being to enable two keys at the same time (up/ight for instance). did you investigate further?
    urzq
    29 Dec 2012
    Hi,

    I need to rewrite this sketch, but in the meantime, I wrote a quick demo for you:
    http://www.openprocessing.org/sketch/84261

    It shows you how to handle the "double key problem", simply by saving the state of each arrow as booleans.
    Then, you are able to process those booleans inside the Draw() function

    I hope it helps...
    You need to login/register to comment.