C#物理库帮助 [英] C# Physics Library Help

查看:59
本文介绍了C#物理库帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我目前正在用c#编写一个名为AnotherPhys的图书馆( https:// github .com / bentekken / AnotherPhys [ ^ ])





编辑:首先,我需要一种以有效的方式解决冲突(主要问题)的方法。其次,我希望能够保持相当简单但有效。



这个项目是为了个人利益,也是为了扩大我对c#的理解。一旦我成功编写了这个库,我就会尝试用c ++编写。

这是到目前为止的代码:

Hi,
I am currently writing a library in c# known as "AnotherPhys" (https://github.com/bentekken/AnotherPhys[^])


edit: Firstly, I need a way of solving collisions (main issue) in an efficient manner. Secondly, I want to be able to keep it fairly simple but effective.

This project is for personal interest but also for me to broaden my understanding of c#. Once I have successfully written this library, I''m going to try and write in c++.
this is the code so far:

public class PhysicsObject
    {
        //Default Constructor
        public PhysicsObject() { }
        public PhysicsObject(double mass, double acceleration)
        {
            this.Mass = mass;
            this.Acceleration = acceleration;
        }


        int x; int y;
        double StartTime; double Time; double ChangeTime;
        double StartSpeed; double Speed; double ChangeSpeed;
        double Mass { get; set; }
        double Acceleration { get; set; }
        public double GetAccelSync()
        {
            ChangeSpeed = (Speed - StartSpeed);
            ChangeTime = (Time - StartTime);
            Acceleration = (Speed - StartSpeed) / (Time - StartTime);
            return (Speed - StartSpeed) / (Time - StartTime);
        }
        public double GetSpeedSync()
        {
            return Acceleration * ChangeTime;
        }
        public void MoveUpdate()
        {
            GetAccelSync();
        }
        /// <summary>
        /// Returns a timespan based on time change. The unit is pixels per second (p/s).
        /// Returning a TimeSpan provides more flexibility.
        /// </summary>
        /// <returns>TimeSpan</returns>
        TimeSpan GetDuration()
        {
            return TimeSpan.FromSeconds(Time - StartTime);
        }
    }
    public class PhysicsWorld
    {
        Rectangle WorldBounds;
        PhysicsObject[] PhysObjArray { get; set; }
        Timer WorldTime;
        public PhysicsWorld() 
        {
            WorldTime.Tick += new EventHandler(WorldTime_Tick);
        }


        void WorldTime_Tick(object sender, EventArgs e)
        {
            Collisions();
            
        }
        public void Collisions()
        {
            foreach (PhysicsObject obj in PhysObjArray)
            { 
                
            }
        }
        public bool Freeze()
        {
            WorldTime.Stop();
            return true;
        }
        public bool UnFreeze()
        {
            WorldTime.Start();
            return true;
        }
        
        public void AddPhysObj(PhysicsObject PhysObj)
        {
            PhysObjArray[PhysObjArray.Length] = PhysObj;
        }


        double GravityValue { get; set; }
        public void SetGravity(double gravity)
        {
            this.GravityValue = gravity;
        }
    }

推荐答案

您的 PhysicsObject '看起来无量纲点(这是一个很有争议的假设)因此如果它们的 {x,y} 坐标完全相同,就会发生碰撞。您可以这样检查:

Your PhysicsObject''s looks dimensionless points (it is a quite arguable assumption) hence a collision happens if their {x,y} coordinates are exactly the same. You might check that this way:
for (i=0; n<physobjarray.length-1; i++)   {
  for (j=i+1; n<physobjarray.length; j++)    {
    if (  PhysObjArray[i].x == PhysObjArray[j].x && PhysObjArray[i].y == PhysObjArray[j].y)
    {
      // collision detected here
    }
  }
}





顺便说一下你的碰撞 methjod应该自己处理碰撞(这是它们的后果)或者返回一个包含碰撞对象的数组。



By the way your Collisions methjod should either handle itself the collions (that is their consequences) or return an array containing colliding objects.


首先,我将默认构造函数设为私有。你可能不想创建具有所有0属性的对象,至少不是偶然的。

你的对象构造函数只有质量和加速度。相反,我会创建至少具有质量和位置(x,y)和可能用于碰撞检测的某种尺寸(例如,直径/半径)的物体。

速度(速度)和加速度需要设置,或者必须是构造函数的一部分(我更喜欢构造函数)。

位置需要是一个可读属性。

速度和加速度都是向量量(x'',y''和x'''',y'''')

质量可能应该是在构造函数之后不能设置(取决于你的建模)。

如果你想准确地模拟物体随时间改变位置和速度,例如由于引力交互,那么更新需求分两个阶段完成。

1.每个对象,

   a。计算作用于它的力的矢量和

     - 例如,对于重力,磁力或静电相互作用,你需要所有当前地点

   b。计算下一个加速度矢量

   c。根据当前和下一个加速度矢量的平均值计算下一个速度矢量

   d。根据当前和下一个速度矢量的平均值计算下一个位置

2.每个对象

   - 更新当前加速度,速度和位置向量以及相应的下一个向量
First I''d make the default constructor private. You probably don''t want to create objects with all 0 attributes, at least not by accident.
Your object constructor only has mass and acceleraton. Instead I''d create objects at least with mass and location (x,y) and possibly some measure of size (e.g., diameter/radius) for collision detection.
The speed (velocity) and acceleration need to be settable, or must be part of the constructor (I''d prefer the constructor).
Location needs to be a readable property.
Velocity and acceleration are both vector quantities (x'', y'' and x'''', y'''')
The Mass probably should not be settable after the constructor (depending on what you''re modelling).
If you want to accurately model objects changing position and velocity over time, for example due to gravitational interaction, then the updating needs to be done in two phases.
1. for each object,
  a. calculate the vector sum of forces acting on it
    - e.g., for gravity, magnetic or electrostatic interaction, you''ll need all current locations
  b. calculate the next acceleration vector
  c. calculate the next velocity vector based on the mean of the current and next acceleration vectors
  d. calculate the next location based on the mean of the current and next velocity vectors
2. for each object
  - update the current acceleration, velocity and location vectors with the corresponding next vectors


这篇关于C#物理库帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