Hi, i started working on my player movements and i realise that i have a issue with the the coordinates of my mouse when i’m using a camera method.
Here is my code:
Player player;
LimitOfTheWorld limitoftheworld;
PVector view;
int mode;
int worldSize = 500;
void setup(){
surface.setResizable(true);
size(250,250);
PVector pos = new PVector(250,250);
PVector pos2 = new PVector(10,10);
player = new Player(pos);
limitoftheworld = new LimitOfTheWorld(pos2);
rect(worldSize,worldSize,0,0);
}
void draw(){
background(255);
text("mousex: " + mouseX + "mousey: " + mouseY,20,20);
switch(mode){
case 0: // FLAT
break;
case 1: // TRACK
translate(width/2, height/2); // center screen on 0,0
translate(-view.x, -view.y); // recenter screen on view
break;
}
pushMatrix();
noFill();
player.update();
PVector target = new PVector(mouseX,mouseY);
player.pointMouse(target);
view = PVector.lerp(player.position,player.position,0.0);
limitoftheworld.display(); //player.display();
player.display2();
popMatrix();
}
void keyReleased() {
if (key == CODED) {
if (keyCode == CONTROL) {
mode = (mode+1)%2;
}
}
}
class LimitOfTheWorld{
PVector position;
float r;
LimitOfTheWorld(PVector pos2){
position = pos2.get();
r = 450;
}
void display(){
noFill();
rect(position.x,position.y,r,r);
stroke(5);
}
}
class Player{
PVector position;
float r;
PVector velocity;
PVector acceleration;
float maxspeed;
float maxforce;
Player(PVector pos){
position = pos.get();
r = 6;
acceleration = new PVector(0, 0);
velocity = new PVector(0, 0);
velocity.mult(5);
maxspeed = 1;
maxforce = 0.15;
}
void display2() {
float theta = velocity.heading2D() + radians(90);
fill(127);
stroke(0);
pushMatrix();
translate(position.x, position.y);
rotate(theta);
beginShape(TRIANGLES);
vertex(0, -r*2);
vertex(-r, r*2);
vertex(r, r*2);
endShape();
popMatrix();
text("x: " + position.x + "y: " + position.y, position.x+2, position.y);
int dir = round(4 * (velocity.heading2D() + radians(90)) / TWO_PI + 4) % 4;
switch(dir) {
case 0:
text("north",10,10);
break;
case 1:
text("east",10,10);
break;
case 2:
text("south",10,10);
break;
case 3:
text("west",10,10);
break;
}
}
void pointMouse(PVector target){
PVector desired = PVector.sub(target,position);
desired.setMag(maxspeed);
PVector steer = PVector.sub(desired,velocity);
steer.limit(maxforce);
applyForce(steer);
}
void update() {
velocity.add(acceleration);
velocity.limit(maxspeed);
position.add(velocity);
acceleration.mult(0);
}
void applyForce(PVector force) {
acceleration.add(force);
}
}
So, my issue is:
when i don’t use the camera method(when i press control), everything works fine, the coordinates of my mouse are correct and my player follow the mouse. But when i use the camera method, it keep the coordinates based on the screen and of course it doesn’t work.
My question is, how can i manage to have the coordinates of my mouse based on my world not the screen ?