Problem with robot cursor

Greetings everyone! i was making a program that moves the camera and lets you ovserve an object from any direction (using w, a, s, d and the mouse, kinda like a game), to do that i use the robot class to restart the position of the mouse so the borders of the window don’t act as a limitant factor, the problem is that it doesn’t respond as i was expecting (it reset the position of the camera). Here the code:

import java.awt.*;
import java.awt.event.*;

Robot robot;

float x, y, z;
int xp = 0, yp = 0, zp = 0;

void setup()
{
  size(200,200,P3D);
  surface.setLocation((displayWidth / 2) - (width / 2), (displayHeight / 2) - (height / 2));
  try
  { 
    robot = new Robot();
    robot.setAutoDelay(0);
  } 
  catch(Exception e)
  {
    println(e);
  }
  robot.mouseMove(displayWidth / 2, displayHeight / 2);
  x = 100;
  y = 100;
  z = 0;
}

void draw()
{
  robot.mouseMove(displayWidth / 2, displayHeight / 2);
  background(180);
  fill(255);
  translate(100, 100, 0);
  box(40, 40, 40);
  fill(0);
  line(0, 0, 0, 50, 0, 0);
  text("X", 50, 0, 0);
  line(0, 0, 0, 0, 50, 0);
  text("Y", 0, 50, 0);
  line(0, 0, 0, 0, 0, 50);
  text("Z", 0, 0, 50);
  camera(xp, yp, zp, x, y, z, -1, -1, -1);
  if(keyPressed)
  {
    if(key == 'w')
    {
      xp = (xp>200)?200:(xp+1);
      x++;
    }
    if(key == 's')
    {
      xp = (xp<0)?0:(xp-1);
      x--;
    }
    if(key == 'd')
    {
      yp = (yp>200)?200:(yp+1);
      y++;
    }
    if(key == 'a')
    {
      yp = (yp<0)?0:(yp-1);
      y--;
    }
  }
  if(mouseY > 98)
  {
    z ++;//+= mouseY - pmouseY;
    z = z>100?100:z;
  }
  else if(mouseY < 96)
  {
    z --;//+= mouseY - pmouseY;
    z = z<0?0:z;
  }
  if(mouseX > pmouseX)
  {
    
  }
  println(mouseX + ":" + pmouseX);
}

What i’m i doing wrong?

i’ve reached the problem, i was using the middle position of the mouseX (97) instead of the middle position of the mouseY, those values are for my screen, so it wont work in to many displays, i’ll implement an more responsive coding. Thanks if you stopped to watch the post!

The correct code is the following:

import java.awt.*;
import java.awt.event.*;

Robot robot;

float x, y, z;
int xp = 0, yp = 0, zp = 0, mouseYmiddle = 0, mouseXmiddle = 0;
boolean flag = true;

void setup()
{
  size(200,200,P3D);
  surface.setLocation((displayWidth / 2) - (width / 2), (displayHeight / 2) - (height / 2));
  try
  { 
    robot = new Robot();
    robot.setAutoDelay(0);
  }
  catch(Exception e)
  {
    println(e);
  }
  x = 100;
  y = 100;
  z = 0;
  noCursor();
}

void draw()
{
  robot.mouseMove(displayWidth / 2, displayHeight / 2);
  background(180);
  fill(255);
  translate(100, 100, 0);
  box(40, 40, 40);
  fill(0);
  line(0, 0, 0, 50, 0, 0);
  text("X", 50, 0, 0);
  line(0, 0, 0, 0, 50, 0);
  text("Y", 0, 50, 0);
  line(0, 0, 0, 0, 0, 50);
  text("Z", 0, 0, 50);
  camera(xp, yp, zp, x, y, z, -1, -1, -1);
  if(keyPressed)
  {
    if(key == 'w')
    {
      xp = (xp>200)?200:(xp+1);
      x++;
    }
    if(key == 's')
    {
      xp = (xp<0)?0:(xp-1);
      x--;
    }
    if(key == 'd')
    {
      yp = (yp>200)?200:(yp+1);
      y++;
    }
    if(key == 'a')
    {
      yp = (yp<0)?0:(yp-1);
      y--;
    }
  }
  if(mouseY > mouseYmiddle + 1)
  {
    z += mouseY - mouseYmiddle;
    z = z>100?100:z;
  }
  else if(mouseY < mouseYmiddle - 1)
  {
    z += mouseY - mouseYmiddle;
    z = z<0?0:z;
  }
  if(mouseX > pmouseX)
  {
    
  }
}

void mouseMoved()
{
  if(flag)
  {
    mouseYmiddle = mouseY;
    mouseXmiddle = mouseX;
    flag = false;
  }
}
2 Likes

