Can I change mouse values?
in
Programming Questions
•
10 months ago
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?
...or am I going to have to unzoom it?
- import processing.opengl.*;
- int playerS = 25;
- float playerX, playerY;
- float accX, accY;
- float rotation;
- int gunL = 20;
- int gunW = 15;
- boolean pressUp = false;
- boolean pressDown = false;
- boolean pressLeft = false;
- boolean pressRight = false;
- void setup() {
- size(900, 600, OPENGL);
- smooth();
- cursor(CROSS);
- rectMode(CENTER);
- playerX = width/2;
- playerY = height/2;
- }
- void draw() {
- camera(playerX, playerY, 300,
- playerX, playerY, 0,
- 0, 1, 0);
- background(255);
- fill(255);
- rect(width/2, height/2, width, height);
- rotation = atan2(mouseY - playerY, mouseX - playerX);
- pushMatrix();
- translate(playerX, playerY);
- rotate(rotation);
- fill(255, 0, 0);
- rect(gunL/2, 0, gunL, gunW);
- popMatrix();
- playerX += accX;
- playerY += accY;
- fill(0, 0, 255);
- ellipse(playerX, playerY, playerS, playerS);
- if (accX > 0)
- accX -= 0.2;
- if (accX < 0)
- accX += 0.2;
- if (accY > 0)
- accY -= 0.2;
- if (accY < 0)
- accY += 0.2;
- if (pressUp == true)
- accY -= 2;
- if (pressDown == true)
- accY += 2;
- if (pressLeft == true)
- accX -= 2;
- if (pressRight == true)
- accX += 2;
- if (accX > 6)
- accX = 6;
- if (accX < -6)
- accX = -6;
- if (accY > 6)
- accY = 6;
- if (accY < -6)
- accY = -6;
- }
- void keyPressed() {
- if (key == 'w')
- pressUp = true;
- if (key == 's')
- pressDown = true;
- if (key == 'a')
- pressLeft = true;
- if (key == 'd')
- pressRight = true;
- }
- void keyReleased() {
- if (key == 'w')
- pressUp = false;
- if (key == 's')
- pressDown = false;
- if (key == 'a')
- pressLeft = false;
- if (key == 'd')
- pressRight = false;
- }
1