LIBGDX学习]LibGDX代码详解(四十一)Bullet 接触测试

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.bullet.collision.btCollisionDispatcher;
import com.badlogic.gdx.physics.bullet.collision.btCollisionObject;
import com.badlogic.gdx.physics.bullet.collision.btCollisionObjectWrapper;
import com.badlogic.gdx.physics.bullet.collision.btCollisionWorld;
import com.badlogic.gdx.physics.bullet.collision.ContactResultCallback;
import com.badlogic.gdx.physics.bullet.collision.btDbvtBroadphase;
import com.badlogic.gdx.physics.bullet.collision.btDefaultCollisionConfiguration;
import com.badlogic.gdx.physics.bullet.collision.btManifoldPoint;

/** @author xoppa */
public class CollisionWorldTest extends BaseBulletTest {
    BulletEntity movingBox;
    boolean hit = false;
    Color normalColor = new Color();
    btCollisionObject other;

    public class TestContactResultCallback extends ContactResultCallback {
        @Override
        public float addSingleResult (btManifoldPoint cp, btCollisionObjectWrapper colObj0Wrap, int partId0, int index0,
                                      btCollisionObjectWrapper colObj1Wrap, int partId1, int index1) {
            hit = true;
            other = colObj0Wrap.getCollisionObject() == movingBox.body ? colObj1Wrap.getCollisionObject() : colObj0Wrap
                    .getCollisionObject();

            return 0f;
        }
    }

    TestContactResultCallback contactCB;

    @Override
    public BulletWorld createWorld () {
        btDefaultCollisionConfiguration collisionConfig = new btDefaultCollisionConfiguration();
        btCollisionDispatcher dispatcher = new btCollisionDispatcher(collisionConfig);
        btDbvtBroadphase broadphase = new btDbvtBroadphase();
        btCollisionWorld collisionWorld = new btCollisionWorld(dispatcher, broadphase, collisionConfig);
        return new BulletWorld(collisionConfig, dispatcher, broadphase, null, collisionWorld);
    }

    @Override
    public void create () {
        super.create();

        instructions = "Long press to toggle debug mode\nSwipe for next test\nCtrl+drag to rotate\nScroll to zoom";

        contactCB = new TestContactResultCallback();

        Model groundModel = world.getConstructor("ground").model;
        Model boxModel = world.getConstructor("box").model;

        world.addConstructor("collisionGround", new BulletConstructor(groundModel));
        world.addConstructor("collisionBox", new BulletConstructor(boxModel));

        world.add("collisionGround", 0f, 0f, 0f).setColor(0.25f + 0.5f * (float)Math.random(), 0.25f + 0.5f * (float)Math.random(),
                0.25f + 0.5f * (float)Math.random(), 1f);

        world.add("collisionBox", 0f, 1f, 5f).setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
                0.5f + 0.5f * (float)Math.random(), 1f);
        world.add("collisionBox", 0f, 1f, -5f).setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
                0.5f + 0.5f * (float)Math.random(), 1f);
        world.add("collisionBox", 5f, 1f, 0f).setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
                0.5f + 0.5f * (float)Math.random(), 1f);
        world.add("collisionBox", -5f, 1f, 0f).setColor(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
                0.5f + 0.5f * (float)Math.random(), 1f);
        movingBox = world.add("collisionBox", -5f, 1f, 0f);
        normalColor.set(0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(), 0.5f + 0.5f * (float)Math.random(),
                1f);
    }

    Color tmpColor = new Color();

    @Override
    public void render () {
        //movingBox.transform.val[Matrix4.M03] = movingBox.transform.val[Matrix4.M13] = movingBox.transform.val[Matrix4.M23] = 0f;
        // M03 零排三列?以此类推?意思是转动轴?
        movingBox.transform.val[Matrix4.M03] = movingBox.transform.val[Matrix4.M13] = movingBox.transform.val[Matrix4.M23] = 0f;

        movingBox.transform.rotate(Vector3.Y, Gdx.graphics.getDeltaTime() * 45f);
        movingBox.transform.translate(-5f, 1f, 0f);
        //movingBox.transform.translate(-0.1f, 0.1f, 0f);
        movingBox.body.setWorldTransform(movingBox.transform);

        super.render();
    }

    @Override
    public void update () {
        super.update();
        // Not using dynamics, so update the collision world manually
        if (world.performanceCounter != null) world.performanceCounter.start();
        world.collisionWorld.performDiscreteCollisionDetection();
        if (world.performanceCounter != null) world.performanceCounter.stop();
    }

    @Override
    protected void renderWorld () {
        hit = false;
        other = null;

        // 接触测试
        world.collisionWorld.contactTest(movingBox.body, contactCB);

        movingBox.setColor(hit ? Color.BLACK: normalColor);

        BulletEntity e = null;
        if (other != null && other.userData != null && other.userData instanceof BulletEntity) {
            e = (BulletEntity)(other.userData);
            tmpColor.set(e.getColor());
            e.setColor(Color.RED);
        }

        super.renderWorld();

        if (e != null) e.setColor(tmpColor);
    }

    @Override
    public void dispose () {
        super.dispose();
        movingBox = null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值