Loading...
Logo
Processing Forum
Is there a way to change mouseX/Y values to a space within the program instead of the program window? If you see in this sketch, I want the rectangle to be pointing to the mouse but since I zoomed in with camera the values for mouseX/Y are different then they appear (when the player moves off center).

...or am I going to have to unzoom it?
Copy code
  1. import processing.opengl.*;

  2. int playerS = 25;
  3. float playerX, playerY;
  4. float accX, accY;
  5. float rotation;
  6. int gunL = 20;
  7. int gunW = 15;
  8. boolean pressUp = false;
  9. boolean pressDown = false;
  10. boolean pressLeft = false;
  11. boolean pressRight = false;

  12. void setup() {
  13.   size(900, 600, OPENGL);
  14.   smooth();
  15.   cursor(CROSS);
  16.   rectMode(CENTER);

  17.   playerX = width/2;
  18.   playerY = height/2;
  19. }

  20. void draw() {
  21.   camera(playerX, playerY, 300,
  22.   playerX, playerY, 0,
  23.   0, 1, 0);

  24.   background(255);
  25.   fill(255);
  26.   rect(width/2, height/2, width, height);

  27.   rotation = atan2(mouseY - playerY, mouseX - playerX);
  28.   pushMatrix();
  29.   translate(playerX, playerY);
  30.   rotate(rotation);
  31.   fill(255, 0, 0);
  32.   rect(gunL/2, 0, gunL, gunW);
  33.   popMatrix();

  34.   playerX += accX;
  35.   playerY += accY;

  36.   fill(0, 0, 255);
  37.   ellipse(playerX, playerY, playerS, playerS);

  38.   if (accX > 0)
  39.     accX -= 0.2;
  40.   if (accX < 0)
  41.     accX += 0.2;
  42.   if (accY > 0)
  43.     accY -= 0.2;
  44.   if (accY < 0)
  45.     accY += 0.2;

  46.   if (pressUp == true)
  47.     accY -= 2;
  48.   if (pressDown == true)
  49.     accY += 2;
  50.   if (pressLeft == true)
  51.     accX -= 2;
  52.   if (pressRight == true)
  53.     accX += 2;

  54.   if (accX > 6)
  55.     accX = 6;
  56.   if (accX < -6)
  57.     accX = -6;
  58.   if (accY > 6)
  59.     accY = 6;
  60.   if (accY < -6)
  61.     accY = -6;
  62. }

  63. void keyPressed() {
  64.   if (key == 'w')
  65.     pressUp = true;
  66.   if (key == 's')
  67.     pressDown = true;
  68.   if (key == 'a')
  69.     pressLeft = true;
  70.   if (key == 'd')
  71.     pressRight = true;
  72. }

  73. void keyReleased() {
  74.   if (key == 'w')
  75.     pressUp = false;
  76.   if (key == 's')
  77.     pressDown = false;
  78.   if (key == 'a')
  79.     pressLeft = false;
  80.   if (key == 'd')
  81.     pressRight = false;
  82. }

Replies(19)

Can you clarify the question, it doesn't seem to make sense in it's present form (to me at least!)? :D
I was afraid of that...

Anyways, as you can see from running the sketch the rectangle coming out of the ball doesn't always point towards the mouse and that's because the values for the mouse doesn't match with the game space (if that makes sense). If you comment out the camera it'll work appropriately. What I'm asking is if I can keep it zoomed in and have the rectangle follow the mouse?

there's a wild thing Robot
http://docs.oracle.com/javase/1.5.0/docs/api/java/awt/Robot.html

that can change mouse pos

It's used here http://www.openprocessing.org/sketch/25255

but I am not sure if you mean this....

or look at screenX and screenY ? No.....      

but maybe http://processing.org/reference/modelX_.html combined with Robot?                       
I'm not sure ... :s ... I don't fully understand the issue myself.

Moving the player character doesn't change the mouse value (although I want it to, because the mouse IS moving within the game space if the player is moved), the only way to change the mouse value is from moving the mouse within the program window.


processing is a peaceful environement.

that's why your gun doesn't work





why are you using OPENGL?
you could do 2D.
then you would have a viewport that wanders with your player?

How can you do that without camera?


  • the mouse pos is ok.
  • the gun is also ok.

but the angle is wrong

