對於我正在構建的物理引擎,我需要快速確定兩個剛體是否在前一幀接觸。我希望它儘可能快,所以也許你們可以提供一些想法?聯繫圖
這是我到目前爲止。 (和它的作品不錯,但整個事情讓我想知道我怎麼能改善它。)
// I thought using a struct would be a good idea with only two values?
struct Contact
{
public readonly PhyRigidBody Body1;
public readonly PhyRigidBody Body2;
public Contact(PhyRigidBody body1, PhyRigidBody body2)
{
Body1 = body1;
Body2 = body2;
}
}
class ContactComparer : IEqualityComparer<Contact>
{
public bool Equals(Contact x, Contact y)
{
// It just have to be the two same bodies, nevermind the order.
return (x.Body1 == y.Body1 && x.Body2 == y.Body2) || (x.Body1 == y.Body2 && x.Body2 == y.Body1);
}
public int GetHashCode(Contact obj)
{
// There has got to be a better way than this?
return RuntimeHelpers.GetHashCode(obj.Body1) + RuntimeHelpers.GetHashCode(obj.Body2);
}
}
// Hold all contacts in one big HashSet.
private HashSet<Contact> _contactGraph = new HashSet<Contact>(new ContactComparer());
// To query for contacts I do this
Contact contactKey = new Contact(body1, body2);
bool prevFrameContact = _contactGraph.Remove(contactKey);
// ... and then I re-insert it later, if there is still contact.
如何提取擴展或類級別的方法,「聯繫」PhyRigidBody - 即布爾聯繫= somePhyRigidBody.Contact(anotherObject)。更好的是,如果你以後想要實現非剛體物理,你應該考慮使Contact()方法成爲接口的一部分,這樣你就可以輕鬆擴展你的庫。 – 2009-06-12 18:45:50
我認爲你在這裏混淆了兩個不同的問題:如何確定聯繫人確實應該到達可以被覆蓋的地方(如界面等)。但這傢伙正在詢問如何*存儲*前一幀的聯繫數據,據推測*在計算之後*。 – Avish 2009-06-12 19:08:53