@erwrow Not only did i watch the post, i felt like i needed to run your program, which is pretty cool! It reminds me of Blender Feel free to send us an update if you wish:-)

2 Likes

Neat idea! As this is using the JOGL renderer you might want to consider using GLWindow over Robot though, which should handle things relatively.

https://jogamp.org/deployment/jogamp-next/javadoc/jogl/javadoc/com/jogamp/newt/opengl/GLWindow.html#warpPointer(int,%20int)

1 Like

@dan850 Wow, thanks a lot for that comment, i’m very glad to read that you found interesting, i was learning new stuff and this came up, of course i’ll send updates if there is this kind of feedback!

@neilcsmith Oh, that’s great, realy helped a lot, now i can get rid of the annoying parts of the code that handled the coordinates!! Thanks a lot!

The new code is this:

import com.jogamp.newt.opengl.GLWindow;

float x, y, z;
int xp = 0, yp = 0, zp = 0;

void setup()
{
  size(200,200,P3D);
  surface.setLocation((displayWidth / 2) - (width / 2), (displayHeight / 2) - (height / 2));
  
  x = 100;
  y = 100;
  z = 0;
  noCursor();
}

void draw()
{
  ((GLWindow)getSurface().getNative()).warpPointer(width / 2, height / 2);
  background(180);
  fill(255);
  translate(100, 100, 0);
  box(40, 40, 40);
  fill(0);
  line(0, 0, 0, 50, 0, 0);
  text("X", 50, 0, 0);
  line(0, 0, 0, 0, 50, 0);
  text("Y", 0, 50, 0);
  line(0, 0, 0, 0, 0, 50);
  text("Z", 0, 0, 50);
  camera(xp, yp, zp, x, y, z, -1, -1, -1);
  if(keyPressed)
  {
    if(key == 'w')
    {
      xp = (xp>200)?200:(xp+1);
      x++;
    }
    if(key == 's')
    {
      xp = (xp<0)?0:(xp-1);
      x--;
    }
    if(key == 'd')
    {
      yp = (yp>200)?200:(yp+1);
      y++;
    }
    if(key == 'a')
    {
      yp = (yp<0)?0:(yp-1);
      y--;
    }
  }
  if(mouseY > (height / 2) + 1)
  {
    z += mouseY - height / 2;
    z = z>100?100:z;
  }
  else if(mouseY < (height / 2) - 1)
  {
    z += mouseY - height / 2;
    z = z<0?0:z;
  }
  if(mouseX > pmouseX)
  {
    
  }
}

Anyway, I’ll keep the part of where the window is located, because I found it a bit annoying that it appears where it wants.
Now i’m working on the other axis of the mouse, as soon as I’ve an update i’ll post it.

Again, thanks for the feedback! I’m very appreciative.

@dan850 as you requested, here i post the new updates on this code!

import com.jogamp.newt.opengl.GLWindow;

float x, y, z, xp = 0, yp = 0, zp = 0, anguloXY = 45, anguloZ = 180;

boolean Movements[] = new boolean[5], flag = true;

void setup()
{
  size(200,200,P3D);
  //fullScreen(P3D);
  
  surface.setLocation((displayWidth / 2) - (width / 2), (displayHeight / 2) - (height / 2));
  
  x = 100;
  y = 100;
  z = 0;
  noCursor();
}

