轉自:http://www.cnblogs.com/corgi/p/5405450.html
三、示例代碼
void UParabolicMovementComponent::InitComputeParams() { //無目標時 if (!bHasTarget) { // 指定出射角度時計算方法 // FQuat quat = UpdatedComponent->GetComponentQuat() * FQuat(Rotation); // DirHorz = quat.Rotator().Vector(); // float Theta = DirHorz.Z / DirHorz.Size2D(); // VerticalSpeed = HorzSpeed * tan(Theta); // DirHorz.Z = 0; //指定最大高度時計算方法 DirHorz = UpdatedComponent->GetComponentRotation().Vector(); DirHorz.Z = 0; DirHorz.Normalize(); //若無目標則默認G=980.0f VerticalSpeed = FMath::Sqrt(2 * Gravity * MaxHeight); } //有目標時 else { DirHorz = GetTargetPosition() - GetHostPosition(); float HeightDist = DirHorz.Z; DirHorz.Z = 0; float Dist = DirHorz.Size(); DirHorz.Normalize(); TotalTime = Dist / HorzSpeed; VerticalSpeed = (2.0f * (MaxHeight + FMath::Sqrt(MaxHeight*MaxHeight - MaxHeight*HeightDist))) / TotalTime; Gravity = VerticalSpeed * VerticalSpeed / (2.0f * MaxHeight); //VerticalSpeed = HeightDist / TotalTime + .5f * Gravity * TotalTime; } CurrentTime = 0; StartPos = GetHostPosition(); Velocity = DirHorz * HorzSpeed + FVector::UpVector * VerticalSpeed; UpdateComponentVelocity(); } //計算當前時刻所在位置 void UParabolicMovementComponent::ComputeMovement(float DeltaTime, FVector& OutMoveDelta) { CurrentTime += DeltaTime; if(bHasTarget) { if (CurrentTime >= TotalTime) { CurrentTime = TotalTime; bStop = true; } } float CurrentVertSpeed = VerticalSpeed - Gravity * CurrentTime; float fVertDist = .5f * (VerticalSpeed + CurrentVertSpeed) * CurrentTime; OutMoveDelta = StartPos + CurrentTime * HorzSpeed * DirHorz + fVertDist * FVector::UpVector - GetHostPosition(); Velocity = DirHorz * HorzSpeed + FVector::UpVector * CurrentVertSpeed; //OutNewRotation = Velocity.Rotation().Quaternion(); }
導彈線軌跡計算
物體以某個初速度方向出發后,先保持穩定線性速度,以一定角速度向目標點旋轉,當速度方向和自身-目標方向的夾角小於一定角度后,速度方向立刻改為自身-目標方向,進行直線加速。
void UMissleMovementComponent::InitComputeParams() { Dir = (UpdatedComponent->GetComponentQuat() * Rotation.Quaternion()).Rotator().Vector(); Dir.Normalize(); Velocity = Dir * StartSpeed; CurrentSpeed = StartSpeed; UpdateComponentVelocity(); } void UMissleMovementComponent::ComputeMovement(float DeltaTime, FVector& OutMoveDelta) { float MinDist = GetMinimalDistance(); static const float fLimit = (float)cos(FMath::DegreesToRadians(LimitDegree)); FVector vDir; vDir = GetTargetPosition() - GetHostPosition(); float fLeft = vDir.Size(); vDir.Normalize(); float fDist = CurrentSpeed * DeltaTime; if (fDist >= fLeft) { fDist = fLeft; bStop = true; } float fAngle = Velocity.GetSafeNormal() | vDir; //如果小於最小距離;或者角度相近,使用直線 if (fLeft < MinDist || fAngle > fLimit) { CurrentSpeed += LinearAcc * DeltaTime; OutMoveDelta = vDir * fDist; Velocity = vDir * CurrentSpeed; } else { OutMoveDelta = Velocity * DeltaTime; FVector vUp = Velocity ^ vDir; vUp.Normalize(); FQuat q(vUp, RotationSpeed * DeltaTime); Velocity = q.RotateVector(Velocity); } //OutNewRotation = Velocity.ToOrientationQuat(); //OutNewRotation = Velocity.Rotation().Quaternion(); } float UMissleMovementComponent::GetMinimalDistance() { float d = 2.0f * StartSpeed / RotationSpeed; return d; }