Reputation: 168
I am working on some sort of city builder where you can drag the camera with your mouse. It all works fine accept when you turn the camera.
Mouse drag script:
private float angleArroundTarget = 0; // rotation arround the target \\
// draggable camera \\
if(Mouse.isButtonDown(0)){
targetPosition.x += delta * (Mouse.getDX() * 2);
targetPosition.z -= delta * (Mouse.getDY() * 2);
}
If you know a calculation where it does not depend on the angleArroundTarget when you drag the camera arround please let me know.
Thanks in advance
Upvotes: 1
Views: 60
Reputation: 96316
I assume you need something like:
if(Mouse.isButtonDown(0)){
float dx = delta * (Mouse.getDX() * 2);
float dy = -delta * (Mouse.getDY() * 2);
float c = Math.cos(angleArroundTarget);
float s = Math.sin(angleArroundTarget);
targetPosition.x += c * dx - s * dy;
targetPosition.z += s * dx + c * dy;
}
This code rotates ( delta * (Mouse.getDX() * 2) , -delta * (Mouse.getDY() * 2))
vector by angleArroundTarget
angle.
Depending on how your camera is set up, the exact code above might not work. If it happends, try negating angleArroundTarget
and/or dy
.
Upvotes: 2