Thanks for your replies!
I tried something with code I found on internet. In the example it worked, but applied to my application it doesn’t.
The problem with translate for me is that I want the latest rectangle to be drawn in center, but the previous drawn rectangles should be visible as a trail. The locations of the rectangles are based on GPS coordinates.
import processing.serial.*;
String val; // Data received from the serial port
Serial serialConnectionObject;
int serialChannelIndexInt;
float lat;
float lng;
int rectSizeX = 40 ;
int rectSizeY = 40;
float latcoord = 0;
float longcoord = 0;
boolean newData = false;
float xCoord;
float yCoord;
float oldxCoord;
float oldyCoord;
Camera worldCamera;
void setup()
{
background(0);
size(400,400, P3D);
InitSerialConnectionVoid(0);
String portName = Serial.list()[serialChannelIndexInt]; //change the 0 to a 1 or 2 etc. to match your port
println(portName);
serialConnectionObject.bufferUntil('\n');
worldCamera = new Camera();
}
void draw()
{
translate(-worldCamera.pos.x, -worldCamera.pos.y);
worldCamera.draw();
if(newData) {
fill(#FFFFFF, 10);
noStroke();
rectMode(CENTER);
scale(2);
rect(xCoord, yCoord, rectSizeX, rectSizeY);
}
}
class Camera {
PVector pos; //Camera's position
//The Camera should sit in the top left of the window
Camera() {
pos = new PVector(0, 0);
//You should play with the program and code to see how the staring position can be changed
}
void draw() {
float px = xCoord - oldxCoord;
float py = yCoord-oldyCoord;
//I used the mouse to move the camera
//The mouse's position is always relative to the screen and not the camera's position
//E.g. if the mouse is at 1000,1000 then the mouse's position does not add 1000,1000 to keep up with the camera
//if (mouseX < 100) pos.x-=5;
//else if (mouseX > width - 100) pos.x+=5;
// if (mouseY < 100) pos.y-=5;
//else if (mouseY > height - 100) pos.y+=5;
//I noticed on the web the program struggles to find the mouse so I made it key pressed
if (keyPressed) {
if (key == 'w') pos.y -= py;
if (key == 's') pos.y += py;
if (key == 'a') pos.x -= px;
if (key == 'd') pos.x += px;
}
}
}
void serialEvent(Serial p) {
oldxCoord = xCoord;
oldyCoord = yCoord;
try {
println("test");
val = p.readString();
println(val);
JSONObject json = parseJSONObject(val);
if (json == null) {
newData = false;
} else {
println(json.toString());
lat = json.getFloat("lat");
lng = json.getFloat("lng");
println(lat);
LatLngtoXY(lat,lng);
println(xCoord, yCoord);
newData=true;
}
//latcoord = map(lat, lat-0.00045000045, lat+0.00045000045, 0, width);
//longcoord = map(lng, lng-0.00045000063, lng+0.00045000063, 0, height);
}
catch(RuntimeException e) {
e.printStackTrace();
}
}
void LatLngtoXY(float lat, float lon) {
float mapWidth = width;
float mapHeight = height;
// get x value
xCoord = (lon+180)*(mapWidth/360);
// convert from degrees to radians
float latRad = lat*PI/180;
// get y value
float mercN = log(tan((PI/4)+(latRad/2)));
yCoord = (mapHeight/2)-(mapWidth*mercN/(2*PI));
}
void InitSerialConnectionVoid(int _serialChannelIndexInt){
serialChannelIndexInt = _serialChannelIndexInt;
if(serialChannelIndexInt == Serial.list().length){ return; }
try{
serialConnectionObject = new Serial(this, Serial.list()[serialChannelIndexInt], 115200);
serialConnectionObject.bufferUntil('\n');
}
catch (RuntimeException e){
serialConnectionObject = null;
println(e);
println("SERIAL CONNECTION FAILED");
InitSerialConnectionVoid(serialChannelIndexInt + 1);
}
}