C# 如何删除wpf中用于模拟的SphereVisual3D对象
我试着用物理学来模拟一个被扔掉的球。 移动部分已经在我的计算中起作用了,但我似乎无法移除上一个球,这个球是一个SphereVisual3D对象 这就是高尔夫球的制作方法C# 如何删除wpf中用于模拟的SphereVisual3D对象,c#,wpf,C#,Wpf,我试着用物理学来模拟一个被扔掉的球。 移动部分已经在我的计算中起作用了,但我似乎无法移除上一个球,这个球是一个SphereVisual3D对象 这就是高尔夫球的制作方法 class GolfBall { public void CreateBall(HelixViewport3D Viewport, double x, double y, double z) { Viewport.Children.Add(Balll(x, y, z)); } pu
class GolfBall
{
public void CreateBall(HelixViewport3D Viewport, double x, double y, double z)
{
Viewport.Children.Add(Balll(x, y, z));
}
public void DeleteBall(HelixViewport3D Viewport, double x, double y, double z)
{
Viewport.Children.Remove(Balll(x,y,z));
}
private SphereVisual3D Balll(double x, double y, double z)
{
var ball = new SphereVisual3D
{
Center = new Point3D(1.5, 12, -1.5),
Radius = 5,
Material = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 197, 147))),
Transform = new TranslateTransform3D(x, y, z)
};
return ball;
}
}
(MainWindow.xaml.cs)在这部分代码中创建了球,但DeleteBall方法似乎无法删除前一个球。我没有得到一个错误的方法
private void timer_Tick(object sender, EventArgs e)
{
time += timer.Interval;
deltaT = time - firstTick;
firstTick = time;
this.ball.DeleteBall(Viewport, BulletX, BulletY, 50);
Vy += YAcceleration;
BulletX += Vx;
BulletY -= Vy;
ball = new GolfBall();
ball.CreateBall(Viewport, BulletX, BulletY, 50);
}
我还尝试用这个代码删除球
Viewport.Children.Remove(球)代码>
在我的MainWindow.xaml.cs中,在方法timer_tick中,但我得到以下错误:
'参数1:无法从'\u 1819apep3dewulfflorian.GolfBall'转换为'
'System.Windows.Media.Media3D.Visual3D'1819apep3dewulfflorian'
对不起,我的英语不好。
如果我忘了什么,你可以告诉我。
谢谢。我举一个简单的例子:
main window.xaml
<Window x:Class="WpfApp10.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:helix="http://helix-toolkit.org/wpf"
xmlns:local="clr-namespace:WpfApp10" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="800" Height="450"
mc:Ignorable="d">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<helix:HelixViewport3D x:Name="viewPort" />
<Button Grid.Row="1" Content="Mover" Click="Button_Click" />
</Grid>
</Window>
MainWindow.xaml.cs
using HelixToolkit.Wpf;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Media3D;
namespace WpfApp10
{
/// <summary>
/// Lógica de interacción para MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
SphereVisual3D ball;
double x = 5;
double y = 0;
double z = 0;
double acceleration = 0.5;
public MainWindow()
{
InitializeComponent();
Loaded += MainWindow_Loaded;
}
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
ball = new SphereVisual3D
{
Center = new Point3D(1.5, 1, 1),
Radius = 0.5,
Material = new DiffuseMaterial(new SolidColorBrush(Color.FromRgb(255, 197, 147))),
Transform = new TranslateTransform3D(x, y, z),
Visible = true
};
viewPort.Children.Add(ball);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (y < -5)
{
y = 2;
}
if (x < -5)
{
x = 2;
}
if (z > 5)
{
z = -2;
}
y -= acceleration;
x -= 0.5;
z += 0.5;
ball.Transform = new TranslateTransform3D(x, y, z);
}
}
}
使用HelixToolkit.Wpf;
使用System.Windows;
使用System.Windows.Media;
使用System.Windows.Media.Media3D;
命名空间WpfApp10
{
///
///Lógica de interacción para main window.xaml
///
公共部分类主窗口:窗口
{
球面球;
双x=5;
双y=0;
双z=0;
双加速度=0.5;
公共主窗口()
{
初始化组件();
加载+=主窗口\u加载;
}
已加载私有void主窗口(对象发送器、路由目标)
{
ball=新球体平均3d
{
中心=新点3D(1.5,1,1),
半径=0.5,
材质=新的漫射材质(新的SolidColorBrush(颜色为RGB(255、197、147)),
变换=新的TranslateTransform3D(x,y,z),
可见=真
};
viewPort.Children.Add(ball);
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
如果(y<-5)
{
y=2;
}
如果(x<-5)
{
x=2;
}
如果(z>5)
{
z=-2;
}
y-=加速度;
x-=0.5;
z+=0.5;
ball.Transform=新的TranslateTransform3D(x,y,z);
}
}
}
我对wpf中的3d效果知之甚少
我希望它能帮助你。你好。你为什么不创造一个球然后移动它?@Roberto你好,我不知道怎么做。这不是让事情变得更复杂了吗?它确实帮助我对如何做不同的事情有了更多的了解,非常感谢。我能让它工作:)(但还是以另一种方式)