C#WPF复合变换问题
同时处理平移和旋转变换时存在问题。 我刚刚开始尝试构建小行星,这是我第一次编写WPF应用程序 我的玩家飞船由一个多边形对象表示,它在按键事件上执行旋转和平移 我的旋转变换按预期工作。 主要问题是,一旦我平移,然后再次尝试旋转,旋转变换将尝试围绕上一点旋转,即使其指定用于获取新的centerX和centerY坐标 平移变换似乎将多边形大致移动+50x和+50y(即使变换中使用的坐标证明并非如此),然后它将正确地沿旋转指定的方向移动 提前感谢任何人能给我的帮助 这是我的C:C#WPF复合变换问题,c#,wpf,rotation,transform,composite,C#,Wpf,Rotation,Transform,Composite,同时处理平移和旋转变换时存在问题。 我刚刚开始尝试构建小行星,这是我第一次编写WPF应用程序 我的玩家飞船由一个多边形对象表示,它在按键事件上执行旋转和平移 我的旋转变换按预期工作。 主要问题是,一旦我平移,然后再次尝试旋转,旋转变换将尝试围绕上一点旋转,即使其指定用于获取新的centerX和centerY坐标 平移变换似乎将多边形大致移动+50x和+50y(即使变换中使用的坐标证明并非如此),然后它将正确地沿旋转指定的方向移动 提前感谢任何人能给我的帮助 这是我的C: public void
public void handleRotate(KeyEventArgs e)
{
if (e.Key == Key.Right)
{
this.rotate(10);
}
if (e.Key == Key.Left)
{
this.rotate(-10);
}
}
public void handleUpDown(KeyEventArgs e)
{
if (e.Key == Key.Up)
{
this.move();
}
}
public double convertToRadians(double angle)
{
return (Math.PI / 180) * (angle - 90);
}
public void move()
{
double radians = convertToRadians(playerShip.getHeading());
double xMovement = Math.Cos(radians) * 10;
double yMovement = Math.Sin(radians) * 10;
playerShip.setShipCenterX(xMovement += playerShip.getShipCenterX());
playerShip.setShipCenterY(yMovement += playerShip.getShipCenterY());
MessageBox.Show("Heading: " + playerShip.getHeading().ToString() + " centerx: " + playerShip.getShipCenterX().ToString() + " centery: " + playerShip.getShipCenterY().ToString());
TranslateTransform translate = new TranslateTransform(xMovement, yMovement);
theShipShape.RenderTransform = translate;
}
public void rotate(double rotation)
{
double newHeading = playerShip.getHeading() + rotation;
RotateTransform rotate = new RotateTransform(newHeading, playerShip.getShipCenterX(), playerShip.getShipCenterY());
MessageBox.Show("Heading: " + newHeading.ToString() + " centerx: " + playerShip.getShipCenterX().ToString() + " centery: " + playerShip.getShipCenterY().ToString());
theShipShape.RenderTransform = rotate;
playerShip.setHeading(newHeading);
}
<Window x:Name="GameWindow" x:Class="AsteroidsAttempt2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="GameCanvas" Focusable="True" IsEnabled="True" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517" KeyDown="GameCanvas_KeyDown"/>
</Window>
}
这是我的xaml:
public void handleRotate(KeyEventArgs e)
{
if (e.Key == Key.Right)
{
this.rotate(10);
}
if (e.Key == Key.Left)
{
this.rotate(-10);
}
}
public void handleUpDown(KeyEventArgs e)
{
if (e.Key == Key.Up)
{
this.move();
}
}
public double convertToRadians(double angle)
{
return (Math.PI / 180) * (angle - 90);
}
public void move()
{
double radians = convertToRadians(playerShip.getHeading());
double xMovement = Math.Cos(radians) * 10;
double yMovement = Math.Sin(radians) * 10;
playerShip.setShipCenterX(xMovement += playerShip.getShipCenterX());
playerShip.setShipCenterY(yMovement += playerShip.getShipCenterY());
MessageBox.Show("Heading: " + playerShip.getHeading().ToString() + " centerx: " + playerShip.getShipCenterX().ToString() + " centery: " + playerShip.getShipCenterY().ToString());
TranslateTransform translate = new TranslateTransform(xMovement, yMovement);
theShipShape.RenderTransform = translate;
}
public void rotate(double rotation)
{
double newHeading = playerShip.getHeading() + rotation;
RotateTransform rotate = new RotateTransform(newHeading, playerShip.getShipCenterX(), playerShip.getShipCenterY());
MessageBox.Show("Heading: " + newHeading.ToString() + " centerx: " + playerShip.getShipCenterX().ToString() + " centery: " + playerShip.getShipCenterY().ToString());
theShipShape.RenderTransform = rotate;
playerShip.setHeading(newHeading);
}
<Window x:Name="GameWindow" x:Class="AsteroidsAttempt2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Canvas x:Name="GameCanvas" Focusable="True" IsEnabled="True" HorizontalAlignment="Left" Height="320" VerticalAlignment="Top" Width="517" KeyDown="GameCanvas_KeyDown"/>
</Window>
渲染变换在渲染时应用,不会改变对象本身的状态。如果要应用consequentive翻译,您需要的是一个TransformGroup。其中一个有一个子属性,您可以按照希望它们执行的顺序向其添加转换,这样您就可以进行翻译,然后进行rorate,然后再次翻译,等等 当然,如果他们中的许多人在儿童时期积累起来,那么计算结果可能需要一些时间。在这种情况下,您可能希望定期告诉TransformGroup给您一个TransformMatrix(如果它没有在网络上搜索某个实现的方法),然后您可以将其用作TransformGroup的唯一子级(删除以前的子级)。然后再开始添加更多的子项,如TranslateTransform等。然后,如果达到10个变换,则再次删除它们,并为它们添加等效的MatrixTransform,作为TransformGroup的第一个子项,依此类推