Hello, I already asked a question about turning the movement direction withmovingthe mouse in3D.
It was successfully answered.
However, now I want to turn the camera as well.
<
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();
camera(xCam,yCam,zCam,xPos,yPos,zPos,0,1,1);
rotateY(mousey);
endCamera();
}
if(cam>0)
{
xCam=xPos;
yCam=yPos;
zCam=zPos+10;
}
}
As you can see, I tried it with beginCamera(). “mousey” is a variable that is programmed in that way:
<
if(mouseX<650)
{mousey=-(-mouseX+650)*0.01;}
if(mouseX>650)
{mousey=(mouseX-650)*0.01;}
Because of your help, I could already change the movement directions with mousey. Now I also want to change the camera, but it’s always moving wrong, it is moving, but it’s moving like the Y axis is a little bit moved away from the figur.
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!!!
1 Like