C#编程之物理力的实现

2021-01-21 23:14

阅读:726

标签:cto   navig   sch   logic   ros   task   stop   namespace   gre   

这里主要讲一下我们高中物理知识力在计算机模拟的效果。

我们知道,物体受到的力与物体质量和加速度有关,例如自由落体,它所产生的力由重力加速度g与其本身重量由关,F=Mg.

那么由力就可以得出加速度a=F÷M;

距离公式:S=V0*T+½ * a * T²

技术图片技术图片
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 System.Diagnostics;

namespace Geometry
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        // veriables
        private Vector v1 = new Vector(0, 0);       //speed
        private TimeSpan prevTime = TimeSpan.Zero;  //time interval
        private Point point = new Point(0, 0);      //objct posion
        private Point pMouse = new Point(0, 0);     //layout posion
        private Line line = new Line();             //line
        private Stopwatch stopwatch = new Stopwatch();// timer
        private void Windown_Load(object sender, RoutedEventArgs e)
        {
            //add RenderTransform
            this.moveObj.RenderTransform = new TranslateTransform();
            //counter start
            stopwatch.Start();
            //对象提供了创建基于每帧回调的自定义动画的功能。
            //每帧渲染之前触发CompositionTarget的Rendering事件。
            //想要在每帧动画渲染前做一些处理,可以调用实现此事件。
            CompositionTarget.Rendering += new EventHandler(CompositionTarget_render);
            //line
            line.StrokeThickness = 2;
            line.Stroke = System.Windows.Media.Brushes.LightBlue;
            layout.Children.Add(line);
        }
        private void Window_MouseLeftButtonUp(object sender,MouseButtonEventArgs e)
        {
            //get the object position
            point = e.GetPosition(moveObj);
            point.Offset(-moveObj.ActualWidth / 2, -moveObj.ActualHeight / 2);
            pMouse=e.GetPosition(layout);
        }
        private void CompositionTarget_render(object sender, EventArgs e)
        {
            //获取当前实例测量得出的总运行时间
            TimeSpan currentTime = this.stopwatch.Elapsed;
            double t = (currentTime - this.prevTime).TotalSeconds;
            this.prevTime = currentTime;
            Vector fPush = new Vector(point.X, point.Y);
            double eta = 100;
            double d = moveObj.Width;
            double rho = 1;
            double m = (Math.PI * d * d * d * rho / 6);
            fPush = d * d * d * fPush;
            Vector vDiff = ((fPush - 3 * Math.PI * eta * v1 * d) / m) * t;
            Vector s = v1 * t + vDiff * t / 2;

            //trajectory
            TranslateTransform translate = (TranslateTransform)moveObj.RenderTransform;
            translate.X += s.X;
            translate.Y += s.Y;
            //new position
            point.X -= s.X;
            point.Y -= s.Y;
            Vector v2 = vDiff + v1;
            v1 = v2;
            //line
            line.X1 = pMouse.X;
            line.Y1 = pMouse.Y;
            line.X2 = pMouse.X - point.X;
            line.Y2 = pMouse.Y - point.Y;
        }
    }
}
CS
技术图片技术图片
"Geometry.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" Loaded="Windown_Load" MouseLeftButtonUp="Window_MouseLeftButtonUp">
    "layout">
        "auto" Margin="0">
            "-1.5 2 4" LookDirection="0.4 -0.5 -1"/>
            "
                                                -1,0, 0 
                                                0 ,0,-1 
                                                1 ,0, 0 
                                                0 ,0, 1 
                                                0 ,1, 0"
                                                TriangleIndices="
                                                0 1 2 
                                                2 3 0
                                                1 0 4
                                                0 3 4
                                                3 2 4
                                                2 1 4
                                                " />
                            "Green" Opacity="0.5"/>
                                    "Red" Opacity="0.5"/>
                                    "myAngleRotation" Axis="0,1,0" Angle="0"/>
                                    "1 0 0" Color="White"/>
                    "Viewport3D.Loaded">
                    DoubleAnimation 
                                    Storyboard.TargetName="myAngleRotation"
                                    Storyboard.TargetProperty="Angle"
                                    From="0" To="360" Duration="0:0:10"
                                    RepeatBehavior="Forever" />
                        "moveObj" Width="80" Height="80" Source="F:\MiloLu\2015\vs\C#\ico\favicon-20190830044057967.ico"/>
    
XAML

技术图片

我们添加Image是直接添加其路径下的文件:

1 "moveObj" Width="80" Height="80" Source="F:\MiloLu\2015\vs\C#\ico\favicon-20190830044057967.ico"/>

End.

谢谢.

C#编程之物理力的实现

标签:cto   navig   sch   logic   ros   task   stop   namespace   gre   

原文地址:https://www.cnblogs.com/lumao1122-Milolu/p/12095795.html


评论


亲,登录后才可以留言!