compositionTarget.rendering:更改变量

时间:2015-02-15 10:01:54

标签: c# wpf

我目前正在使用CompositionTarget.Rendering作为我的弹丸模拟的图形,但我不知道如何让我的加速度改变。是否有可能将它变为变量并被数据化,以便它变化,加速度也会变化?

感谢您的帮助。

到目前为止我的代码:

    private Point position; // in pixels
    private Vector Velocity; // in pixels per second
    private Vector acceleration; // in pixels per square second

    Velocity = new Vector(HVelTemp, VVelTemp); // y direction is downwards
    acceleration = new Vector(0, -1 * newEnvironment.gravity * PixelPerMeter); // y direction is downwards
    private void OnLoaded(object sender, RoutedEventArgs e)
    {
        position = new Point(5, (HighestPoint * PixelPerMeter)); // fix later pls
        int removedtime = 0;
        time = DateTime.Now;
        RenderingEvent = (s, g) => OnRendering(s, g, removedtime, time);
        CompositionTarget.Rendering += RenderingEvent;
    }

    private void OnRendering(object sender, EventArgs g, int removedtime, DateTime t)
    {
        var now = DateTime.Now.AddSeconds(-removedtime);
        var dt = (now - t).TotalSeconds;
        time = now;
        position += Velocity * dt;
        Velocity += acceleration * dt;
        projectileGeometry.Center = position;
    }

1 个答案:

答案 0 :(得分:0)

您可以将Acceleration声明为依赖属性。因此它将是可绑定的。以下声明假定您的代码在MainWindow类中。

public static readonly DependencyProperty AccelerationProperty =
    DependencyProperty.Register(
        "Acceleration", typeof(Vector), typeof(MainWindow));

public Vector Acceleration
{
    get { return (Vector)GetValue(AccelerationProperty); }
    set { SetValue(AccelerationProperty, value); }
}

...
Velocity += Acceleration * dt;