読者です 読者をやめる 読者になる 読者になる

ポンポンハネる

実行イメージとソース

XAMLは前回と一緒なので省略。

private double vx = 0;
private double vy = 0;
private double grabity = 1;

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
  var random = new Random();
  vx = random.NextDouble() * -10d;
  vy = random.NextDouble() * -10d;
  CompositionTarget.Rendering += CompositionTarget_Rendering;
}

private void CompositionTarget_Rendering(object sender, EventArgs e)
{
  if (Math.Abs(vx) < 0.00000001 && Math.Abs(vy) < 0.5)
  {
    var random = new Random();
    vx = random.NextDouble() * -10d;
    vy = random.NextDouble() * -50d;
  }

  vy += grabity;

  target.SetValue(Canvas.LeftProperty, (double)target.GetValue(Canvas.LeftProperty) + vx);
  target.SetValue(Canvas.TopProperty, (double)target.GetValue(Canvas.TopProperty) + vy);

  if ((double)target.GetValue(Canvas.LeftProperty) + target.Width > area.Width)
  {
    target.SetValue(Canvas.LeftProperty, area.Width - target.Width);
    vx *= -1d;
  }
  if ((double)target.GetValue(Canvas.LeftProperty) - target.Width < 0)
  {
    target.SetValue(Canvas.LeftProperty, 0 + target.Width);
    vx *= -1d;
  }
  if ((double)target.GetValue(Canvas.TopProperty) + (target.Height) > area.Height)
  {
    target.SetValue(Canvas.TopProperty, area.Height - target.Height);

    //地面の摩擦
    vx *= 0.95;

    //ボールの弾力
    vy *= -1d;
    vy *= 0.9;
  }
  if ((double)target.GetValue(Canvas.TopProperty) - (target.Height) < 0)
  {
    target.SetValue(Canvas.TopProperty, 0 + target.Height);
    vy *= -1d;
  }
}


ものすごい微妙。特に停止してるかどうかの判定がいけてない。


誰か、いけてる方法教えてください〜〜

実行イメージとソース