问题描述
我目前正在用c#编写一个名为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;
}
}
推荐答案
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.
这篇关于C#物理库帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!