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

**URL:** <https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169>\
**Category:** Coding Questions\
**Created:** [December 12, 2020, 9:15pm UTC](https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169 "2020-12-12T21:15:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 12, 2020, 9:15pm UTC](https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169/1 "2020-12-12T21:15:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [December 12, 2020, 11:00pm UTC](https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169/2 "2020-12-12T23:00:01Z")

</div>

> [@SailorToei](#):
>
> 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

```auto

//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);
    }
  }
}// 
//

```

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 13, 2020, 12:06pm UTC](https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169/3 "2020-12-13T12:06:11Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![SailorToei](https://avatars.discourse-cdn.com/v4/letter/s/898d66/32.png) [@SailorToei](https://discourse.processing.org/u/SailorToei)\
**Post date:** [December 13, 2020, 2:35pm UTC](https://discourse.processing.org/t/needing-help-for-rotating-movement-directions-with-mousemoved-in-3d/26169/4 "2020-12-13T14:35:56Z")

</div>

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 🙂
