# Needing help for rotating movement directions with mouseMoved() in 3D

I am working with threedimensional spaces lately. This is my Code:
//The Main Programm:

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

//The Figur class tab:

float a1=0;
float b1=0;
float c1=0;
float mousey=0;
float cam=0;
class Figur
{
float xPos;
float yPos;
float zPos;
Figur(float x,float y,float z)
{
xPos=x;
yPos=y;
zPos=z;
}
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;}
}
void releasedkeys()
{
if(key==‘w’)
{c1=0;}
if(key==‘s’)
{c1=0;}
if(key==‘c’)
{cam=0;}
}
void movedmouse()
{
if(mouseX<650)
{mousey=-(-mouseX+650)*0.01;}
if(mouseX>650)
{mousey=(mouseX-650)*0.01;}
}
void moveFigur()
{
if(c1>0)
{
zPos=zPos+5;
}
if(c1<0)
{
zPos=zPos-5;
}
}
void kamera()
{
camera(xPos,yPos,zPos+200,xPos,yPos,zPos,0,1,1);
if(cam>0)
{
camera(xPos,yPos,zPos+10,xPos,yPos,zPos,0,1,1);
}
}
}
My Problem is, that I want the movement directions to rotate, too.
As far only the Figur rotates, but when I move her, the figur does not change her directions of movement. I tried it with PVectors, but I am not very good with them, because I never used them before. Can someone please explain me how I can fullfill my goal? Thank you.

Interesting project

maybe you can start here, it’s with cos and sin

``````

//The Main Programm:

Figur figur1;

void setup()
{
size(1300, 750, P3D);
figur1=new Figur(650, 375, 0);
}
void draw()
{
// println(mousey);
background(#FFFFFF);
lights();

//floor code:

float yFloor=375+50+30;

for (int f=(0); f<300; f++) // floor
{
stroke(255, f*1.0+0, 66);
//stroke(0);
line(f*11, yFloor, -330,
f*11+11, yFloor, 330 );
line(0, yFloor, -330+f*11,
width, yFloor, 330 +f*11+11);

line(f*11, yFloor, 330,
f*11, yFloor, 430 );
}

figur1.drawFigur();
figur1.moveFigur();
figur1.kamera();
}

// -----------------------------------------------------------------

void keyPressed()
{
figur1.pressedkeys();
}

void keyReleased()
{
figur1.releasedkeys();
}
void mouseMoved()
{
figur1.movedmouse();
}

// =============================================================================
//The Figur class tab:

float a1=0;
float b1=0;
float c1=0;
float mouseXForScreen=0;
float cam=0;

class Figur
{
float xPos;
float yPos;
float zPos;

//constr
Figur(float x, float y, float z)
{
xPos=x;
yPos=y;
zPos=z;
}//constr

// -----

void drawFigur()
{
pushMatrix();
fill(#0000FF);
translate(xPos, yPos, zPos);
if (mouseX<1300&mouseX>0)
{
rotateY(mouseXForScreen);
}
noStroke();
sphere(20);
popMatrix();
//------------------------
pushMatrix();
translate(xPos, yPos+50, zPos);
if (mouseX<1300&mouseX>0)
{
rotateY(mouseXForScreen);
}
stroke(111);
box(15, 60, 15);
popMatrix();
}

void pressedkeys()
{
if (key=='w')
{
c1=5;
}
if (key=='s')
{
c1=-5;
}
if (key=='c')
{
cam=5;
}
}

void releasedkeys()
{
if (key=='w')
{
c1=0;
}
if (key=='s')
{
c1=0;
}
if (key=='c')
{
cam=0;
}
}

void movedmouse()
{
if (mouseX<650)
{
mouseXForScreen=-(-mouseX+650)*0.01;
}
if (mouseX>650)
{
mouseXForScreen=(mouseX-650)*0.01;
}
}
void moveFigur()
{
if (c1==0)
return;

xPos=cos(mouseXForScreen) * c1 + xPos;
zPos=sin(mouseXForScreen) * c1 + zPos;
/*
if (c1>0)
{
zPos=zPos+5;
}
if (c1<0)
{
zPos=zPos-5;
}
*/
}

// ----------------------------------

void kamera()
{
camera(xPos, yPos, zPos+200,
xPos, yPos, zPos,
0, 1, 1);
if (cam>0)
{
camera(xPos, yPos, zPos+10,
xPos, yPos, zPos,
0, 1, 1);
}
}
}//
//

``````

Thank you very much!
I tried to do something with tan before I tried out the PVectors, but I could not come to this solution. Thank you very much for helping me. You’re a genius!

1 Like

I have another problem: I want the camera to turn around the Figur as well. I tried it in the camera method with beginCamera() then rotateY(mousey) and then endCamera();
void kamera()
{
camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,1);
xCam=xPos;
yCam=yPos;
zCam=zPos+200;
if(mousey>0||mousey<0)
{

``````beginCamera();
rotateY(mousey);
endCamera();

}
``````

Thank you 1 Like