Needing help to rotate the camera around an object by moving the mouse away from the middle of the screen

please format code with </> button * homework policy * asking questions

Hello everybody, I want to create a game, where you can walk sideways straight forward etc, with the camera always 200 pixels behind the “Figur” which resembles the player’s character. When you move the mouse away from the middle of the screen, the Figur turns and moves, if the key is pressed, into the direction, but the direction is influenced by the position of the mouse. that means for example that the Figur moves half to the rightand half straight forward. I managed the turning of the Figur by myself, but the movement directions weren’t influenced. Luckily Chrisir helped me to fix that. However the camera is still going crazy. The camera should move around the Figur when itis turning in that way, that one can always only see the back of the Figur. Can you help me to fix it?
I think you only need mousey, a variable I created,which counts the deviation of mouseX frothe middle of the screen, to make a connection to my work, but I will put more code in this question.
Here’s the code of the main programm:

Figur figur1;
void setup()
{
size(1300,750,P3D);
figur1=new Figur(650,375,0,0,0,0);
}
void draw()
{
//println(mousey);
background(#FFFFFF);
lights();
figur1.drawFigur();
figur1.moveFigur();
figur1.kamera();
}
void keyPressed()
{
figur1.pressedkeys();
}
void keyReleased()
{
figur1.releasedkeys();
}
void mouseMoved()
{
figur1.movedmouse();
}
<
this is the class programm of “Figur” where you can find mousey.

float a1=0;
float b1=0;
float c1=0;
float mousey=0;
float cam=0;
class Figur
{
float xPos;
float yPos;
float zPos;
float xCam;
float yCam;
float zCam;
Figur(float x,float y,float z,float v,float b,float n)
{
xPos=x;
yPos=y;
zPos=z;
xCam=v;
yCam=b;
zCam=n;
}
void drawFigur()
{
pushMatrix();
fill(#0000FF);
translate(xPos,yPos,zPos);
if(mouseX<1300&mouseX>0)
{
rotateY(mousey);
}
sphere(20);
popMatrix();
pushMatrix();
translate(xPos,yPos+50,zPos);
if(mouseX<1300&mouseX>0)
{rotateY(mousey);}
box(15,60,15);
popMatrix();
}
void pressedkeys()
{
if(key==‘w’)
{c1=5;}
if(key==‘s’)
{c1=-5;}
if(key==‘c’)
{cam=5;}
if(key==‘a’)
{b1=-5;}
if(key==‘d’)
{b1=5;}
}
void releasedkeys()
{
if(key==‘w’)
{c1=0;}
if(key==‘s’)
{c1=0;}
if(key==‘c’)
{cam=0;}
if(key==‘a’)
{b1=0;}
if(key==‘d’)
{b1=0;}
}
void movedmouse()
{
if(mouseX<650)
{mousey=-(-mouseX+650)*0.01;}
if(mouseX>650)
{mousey=(mouseX-650)*0.01;}
}
void moveFigur()
{
if(c1>0)
{
xPos=cos(mousey)*c1+xPos;
zPos=sin(mousey)*c1+zPos;
}
if(c1<0)
{
xPos=cos(mousey)*c1+xPos;
zPos=sin(mousey)*c1+zPos;
}
if(b1>0)
{
xPos=sin(mousey)*b1+xPos;
zPos=cos(mousey)*b1+zPos;
}
if(b1<0)
{
xPos=sin(mousey)*b1+xPos;
zPos=cos(mousey)*b1+zPos;
}
}
void kamera()
{

camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);
//if(mousey<1300&mousey>0)
//{
//camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);

//yCam=375;


//if(mousey>0||mousey<0)
//{
//beginCamera();
//camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,1);
//rotateY(mousey);
//endCamera();
//}

}
}
<
I really need your help. I am at the border of desperation. Please help me.

Solved it!
This is the code:
The main programm ( you can find the camera instructions in the class programm, which comes after this):

Figur figur1;
void setup()
{
  size(1300,750,P3D);
  figur1=new Figur(650,375,0,0,0,0);
}
void draw()
{
  //println(mousey);
  background(#FFFFFF);
  lights();
}
void keyPressed()
{
  figur1.pressedkeys();
}
void keyReleased()
{
  figur1.releasedkeys();
}
void mouseMoved()
{
  figur1.movedmouse();
}

Here’s the figurClass:

float a1=0;
float b1=0;
float c1=0;
float mousey=0;
float cam=0;
float camdist=200;
class Figur
{
  float xPos;
  float yPos;
  float zPos;
  float xCam;
  float yCam;
  float zCam;
  Figur(float x,float y,float z,float v,float b,float n)
  {
    xPos=x;
    yPos=y;
    zPos=z;
    xCam=v;
    yCam=b;
    zCam=n;
  }
  void drawFigur()
  {
    pushMatrix();
    fill(#0000FF);
    translate(xPos,yPos,zPos);
    rotateY(mousey);
    sphere(20);
    popMatrix();
    pushMatrix();
    translate(xPos,yPos+50,zPos);
    rotateY(mousey);
    box(15,60,15);
    popMatrix();
  }
  void pressedkeys()
  {
    if(key=='w')
    {c1=-5;}
    if(key=='s')
    {c1=5;}
    if(key=='c')
    {cam=5;}
    if(key=='a')
    {b1=-5;}
    if(key=='d')
    {b1=5;}
  }
  void releasedkeys()
  {
    if(key=='w')
    {c1=0;}
    if(key=='s')
    {c1=0;}
    if(key=='c')
    {cam=0;}
    if(key=='a')
    {b1=0;}
    if(key=='d')
    {b1=0;}
  }
  void movedmouse()
  {
    if(mouseX<650)
    {mousey=-(-mouseX+650)*0.01;}
    if(mouseX>650)
    {mousey=(mouseX-650)*0.01;}
   
  }
  void moveFigur()
  {
    if(c1>0)
    { 
      xPos=sin(mousey)*c1+xPos;
      zPos=cos(mousey)*c1+zPos;
    }
    if(c1<0)
    { 
      xPos=sin(mousey)*c1+xPos;
      zPos=cos(mousey)*c1+zPos;
    }
    if(b1>0)
    {
      xPos=cos(mousey)*b1+xPos;
      zPos=sin(mousey)*b1+zPos;
    }
    if(b1<0)
    {
      xPos=cos(mousey)*b1+xPos;
      zPos=sin(mousey)*b1+zPos;
    }
  }
  void kamera()
  {
    translate(xPos,zPos);
    xCam=(cos(mousey)*camdist)+xPos;
    yCam=yPos;
    zCam=(sin(mousey)*camdist)+zPos;
    camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,0);
  if(cam>0)
    {
     camdist=10;
    }
    if(cam==0)
    {
      camdist=200;
    }
  }
}

I’m so happy! Thank you Chrisir! It was your coding solution that brought me to this idea! Thank you all so much. The movement is a little bit twisted at the moment, but the camera works how it should. That was my goal. Thank you all so much!!! :star_struck:

2 Likes