如何使Farseer Physics在WPF中正常工作

时间:2018-12-11 19:27:45

标签: c# wpf

我正在尝试在 WPF窗口中使用 Farseer Engine 它可以工作,但是不能正常工作...

  • 首先,我下载了 Farseer Physics Engine 的最新稳定版本。
  • 在下一步中,我手动添加了Microsoft.Xna.Framework.dll以将其用于矢量。

这是我的全部代码:

MainWindow.xaml:

<Window x:Class="C_2D_WPF.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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:C_2D_WPF"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800" Loaded="Window_Loaded">
    <Grid Background="#FF4F4F4F">
        <Rectangle x:Name="floor_mesh" Fill="#FF303030" HorizontalAlignment="Left" Height="55" Margin="122,307,0,0" Stroke="Black" VerticalAlignment="Top" Width="556" RenderTransformOrigin="0.5,0.5"/>
        <Ellipse x:Name="ball_mesh" Fill="#FF71BF30" HorizontalAlignment="Left" Height="128" Margin="332,50,0,0" Stroke="Black" VerticalAlignment="Top" Width="128"/>
        <Rectangle x:Name="box_mesh" Fill="#FF272727" HorizontalAlignment="Left" Height="100" Margin="239,207,0,0" Stroke="Black" VerticalAlignment="Top" Width="205"/>
        <Rectangle x:Name="box2_mesh" Fill="#FF71BF30" HorizontalAlignment="Left" Height="34" Margin="414,-123,0,0" Stroke="Black" VerticalAlignment="Top" Width="264"/>

    </Grid>
</Window>

MainWindow.cs:

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;
using FarseerPhysics;
using FarseerPhysics.Collision;
using FarseerPhysics.Common;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Factories;
using Microsoft.Xna.Framework;

namespace C_2D_WPF
{

    public partial class MainWindow : Window
    {
        World _world;
        Body _floor;
        Body _object;
        Body _box;
        Body _box2;

        public MainWindow()
        {
            InitializeComponent();
        }

        private async void Window_Loaded(object sender, RoutedEventArgs e)
        {

            _world = new World(new Vector2(0, 9.8f));
            _world.EnableSubStepping = true;
            _object = BodyFactory.CreateCircle(_world, (float)ball_mesh.ActualHeight/2, 2000, new Vector2((float)(ball_mesh.Margin.Left), (float)(ball_mesh.Margin.Top)));
            _object.Restitution = 0f;
            _object.Friction = 0.5f;
            _object.BodyType = BodyType.Dynamic;

            _floor = BodyFactory.CreateRectangle(_world, (float)floor_mesh.ActualWidth, (float)floor_mesh.ActualHeight, 10000, new Vector2((float)(floor_mesh.Margin.Left), (float)(floor_mesh.Margin.Top)));
            _floor.Restitution = 1f;
            _floor.Friction = 10f;
            _floor.IsStatic = true;

            _box = BodyFactory.CreateRectangle(_world, (float)box_mesh.ActualWidth, (float)box_mesh.ActualHeight, 10000, new Vector2((float)(box_mesh.Margin.Left), (float)(box_mesh.Margin.Top)));
            _box.Restitution = 1f;
            _box.Friction = 10f;
            _box.IsStatic = true;

            _box2 = BodyFactory.CreateRectangle(_world, (float)box2_mesh.ActualWidth, (float)box2_mesh.ActualHeight, 10000, new Vector2((float)(box2_mesh.Margin.Left), (float)(box2_mesh.Margin.Top)));
            _box2.Restitution = 0f;
            _box2.Friction = 0.5f;
            _box2.BodyType = BodyType.Dynamic;

            for (int i = 0; i < 1000; i++)
            {
                _world.Step(0.1f);
                ball_mesh.Margin = new Thickness(_object.Position.X,_object.Position.Y,0,0);
                box2_mesh.Margin = new Thickness(_box2.Position.X, _box2.Position.Y, 0, 0);
                await Task.Delay(1);

            }
        }
    }
}

问题是:

  • 位置和比例与wpf不匹配。
  • 球与地板或盒子碰撞后并不会停止弹跳。

如何使它像现实世界一样正常工作? :(我真的很困惑,几乎有关于Farseer引擎的任何文档...

感谢您的帮助...

0 个答案:

没有答案