OK i have my camera system working exactly how i want it except for 1 problem, I have uploaded test build, press N to spawn a ball, then if you press down and to the left so the ball rolls around the camera will at 1 point spin in the opposite direction back to the ball.
New code:
Code:
//
// sets the look at target of the camera
//
procedure TCameraController.setTarget(const targ: TD3DXMatrix);
const
tween = 0.01;
var
pos, rV: TD3DXVector3;
diff: TD3DXVector3;
begin
pos := D3DXVector3(targ._41, targ._42+5, targ._43); // + 5 look slightly above the object
D3DXVec3Subtract(diff, pos, position);
D3DXVec3Normalize(diff, diff);
rV := D3DXVector3(
arctan2(diff.z, diff.x),
arcsin(diff.y),
0
);
rotation.x := (rV.x * tween) + rotation.x * (1 - tween);
rotation.y := (rV.y * tween) + rotation.y * (1 - tween);
rotation.z := (rV.z * tween) + rotation.z * (1 - tween);
end;
//
// sets the position of the camera somewhere behind the object, either facing or by velocity
//
procedure TCameraController.Follow(m: TD3DXMatrix; heading: TD3DXVector3);
const
Dist = 20;
MaxDist = 50;
DefHeight = 15;
var
tweenSpeed: TD3DXVector3;
absdist: TD3DXVector3;
pos: TD3DXVector3;
gPoint: TD3DXVector3;
begin
pos := D3DXVector3(m._41, m._42, m._43);
if (heading.x > 0.00001) and (heading.z > 0.00001) then begin
D3DXVec3Normalize(heading, heading);
gPoint.x := m._41 + -Dist*heading.x;
gPoint.z := m._43 + -Dist*heading.z;
gPoint.y := m._42 + DefHeight;
end else begin
D3DXVec3Normalize(heading, pos);
gPoint.x := m._41 + -Dist*heading.x;
gPoint.z := m._43 + -Dist*heading.z;
gPoint.y := m._42 + DefHeight;
end;
absdist := D3DXVector3(abs(pos.x-position.x),
abs(pos.y-position.y),
abs(pos.z-position.z)
);
if absdist.x > MaxDist then
tweenSpeed.x := 0.005 * (absdist.x - (MaxDist-1))
else
tweenSpeed.x := 0.005;
tweenSpeed.y := 0.005;
if absdist.z > MaxDist then
tweenSpeed.z := 0.005 * (absdist.z - (MaxDist-1))
else
tweenSpeed.z := 0.005;
position.x := (gPoint.x * tweenSpeed.x) + position.x * (1 - tweenSpeed.x);
position.y := (gPoint.y * tweenSpeed.y) + position.y * (1 - tweenSpeed.y);
position.z := (gPoint.z * tweenSpeed.z) + position.z * (1 - tweenSpeed.z);
end;
else everything is nice, camera moves very smoothly for the most part, just this problem that is a pain...
Any ideas?
Thanks.
Bookmarks