您必须将ContactAddedCallback添加到BulletGlobals
在像这样创建ContactAddedCallback之后执行此操作::
ContactAddedCallback myCollisionCallback = new ContactAddedCallback() {
@Override
public boolean contactAdded(
ManifoldPoint cp,
CollisionObject colObj0,
int partId0,
int index0,
CollisionObject colObj1,
int partId1,
int index1) {
System.out.println("hi i am collision !!");// :|
return false;
}
};
并像这样向刚体添加标志::
rb = new RigidBody(constructionInfo);
rb.setCollisionFlags(CollisionFlags.CUSTOM_MATERIAL_CALLBACK);
现在添加到BulletGlobals像这样::
BulletGlobals.setContactAddedCallback(myCollisionCallback);
如果你想得到哪个 RigidBody 被碰撞,请这样做::
当您创建 CollisionShape 时,您必须像这样添加 userPointer ::
BoxShape groundShape = new BoxShape(new Vector3f(1000,10,1000));
rb = new RigidBody(constructionInfo);
...
groundShape.setUserPointer(rb);
最后在 ContactaddedCallback 你可以像这样检查::
ContactAddedCallback myCollisionCallback = new ContactAddedCallback() {
@Override
public boolean contactAdded(
ManifoldPoint cp,
CollisionObject colObj0,
int partId0,
int index0,
CollisionObject colObj1,
int partId1,
int index1) {
if (colObj0.getUserPointer().equals(rb)) {
//for sample you can push them to up
rb.applyCentralImpulse(new Vector3f(0,1,0));
}
if (colObj1.getUserPointer().equals(rb)) {
rb.applyCentralImpulse(new Vector3f(0,1,0));
}
System.out.println("hi i am collision !!");// :|
return false;
}
};