void draw()
{
  ((GLWindow)getSurface().getNative()).warpPointer(width / 2, height / 2);
  background(180);
  lights();
  noFill();
  //translate(width / 2, height / 2, 0);
  //box(200, 200, 200);
  displayBox(100, 100, -80, 200);
  fill(255);
  displayBox((width / 2) - 35, (height / 2) - 35, 0, 40);
  displayBox((width / 2) + 35, (height / 2) + 35, 0, 40);
  //box(40, 40, 40);
  fill(0);
  line(0, 0, 0, 50, 0, 0);
  text("X", 50, 0, 0);
  line(0, 0, 0, 0, 50, 0);
  text("Y", 0, 50, 0);
  line(0, 0, 0, 0, 0, 50);
  text("Z", 0, 0, 50);
  println("anguloXY: " + anguloXY + " anguloZ: " + anguloZ + "  |  x: " + x + " y: " + y + " z: " + z);
  camera(xp, yp, zp, x, y, z, 0, 0, 1);
  
  if(Movements[0])    //forward (w)
  {
    xp += sin(map(anguloXY, 0, 360, 0, TWO_PI));
    yp += cos(map(anguloXY, 0, 360, 0, TWO_PI));
    x += sin(map(anguloXY, 0, 360, 0, TWO_PI));
    y += cos(map(anguloXY, 0, 360, 0, TWO_PI));
  }
  if(Movements[1])    //backwards (s)
  {
    xp -= sin(map(anguloXY, 0, 360, 0, TWO_PI));
    yp -= cos(map(anguloXY, 0, 360, 0, TWO_PI));
    x += sin(map(anguloXY, 0, 360, 0, TWO_PI));
    y += cos(map(anguloXY, 0, 360, 0, TWO_PI));
  }
  if(Movements[2])    //left (a)
  {
    xp += sin(map(anguloXY + 90, 0, 360, 0, TWO_PI));
    yp += cos(map(anguloXY + 90, 0, 360, 0, TWO_PI));
    x += sin(map(anguloXY + 90, 0, 360, 0, TWO_PI));
    y += cos(map(anguloXY + 90, 0, 360, 0, TWO_PI));
  }
  if(Movements[3])    //right (d)
  {
    xp += sin(map(anguloXY - 90, 0, 360, 0, TWO_PI));
    yp += cos(map(anguloXY - 90, 0, 360, 0, TWO_PI));
    x += sin(map(anguloXY - 90, 0, 360, 0, TWO_PI));
    y += cos(map(anguloXY - 90, 0, 360, 0, TWO_PI));
  }
  
  if(Movements[4])
  {
    if(flag)
    {
      zp += 20;
      z += 20;
      flag = false;
    }
  }
  else
  {
    if(flag)
    {
      zp -= 20;
      z -= 20;
      flag = false;
    }
  }
  
  if(mouseY > (height / 2) + 1)
  {
    //z += mouseY - (height / 2);
    //z = z>100?100:z;
    anguloZ += mouseY - (height / 2);
    anguloZ = anguloZ>90?90:anguloZ;
  }
  else if(mouseY < (height / 2) - 1)
  {
    //z += mouseY - (height / 2);
    //z = z<-100?-100:z;
    anguloZ += mouseY - (height / 2);
    anguloZ = anguloZ<-90?-90:anguloZ;
  }
  if(mouseX > (width / 2) + 1)
  {
    anguloXY += mouseX - (width / 2);
    anguloXY = anguloXY>360?0:anguloXY;
  }
  else if(mouseX < (width / 2) - 1)
  {
    anguloXY += mouseX - (width / 2);
    anguloXY = anguloXY<0?360:anguloXY;
  }
  
  xp = constrain(xp, -((width * 2) + 1), ((width * 2) - 1));
  yp = constrain(yp, -((width * 2) + 1), (width * 2) - 1);
  x = (width / 2) + sin(map(anguloXY, 0, 360, 0, TWO_PI)) * (width / 2);
  x = constrain(x, -(width * 2), width * 2);
  y = (width / 2) + cos(map(anguloXY, 0, 360, 0, TWO_PI)) * (width / 2);
  y = constrain(y, -(width * 2), width * 2);
  z = -100 + sin(map(anguloZ, -90, 360, -PI/2, TWO_PI)) * 300;
  z = constrain(z, -100, 200);
  
  //text();

}

void keyPressed()
{
  if(key == 'w')
  {
    Movements[0] = true;
  }
  if(key == 's')
  {
    Movements[1] = true;
  }
  if(key == 'd')
  {
    Movements[2] = true;
  }
  if(key == 'a')
  {
    Movements[3] = true;
  }
  if(key == ' ')
  {
    Movements[4] = true;
    flag = true;
  }
}

void keyReleased()
{
  if(key == 'w')
  {
    Movements[0] = false;
  }
  if(key == 's')
  {
    Movements[1] = false;
  }
  if(key == 'd')
  {
    Movements[2] = false;
  }
  if(key == 'a')
  {
    Movements[3] = false;
  }
  if(key == ' ')
  {
    Movements[4] = false;
    flag = true;
  }
}

void displayBox(float x, float y, float z, float r)
{
  pushMatrix();
  translate(x, y, z);
  box(r);
  popMatrix();
}

i’m open to any suggestions!

3 Likes

@erwrow It’s even better in terms of CPU efficiency than your last one

One idea to add to it, maybe change the color of the faces of the cube? for example, in Daniel Shiffman’s book, he has a 2X2 grid changing color based on the direction of the mouse - http://learningprocessing.com/examples/chp05/example-05-02-conditionals-quadrants#

See if you can make your cube do something similar. Good work!

1 Like

Did you work further on this?

What I’d like to see improved is that when you turn around more than 180° (screen border).

At the moment it seems to jump back, but I think it would be better (more like in a First Person Shooter) when you could look around endlessly. So when you look right more than 180° or 360° you just rotate around yourself.

Could you modify your code in this way?

Thank you very much!

Regards, Chrisir

Chrisir, i didn’t work any further on this. It’s been a while since that code and that’s how far it got. If you want share your workaround to this post!

Erwrow!

1 Like

Thank you very much!