en:
Trying to rotate the object around the point at an arbitrary distance from it.
In 2D everything turns out fine in UE4 is not obtained.
Maybe someone already have experience, please help.
ru:
Пытаюсь поворачивать объект вокруг точки, на произвольном удалении от неё.
В 2D всё получается нормально, в UE4 уже не получается.
Может у кого уже есть наработки, прошу помочь.
void USmartMath::Actor_RotateAtPoint(AActor* Actor, FVector Point, FRotator Angle)
{
if (Actor != nullptr)
{
float newXLocation=0, newYLocation=0, newZLocation=0;
FVector newLocation = FVector();
float dist = 100;//FVector::Distance(Point, Actor->GetActorLocation());
//X
newYLocation = Point.Y - (dist*FMath::Cos(Angle.Roll* (PI/180)));
newZLocation = Point.Z + (dist*FMath::Sin(Angle.Roll* (PI/180)));
newLocation = FVector(newXLocation, newYLocation, newZLocation);
Actor->SetActorLocation(newLocation);
//Y
newXLocation = Point.X + (dist*FMath::Cos(Angle.Pitch*(PI/180)));
newZLocation = Point.Z + (dist*FMath::Sin(Angle.Pitch*(PI/180)));
newLocation = FVector(newXLocation, newYLocation, newZLocation);
Actor->SetActorLocation(newLocation);
//Z
newXLocation = Point.X + (dist*FMath::Cos(Angle.Yaw* (PI/180)));
newYLocation = Point.Y + (dist*FMath::Sin(Angle.Yaw* (PI/180)));
newLocation = FVector(newXLocation, newYLocation, newZLocation);
Actor->SetActorLocation(newLocation);
Actor->SetActorRotation(Angle);
}
}
In WPF, in 2d everything works…
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace RotateAtPoint
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void SetAngle(object sender, RoutedEventArgs e)
{
int Angle = int.Parse(textBox.Text);
int Rad = 25;
Line line = new Line();
line.X1 = CanvasField.Width/2; // начальная координата
line.Y1 = CanvasField.Height/2; //
line.X2 = line.X1+(Rad*Math.Cos(Angle*Math.PI/180)); // конечная координата по углу
line.Y2 = line.Y1-(Rad*Math.Sin(Angle*Math.PI/180)); // Math.PI/180 - перевод градусов в радианы
line.Stroke = System.Windows.Media.Brushes.Chocolate; //
line.StrokeThickness = 2;
line.HorizontalAlignment = HorizontalAlignment.Left;
line.VerticalAlignment = VerticalAlignment.Center;
CanvasField.Children.Add(line);
label.Content = "X2 = " + line.X2 + " | Y2 = " + line.Y2;
}
private void AngleInput(object sender, TextCompositionEventArgs e)
{ e.Handled = ("-1234567890".IndexOf(e.Text) < 0); }
}
}
Druha
(Druha)
July 31, 2017, 12:24pm
3
It is easier to use matrices for this:
void RotateAroundPoint(AActor* Actor, const FVector& Center, const FVector& Offset, const FRotator& Rotator)
{
auto TargetTransform = FTranslationMatrix(Offset) * FRotationMatrix(Rotator) * FTranslationMatrix(Center);
Actor->SetActorTransform(TargetTransform);
}
In this case Offset determines where the Actor would be placed when Rotator is zero. You can use FVector(dist, 0, 0) for it.
I haven’t tested it, so if it doesn’t work try to reverse multiplication order.
Grogger
(Grogger)
July 31, 2017, 5:41pm
4
If I understand correctly, FVector::RotateAngleAxis might do what you’re aiming to do?