Having trouble undoing a camera() transformation

hi, I’ve tried searching for a solution online but I wasn’t able to find one I could understand. I’m kinda new to coding in general. This is what I’ve got so far;

  //make varuables
  PVector offset = new PVector(width/2,height/2);
  mouse = new PVector(mouseX,mouseY);
  PVector camadd = mouse.copy().sub(offset);

  //if mouse is far enough from center of screen, then adjust the camera position.
  if(camadd.mag()>150) {cam.add(camadd.setMag(camadd.mag()-150).div(50));}

  //limit how far away the camera is allowed to move from the player.
  cam = cam.copy().sub(player.pos).limit(height/2-height/5).add(player.pos);

  //apply transformation.
  camera(cam.x,cam.y,zoom,  cam.x,cam.y,0,  0,1,0);

  //adjust mouse pos to correct for transformation

I’ve managed to figure out how to correct the x and y values, but as soon as I zoom in or out the mouse position gets distorted. I’ve looked at other threads and they all seemed too complicated for me to understand or implement.

there were some good solutions online but none were usable for me. I need the world pos so I can use it for some other stuff, I can’t just use push() and pop() to fix things. I’ve tried to use screenX and screenY, but those are literally the reverse of what I need. I need to convert a screen position to a world position.

1 Like

You could try modelX(…) etc.


from what I can tell that’s just a 3D version of screenX though?

edit: I just figured out a really stupid way to make it work.


  for(int i = 0; i<250; i++) {
    PVector scrmouse = new PVector(screenX(mouse.x,mouse.y),screenY(mouse.x,mouse.y));
    mouse.add(new PVector(mouseX,mouseY).sub(scrmouse).div(5));

Excuse can you describe your solution?

When it’s the Opposite of screenX where do you get the 3D coordinates from the mouse

Or what is mouse class?

That’s not true. But it’s also not what you are looking for.

since I couldn’t find a legitimate way of doing it, I just had a for() loop that repeatedly adds the difference between the actual mouse position and the position the mouse Pvector appears on the screen. Instead of giving the actual position, it just gets it closer and closer after each loop.

Here, I added stuff to make it easier to understand.

  setup() {
  draw() {
    //place a point at actual pos

    //make a PVector to store mouse pos for easy use
    PVector mouse = new PVector(mouseX,mouseY);

    //put transformations here
    //{ ... }

    //add first point to show where the computer thinks the mouse PVector is

    //get closer and closet to the actual pos every loop
    //place a point at every loop to show progress
    for(int i = 0; i<250; i++) {
      //make a temporary PVector that holds the position of the mouse PVector on the screen
      PVector screen_mouse = new PVector(screenX(mouse.x,mouse.y), screenY(mouse.x,mouse.y));

      //find the difference from the screen pos to the actual pos and add it to the mouse PVector
      mouse.add(new PVector(mouseX,mouseY).sub(screen_mouse).div(5));

      //make a point for demonstration

    //place final point to show true pos;

I don’t think I really understand your question very well. I’m using the z dimension to zoom in. Everything but the camera is at z = 0. I suppose I didn’t actually say that though, so sorry if there was any confusion.

1 Like

Thanks for that

cf. Pro-Question: place a point in 3D with mouse - #6 by jeremydouglass