the line
Copy code
  1.   rotation = atan2((mouseY - playerY, mouseX - playerX);

is the problem.

but I can't figure it out - we need one of the gurus here.






oh, I just solved it.

Copy code
  1. rotation = atan2(mouseY - height/2, mouseX - width/2);

now it works. (Forget about Robot.)


sometimes you are the guru.

That equation causes the rectangle to spin around the center while the previous one causes it to spin around the player.

EDIT:
Oh wait... I get it. Your right. Lol


that's great!



Copy code
  1. import processing.opengl.*;
  2. int playerS = 25;
  3. float playerX, playerY;
  4. float accX, accY;
  5. float rotation;
  6. int gunL = 20;
  7. int gunW = 15;
  8. boolean pressUp = false;
  9. boolean pressDown = false;
  10. boolean pressLeft = false;
  11. boolean pressRight = false;
  12. void setup() {
  13.   size(900, 600, OPENGL);
  14.   smooth();
  15.   cursor(CROSS);
  16.   rectMode(CENTER);
  17.   playerX = width/2;
  18.   playerY = height/2;
  19. }
  20. void draw() {
  21.   background(255);
  22.   camera(playerX, playerY, 500,
  23.   playerX, playerY, -200,
  24.   0, 1, 0);
  25.   fill(111);
  26.   stroke(122);
  27.   // rect(width/2, height/2, width, height);
  28.   rect(width/2, height/2, 11, 11);
  29.   text("center", width/2+13, height/2);
  30.   fill(255);
  31.   stroke(0);
  32.   pushMatrix();
  33.   translate(-40, 0, 10);
  34.   rect(width-30, height-20, 122, 122);
  35.   rect(width-30, height-120, 122, 122);
  36.   rect(width-30, height-220, 122, 122);
  37.   rect(width-30, height-320, 122, 122);
  38.   rect(width-30, height-420, 122, 122);
  39.   popMatrix();
  40.   rotation = atan2(mouseY - height/2, mouseX - width/2);
  41.   pushMatrix();
  42.   translate(playerX, playerY);
  43.   rotate(rotation);
  44.   fill(255, 0, 0);
  45.   rect(gunL/2, 0, gunL, gunW);
  46.   popMatrix();
  47.   playerX += accX;
  48.   playerY += accY;
  49.   fill(0, 0, 255);
  50.   ellipse(playerX, playerY, playerS, playerS);
  51.   if (accX > 0)
  52.     accX -= 0.2;
  53.   if (accX < 0)
  54.     accX += 0.2;
  55.   if (accY > 0)
  56.     accY -= 0.2;
  57.   if (accY < 0)
  58.     accY += 0.2;
  59.   if (pressUp == true)
  60.     accY -= 2;
  61.   if (pressDown == true)
  62.     accY += 2;
  63.   if (pressLeft == true)
  64.     accX -= 2;
  65.   if (pressRight == true)
  66.     accX += 2;
  67.   if (accX > 6)
  68.     accX = 6;
  69.   if (accX < -6)
  70.     accX = -6;
  71.   if (accY > 6)
  72.     accY = 6;
  73.   if (accY < -6)
  74.     accY = -6;
  75. }
  76. void keyPressed() {
  77.   if (key == 'w')
  78.     pressUp = true;
  79.   if (key == 's')
  80.     pressDown = true;
  81.   if (key == 'a')
  82.     pressLeft = true;
  83.   if (key == 'd')
  84.     pressRight = true;
  85. }
  86. void keyReleased() {
  87.   if (key == 'w')
  88.     pressUp = false;
  89.   if (key == 's')
  90.     pressDown = false;
  91.   if (key == 'a')
  92.     pressLeft = false;
  93.   if (key == 'd')
  94.     pressRight = false;
  95. }

Ok, so here's the new problem I'm getting as expected. Having the bullets come out of the player but towards the mouse.

Here's the code I used to test it...
Copy code
  1. import processing.opengl.*;

  2. int playerS = 25;
  3. float playerX, playerY;
  4. float accX, accY;
  5. float rotation;
  6. int gunL = 20;
  7. int gunW = 15;
  8. boolean pressUp = false;
  9. boolean pressDown = false;
  10. boolean pressLeft = false;
  11. boolean pressRight = false;
  12. ArrayList myCircles;
  13. float ballSpeed = 2;

  14. void setup() {
  15.   size(900, 600, OPENGL);
  16.   smooth();
  17.   cursor(CROSS);
  18.   rectMode(CENTER);
  19.   playerX = width/2;
  20.   playerY = height/2;
  21.   myCircles = new ArrayList();
  22.   myCircles.add(new Circle());

  23. }
  24. void draw() {
  25.   camera(playerX, playerY, 300,
  26.   playerX, playerY, 0,
  27.   0, 1, 0);

  28.   background(255);
  29.   fill(255);
  30.   rect(width/2, height/2, width, height);

  31.   rotation = atan2(mouseY - height/2, mouseX - width/2);
  32.   pushMatrix();
  33.   translate(playerX, playerY);
  34.   rotate(rotation);
  35.   fill(255, 0, 0);
  36.   rect(gunL/2, 0, gunL, gunW);
  37.   popMatrix();

  38.   playerX += accX;
  39.   playerY += accY;
  40.   fill(0, 0, 255);
  41.   ellipse(playerX, playerY, playerS, playerS);

  42.   if (accX > 0)
  43.     accX -= 0.2;
  44.   if (accX < 0)
  45.     accX += 0.2;
  46.   if (accY > 0)
  47.     accY -= 0.2;
  48.   if (accY < 0)
  49.     accY += 0.2;

  50.   if (pressUp == true)
  51.     accY -= 2;
  52.   if (pressDown == true)
  53.     accY += 2;
  54.   if (pressLeft == true)
  55.     accX -= 2;
  56.   if (pressRight == true)
  57.     accX += 2;

  58.   if (accX > 6)
  59.     accX = 6;
  60.   if (accX < -6)
  61.     accX = -6;
  62.   if (accY > 6)
  63.     accY = 6;
  64.   if (accY < -6)
  65.     accY = -6;

  66.   if (frameCount > 60*ballSpeed) {
  67.     myCircles.add(new Circle());
  68.     if (ballSpeed > 0.5)
  69.       ballSpeed -= 0.05;
  70.     frameCount = 0;
  71.   }

  72.   for (int i=0; i < myCircles.size(); i++) {
  73.     Circle myCircle = (Circle)myCircles.get(i);
  74.     myCircle.drawCircle();
  75.   }
  76. }

  77. void keyPressed() {
  78.   if (key == 'w')
  79.     pressUp = true;
  80.   if (key == 's')
  81.     pressDown = true;
  82.   if (key == 'a')
  83.     pressLeft = true;
  84.   if (key == 'd')
  85.     pressRight = true;
  86. }

  87. void keyReleased() {
  88.   if (key == 'w')
  89.     pressUp = false;
  90.   if (key == 's')
  91.     pressDown = false;
  92.   if (key == 'a')
  93.     pressLeft = false;
  94.   if (key == 'd')
  95.     pressRight = false;
  96. }

  97. class Circle {
  98.   PVector circle = new PVector(0, 0, 0);
  99.   PVector sum = new PVector(0, 0, 0);
  100.   int circleSize = 10;
  101.   int alph = 300;
  102.   float r = random(255);
  103.   float g = random(255);
  104.   float b = random(255);

  105.   Circle() {
  106.     circle.set(playerX, playerY, 0);
  107.   }

  108.   void drawCircle() {
  109.     fill(r, g, b, alph);

  110.     PVector mouse = new PVector(mouseX, mouseY, 0);
  111.     mouse.sub(circle);
  112.     mouse.mult(0.05);
  113.     sum.add(mouse);
  114.     sum.normalize();
  115.     sum.mult(5);
  116.     circle.add(sum);

  117.     ellipse(circle.x, circle.y, circleSize, circleSize);
  118.   }
  119. }

In bold we have the bullets come out the player but not towards the mouse (same issue with the gun), but if I try to resolve it by changing the bold from "playerX, playerY" to "width/2, height/2" (same solution as the gun) it will shoot towards the mouse but no longer come from the player.
I don't think there's a solution to this to be honest. I was also thinking, instead of using camera to stick to the player, I could keep the player in the center and use translate to move the environment. This way would also cause an issue with the bullets/hit detection (I think...) but may be solvable. Either way, I don't have enough time to experiment sadly. Thanks for the help again, Chrisir.

EDIT:
I'll just have it function like this...
Copy code
  1. import processing.opengl.*;

  2. int playerS = 40;
  3. float playerX, playerY;
  4. float accX, accY;
  5. float rotation;
  6. int gunL = 40;
  7. int gunW = 25;
  8. boolean pressUp = false;
  9. boolean pressDown = false;
  10. boolean pressLeft = false;
  11. boolean pressRight = false;
  12. ArrayList myCircles;
  13. float ballSpeed = 2;

  14. void setup() {
  15.   size(900, 600, OPENGL);
  16.   smooth();
  17.   cursor(CROSS);
  18.   rectMode(CENTER);
  19.   stroke(255);
  20.   strokeWeight(2);
  21.   playerX = width/2;
  22.   playerY = height/2;
  23.   myCircles = new ArrayList();
  24.   myCircles.add(new Circle(random(width), random(height)));
  25. }

  26. void draw() {
  27.   background(255);

  28.   rotation = atan2(mouseY-playerY, mouseX-playerX);
  29.   pushMatrix();
  30.   translate(playerX, playerY);
  31.   rotate(rotation);
  32.   fill(0, 0, 255);
  33.   rect(gunL/2, 0, gunL, gunW);
  34.   popMatrix();

  35.   playerX += accX;
  36.   playerY += accY;
  37.   fill(0, 0, 255);
  38.   ellipse(playerX, playerY, playerS, playerS);

  39.   if (accX > 0)
  40.     accX -= 0.2;
  41.   if (accX < 0)
  42.     accX += 0.2;
  43.   if (accY > 0)
  44.     accY -= 0.2;
  45.   if (accY < 0)
  46.     accY += 0.2;

  47.   if (pressUp == true)
  48.     accY -= 2;
  49.   if (pressDown == true)
  50.     accY += 2;
  51.   if (pressLeft == true)
  52.     accX -= 2;
  53.   if (pressRight == true)
  54.     accX += 2;

  55.   if (accX > 6)
  56.     accX = 6;
  57.   if (accX < -6)
  58.     accX = -6;
  59.   if (accY > 6)
  60.     accY = 6;
  61.   if (accY < -6)
  62.     accY = -6;

  63.   if (frameCount > 60*ballSpeed) {
  64.     myCircles.add(new Circle(random(width), random(height)));
  65.     if (ballSpeed > 0.5)
  66.       ballSpeed -= 0.05;
  67.     frameCount = 0;
  68.   }

  69.   for (int i=0; i < myCircles.size(); i++) {
  70.     Circle myCircle = (Circle)myCircles.get(i);
  71.     myCircle.drawCircle();
  72.   }
  73. }

  74. void keyPressed() {
  75.   if (key == 'w')
  76.     pressUp = true;
  77.   if (key == 's')
  78.     pressDown = true;
  79.   if (key == 'a')
  80.     pressLeft = true;
  81.   if (key == 'd')
  82.     pressRight = true;
  83. }

  84. void keyReleased() {
  85.   if (key == 'w')
  86.     pressUp = false;
  87.   if (key == 's')
  88.     pressDown = false;
  89.   if (key == 'a')
  90.     pressLeft = false;
  91.   if (key == 'd')
  92.     pressRight = false;
  93. }

  94. class Circle {
  95.   PVector circle = new PVector(0, 0, 0);
  96.   PVector sum = new PVector(0, 0, 0);
  97.   int circleSize = 25;
  98.   int alph = 0;
  99.   float r = random(200);
  100.   float g = random(200);
  101.   float b = random(200);

  102.   Circle(float tempX, float tempY) {
  103.     circle.set(tempX, tempY, 0);
  104.   }

  105.   void drawCircle() {
  106.     fill(r, g, b, alph);

  107.     PVector mouse = new PVector(playerX, playerY, 0);
  108.     mouse.sub(circle);
  109.     mouse.mult(0.005);
  110.     sum.add(mouse);
  111.     sum.normalize();
  112.     sum.mult(4);
  113.     circle.add(sum);

  114.     rect(circle.x, circle.y, circleSize, circleSize);
  115.     alph += 8;
  116.   }
  117. }

AW: [WatchList] Can I change mouse values?

10 months ago   - via EMail-to-forum

 

 

isn’t it instead of

  1.     circle.set(playerX, playerY, 0);

 

better

 

  1.     circle.set(width/2, height/2, 0);

 

 

 

Re: [WatchList] Can I change mouse values?

10 months ago   - via EMail-to-forum

Sorry, I am on a journey...

Good luck!

Re: [WatchList] Can I change mouse values?

10 months ago   - via EMail-to-forum

Search viewport on the Forum

Instead of camera you show only one part of the area.

When he moves you don't have to move cam/viewport at once; it's soon enough when he's coming near screenborder