经过和技术支持讨论和摸索,正确实现方法如下。
假设A点位this.CameraPoint,B点为this.TargetPoint。
代码如下。
this._SceneControl.Scene.FirstPersonCamera = this.GetFirstPersonCamera();
public Camera GetFirstPersonCamera()
{
Camera myCamere = new Camera();
myCamere.Longitude = this.CameraPoint.X;
myCamere.Latitude = this.CameraPoint.Y;
myCamere.Altitude = this.CameraPoint.Z;
myCamere.AltitudeMode = AltitudeMode.Absolute;
//计算Heading
double myAngleBaseX = this.CalAngleBaseX(this.CameraPoint.X, this.CameraPoint.Y, this.TargetPoint.X, this.TargetPoint.Y);
myAngleBaseX = myAngleBaseX * 180 / Math.PI;
double myHeading = 360 - myAngleBaseX + 90;
if (myHeading >= 360)
{
myHeading = myHeading - 360;
}
myCamere.Heading = myHeading;
//计算Title
double myTilt = 0;
double myHLength = Math.Sqrt(Math.Pow(this.CameraPoint.X - this.TargetPoint.X, 2) + Math.Pow(this.CameraPoint.Y - this.TargetPoint.Y, 2));
double myZ = Math.Abs(this.CameraPoint.Z - this.TargetPoint.Z);
if (myHLength == 0 && this.CameraPoint.Z > this.TargetPoint.Z)
{
myTilt = 0;
}
else if (myHLength == 0 && this.CameraPoint.Z < this.TargetPoint.Z)
{
myTilt = 180;
}
else if (this.CameraPoint.Z == this.TargetPoint.Z)
{
myTilt = 90;
}
else
{
double myAngle = Math.Atan(Math.Sqrt(myZ / myHLength)) * 180 / Math.PI;
if (this.CameraPoint.Z > this.TargetPoint.Z)
{
myTilt = 90 - myAngle;
}
else
{
myTilt = 90 + myAngle;
}
}
myCamere.Tilt = myTilt;
return myCamere;
}
//计算基于X轴 逆时针的角度
private double CalAngleBaseX(double pX1, double pY1, double pX2, double pY2)
{
double angle = 0;
if (pX1 != pX2)
{
angle = (float)Math.Atan((pY1 - pY2) / (pX1 - pX2));
if (pX1 > pX2)
{
angle = angle + Math.PI;
}
}
else
{
if (pY1 < pY2)
{
angle = (90.0 / 180.0 * Math.PI);
}
else if (pY1 > pY2)
{
angle = (-90.0 / 180.0 * Math.PI);
}
}
return (float)angle;
}