We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Hi guys, i'm trying to implement a first person camera for a little program i'm writing. I actually did implement the camera (it works with differentials and spherical coordinates) and it's doing quite well except for a problem. As i look around the scene (move my mouse), the cursor will eventually fall off the screen and of course stop working. How can i fix that? Here is my code:
void updateCamera(){
this.horizontalAngle += (mouseX - pmouseX)/100f;
this.verticalAngle -= (mouseY - pmouseY)/100f;
if(this.verticalAngle < -PI+0.1)
this.verticalAngle = -PI+0.1;
if(this.verticalAngle > 0)
this.verticalAngle = 0;
centerX = this.pos.x + cos(horizontalAngle) * sin(verticalAngle);
centerY = this.pos.y + sin(horizontalAngle) * sin(verticalAngle);
centerZ = this.pos.z + cos(verticalAngle);
camera(this.pos.x, this.pos.y, this.pos.z, this.centerX, this.centerY, this.centerZ, 0, 0, -1);
}
I tried the robot class with robot.mouseMove();
to set the mouse to center every time i move it, but in this way my camera gets locked. Suggestions? Thank you!
Answers
THere is a library for that QueasyCam
Thank you, i knew there were some libraries but i like to write code by myself, it makes me understand code a lot better (I'm not a programmer, i do this in my free time).
Is there any other way to fix this problem?
Without having an MCVE that allows us to test the behavior you are describing it is hard to tell why it might be happening or give you specific advice.
I'm also not sure what you mean by your camera getting locked. Are you referring to gimbol lock?
Even if you do not want to use QuesyCam, you might still be interested in browsing its source code, which can give you some ideas about the way it approaches these problems.
Here is my problem: http://gph.is/2oU8ifq
I hope this is allowed by the forum, i didn't really know how to show you guys what was happening so i created this gif. It's small and low quality but you can see what's going on with the mouse. For clarity, i'll post the entire code for the player class. I commented it so you can better understand what i did:
Ah, I understand now -- you wanted the mouse drag to be sending events with its location even when the mouse pointer travels outside the bounds of the window.
Were you able to resolve this issue?
I actually did! It took me a while but i made a simple camera class, easy to implement. In the end i just had to use the Robot class method mouseMove and play with the pmouseX and pmouseY variables. I'm kinda proud of it hahaha
If you want to give it a try, here is the code. If you want to test it on any P3D sketch, you have to set the main tab like this:
Here is the Camera class. You move with WASD, SPACEBAR and SHIFT. Mouse movement is not really smooth, but works fine. There you go:
@mothersmilkk -- thanks for sharing your solution with the forum, and congratulations on writing your own simple camera class!
Well done!
;-)
Thank you guys! Hope this will be useful to someone :D