介绍
连续碰撞检测(ccd)解决了子弹与纸张的问题。当一个快速移动的物体在一个时间步长内位于薄物体的一侧,在下一个时间步长内位于另一侧,导致物理引擎认为根本没有发生碰撞。而连续碰撞检测完全不使用时间步长,它创建整个时间步长的扫描体,并查找该扫描体是否与任何物体相交。这既昂贵又不准确(因为只使用简单的圆形碰撞形状,而非完整的碰撞形状)。
用法
连续碰撞检测是按对象设置的,您可以在同一场景中同时使用使用ccd和不使用ccd的对象。如下所示,在物理对象上设置ccd:
RigidBodyControl physicsObject = new RigidBodyControl(mass);
physicsObject.setCcdMotionThreshold(expectedWidthOfThinObject);
physicsObject.setCcdSweptSphereRadius(radiusOfSphereThatWillFullyContainObject);
- 你想将expectedWidthOfThinObject设定为尽可能高的值;请记住ccd十分昂贵且不准确。如果将其设置为零,则会关闭ccd
- 你想将radiusOfSphereThatWillFullyContainObject设定为尽可能小的值,同时完全包含对象
完整示例
以下源代码将展示使用连续碰撞检测和使用标准碰撞检测之间的差异。它会发射两组球,一组快速,一组慢速,并在5秒的时间间隔内开启和关闭ccd。慢速球总是与纸张碰撞,只有当ccd开启时,快速球才会碰撞。
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.font.BitmapText;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.shape.Sphere;
public class BulletTest extends SimpleApplication {
public static void main(String args[]) {
BulletTest app = new BulletTest();
app.start();
}
private BulletAppState bulletAppState;
Material wall_mat;
Material slow_mat;
Material fast_mat;
Material floor_mat;
private RigidBodyControl brick_phy;
private static final Box box;
private static final Sphere sphere;
private RigidBodyControl floor_phy;
private static final Box floor;
private static final float brickLength = 2f;
private static final float brickWidth = 0.015f;
private static final float brickHeight = 1f;
static {
sphere = new Sphere(32, 32, 0.1f, true, false);
box = new Box(brickWidth, brickHeight, brickLength);
floor = new Box(10f, 0.1f, 5f);
}
@Override
public void simpleInitApp() {
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
cam.setLocation(new Vector3f(0, 4f, 6f));
cam.lookAt(new Vector3f(2, 2, 0), Vector3f.UNIT_Y);
initMaterials();
initWall();
initFloor();
setUpHUDText();
}
public static final float FIREPERIOD=0.5f;
double fireTimer=0;
public static final float SWEEPPERIOD=5f;
double sweepTimer=0;
boolean continuouslySweeping=true;
@Override
public void simpleUpdate(float tpf) {
fireTimer+=tpf;
sweepTimer+=tpf;
if (sweepTimer>SWEEPPERIOD){
sweepTimer=0;
continuouslySweeping=!continuouslySweeping;
}
hudText.setText("ContinouslySweeping=" + continuouslySweeping + "(" + (int)(SWEEPPERIOD-sweepTimer) + ")" );
if (fireTimer>FIREPERIOD){
fireTimer=0;
makeCannonBall(new Vector3f(-4,3,0),new Vector3f(6,4,0),slow_mat,continuouslySweeping);
makeCannonBall(new Vector3f(-4,3,-0.5f),new Vector3f(10,1,0),fast_mat,continuouslySweeping);
}
}
public BitmapText hudText;
private void setUpHUDText(){
hudText = new BitmapText(guiFont, false);
hudText.setSize(guiFont.getCharSet().getRenderedSize());
hudText.setColor(ColorRGBA.White);
hudText.setText("ContinouslySweeping=true");
hudText.setLocalTranslation(300, hudText.getLineHeight(), 0);
guiNode.attachChild(hudText);
}
public void initMaterials() {
wall_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
wall_mat.setColor("Color", ColorRGBA.Blue);
fast_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
fast_mat.setColor("Color", ColorRGBA.Red);
slow_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
slow_mat.setColor("Color", ColorRGBA.Green);
floor_mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
floor_mat.setColor("Color", ColorRGBA.Gray);
}
public void initFloor() {
Geometry floor_geo = new Geometry("Floor", floor);
floor_geo.setMaterial(floor_mat);
floor_geo.setLocalTranslation(0, -0.1f, 0);
this.rootNode.attachChild(floor_geo);
floor_phy = new RigidBodyControl(0.0f);
floor_geo.addControl(floor_phy);
bulletAppState.getPhysicsSpace().add(floor_phy);
}
public void initWall() {
Vector3f location=new Vector3f(2,2,0);
Geometry brick_geo = new Geometry("brick", box);
brick_geo.setMaterial(wall_mat);
rootNode.attachChild(brick_geo);
brick_geo.setLocalTranslation(location);
brick_phy = new RigidBodyControl(0);
brick_geo.addControl(brick_phy);
bulletAppState.getPhysicsSpace().add(brick_phy);
}
public void makeCannonBall(Vector3f startPoint, Vector3f initialVelocity, Material material, boolean continuouslySwept) {
Geometry ball_geo = new Geometry("cannon ball", sphere);
ball_geo.setMaterial(material);
rootNode.attachChild(ball_geo);
ball_geo.setLocalTranslation(startPoint);
RigidBodyControl ball_phy = new RigidBodyControl(1f);
ball_geo.addControl(ball_phy);
bulletAppState.getPhysicsSpace().add(ball_phy);
ball_phy.setLinearVelocity(initialVelocity);
if (continuouslySwept){
ball_phy.setCcdMotionThreshold(0.015f);
ball_phy.setCcdSweptSphereRadius(0.01f);
}
}
}
通过对两组球的连续检测(从左上方进入的球),可以发现球弹跳效果正常:
![enter image description here](https://istack.dev59.com/WHZ0I.webp)
关闭连续检测后,快速运动的球(红色)会像穿过空气一样穿过纸张(偶尔也有慢速球(绿色)这样做):
![enter image description here](https://istack.dev59.com/vlcQv.webp)
注:此代码是基于Hello Physics代码并结合高级物理学中的功能进行修改的。