【问题标题】:jmonkeyengine movement too fast for collision detectionjmonkeyengine 运动太快,无法进行碰撞检测
【发布时间】:2013-05-02 10:27:17
【问题描述】:

我正在使用 JMonkeyEngine 制作 Java 游戏,我按照他们网站上的教程在墙上射球(子弹)。我知道一切是如何运作的,但是当我增加子弹的速度时,它会直接穿过墙壁。

现在我知道发生这种情况的原因(因为子弹每帧移动太快而无法记录碰撞)。我也知道如何解决这个问题,我在他们的网站 (JMonkeyEngine) 上找到了以下方法。

setCcdMotionThreshold(0f)

但是谁能告诉我这是如何实现的,或者我在哪里可以找到使用它的人的样本?

【问题讨论】:

    标签: java collision-detection jmonkeyengine


    【解决方案1】:

    简介
    连续碰撞检测 (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();
      }
    
      /** Prepare the Physics Application State (jBullet) */
      private BulletAppState bulletAppState;
    
      /** Prepare Materials */
      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;
    
      /** dimensions used for wall */
      private static final float brickLength = 2f;
      private static final float brickWidth  = 0.015f;
      private static final float brickHeight = 1f;
    
      static {
        /** Initialize the cannon ball geometry */
        sphere = new Sphere(32, 32, 0.1f, true, false);
        /** Initialize the brick geometry */
        box = new Box(brickWidth, brickHeight, brickLength);
        /** Initialize the floor geometry */
        floor = new Box(10f, 0.1f, 5f);
    
      }
    
      @Override
      public void simpleInitApp() {
        /** Set up Physics Game */
        bulletAppState = new BulletAppState();
        stateManager.attach(bulletAppState);
        //bulletAppState.getPhysicsSpace().enableDebug(assetManager);
    
        /** Configure cam to look at scene */
        cam.setLocation(new Vector3f(0, 4f, 6f));
        cam.lookAt(new Vector3f(2, 2, 0), Vector3f.UNIT_Y);
    
        /** Initialize the scene, materials, and physics space */
        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); //slow arcing ball
                makeCannonBall(new Vector3f(-4,3,-0.5f),new Vector3f(10,1,0),fast_mat,continuouslySweeping); //fast straight ball
            }
        }
    
        public BitmapText hudText;
    
        private void setUpHUDText(){
            hudText = new BitmapText(guiFont, false);          
            hudText.setSize(guiFont.getCharSet().getRenderedSize());      // font size
            hudText.setColor(ColorRGBA.White);                             // font color
            hudText.setText("ContinouslySweeping=true");             // the text
            hudText.setLocalTranslation(300, hudText.getLineHeight(), 0); // position
            guiNode.attachChild(hudText);
        }
    
      /** Initialize the materials used in this scene. */
      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);
      }
    
      /** Make a solid floor and add it to the scene. */
      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);
        /* Make the floor physical with mass 0.0f! */
        floor_phy = new RigidBodyControl(0.0f);
        floor_geo.addControl(floor_phy);
        bulletAppState.getPhysicsSpace().add(floor_phy);
      }
    
      /** This loop builds a wall out of individual bricks. */
      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);
            /** Position the brick geometry  */
            brick_geo.setLocalTranslation(location);
    
            //paper thin objects will fall down, mass 0 clamps it in position
            brick_phy = new RigidBodyControl(0); 
    
            /** Add physical brick to physics space. */
            brick_geo.addControl(brick_phy);
            bulletAppState.getPhysicsSpace().add(brick_phy);
      }
    
    
      public void makeCannonBall(Vector3f startPoint, Vector3f initialVelocity, Material material, boolean continuouslySwept) {
            /** Create a cannon ball geometry and attach to scene graph. */
            Geometry ball_geo = new Geometry("cannon ball", sphere);
            ball_geo.setMaterial(material);
            rootNode.attachChild(ball_geo);
            /** Position the cannon ball  */
            ball_geo.setLocalTranslation(startPoint);
            /** Make the ball physcial with a mass > 0.0f */
            RigidBodyControl ball_phy = new RigidBodyControl(1f);
            /** Add physical ball to physics space. */
            ball_geo.addControl(ball_phy);
            bulletAppState.getPhysicsSpace().add(ball_phy);
            /** Accelerate the physcial ball to shoot it. */
            ball_phy.setLinearVelocity(initialVelocity);
    
            if (continuouslySwept){
                ball_phy.setCcdMotionThreshold(0.015f);
                ball_phy.setCcdSweptSphereRadius(0.01f);
            }
    
      }
    
    
    }
    

    连续检测两组球如预期反弹(球从左上角进入):

    通过连续检测,快速的一组球(红色)穿过纸张,就好像它不存在一样(偶尔慢速球(绿色)也会穿过):

    注意:此代码大致基于 hello physics 代码,并添加了来自 advanced physics 的功能

    【讨论】:

    • 这个例子很好!我将脉冲放大了 340f,它仍然有效!该代码实际上可能位于 JME 测试存储库中。我还添加了将退出球限制为 20 个的限制,以便我们可以在调试模式下轻松进行测试:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多