2

我正在使用 Physijs 来确定我的网格之间的静态碰撞。因为我需要知道哪些表面相交。

我破解了一个似乎可行的简单演示。

目前我必须将我的场景配置为使用重力,这会阻止我将我的网格定位在任何 y 位置,因为它们开始下落或漂浮。

有没有简单的方法从模拟中消除重力,只使用网格碰撞检测?

--update--- 我必须将每个网格的质量显式设置为 0 而不是空白。质量=0 重力没有影响。伟大的!

但是网格没有报告碰撞。有什么想法我哪里出错了吗?

谢谢

-lp

4

1 回答 1

2

您不能单独使用 Physijs 进行碰撞检测。它完全配备了基于 ammo.js 库的实时物理模拟。当您将网格的质量设置为 时0,它会使它们成为静态的。然后它们对外力没有反应,例如碰撞响应(即检测到碰撞后施加在网格上的速度变化)或重力。此外,两个相互重叠的静态网格不会触发碰撞事件。

方案A:直接使用ammo.js

Bullet Physics移植而来,该库提供了生成物理模拟的必要工具,或者只是检测定义形状之间的碰撞(Physijs 不希望我们看到)。这是用于检测 2 个刚性球体之间的碰撞的片段:

var bt_collision_configuration;
var bt_dispatcher;
var bt_broadphase;
var bt_collision_world;

var scene_size = 500;
var max_objects = 10; // Tweak this as needed

bt_collision_configuration = new Ammo.btDefaultCollisionConfiguration();
bt_dispatcher = new Ammo.btCollisionDispatcher(bt_collision_configuration);

var wmin = new Ammo.btVector3(-scene_size, -scene_size, -scene_size);
var wmax = new Ammo.btVector3(scene_size, scene_size, scene_size);

// This is one type of broadphase, Ammo.js has others that might be faster
bt_broadphase = new Ammo.bt32BitAxisSweep3(
        wmin, wmax, max_objects, 0, true /* disable raycast accelerator */);

bt_collision_world = new Ammo.btCollisionWorld(bt_dispatcher, bt_broadphase, bt_collision_configuration);

// Create two collision objects
var sphere_A = new Ammo.btCollisionObject();
var sphere_B = new Ammo.btCollisionObject();

// Move each to a specific location
sphere_A.getWorldTransform().setOrigin(new Ammo.btVector3(2, 1.5, 0));
sphere_B.getWorldTransform().setOrigin(new Ammo.btVector3(2, 0, 0));

// Create the sphere shape with a radius of 1
var sphere_shape = new Ammo.btSphereShape(1);

// Set the shape of each collision object
sphere_A.setCollisionShape(sphere_shape);
sphere_B.setCollisionShape(sphere_shape);

// Add the collision objects to our collision world
bt_collision_world.addCollisionObject(sphere_A);
bt_collision_world.addCollisionObject(sphere_B);

// Perform collision detection
bt_collision_world.performDiscreteCollisionDetection();

var numManifolds = bt_collision_world.getDispatcher().getNumManifolds();

// For each contact manifold
for(var i = 0; i < numManifolds; i++){
    var contactManifold = bt_collision_world.getDispatcher().getManifoldByIndexInternal(i);
    var obA = contactManifold.getBody0();
    var obB = contactManifold.getBody1();
    contactManifold.refreshContactPoints(obA.getWorldTransform(), obB.getWorldTransform());
    var numContacts = contactManifold.getNumContacts();

    // For each contact point in that manifold
    for(var j = 0; j < numContacts; j++){

        // Get the contact information
        var pt = contactManifold.getContactPoint(j);
        var ptA = pt.getPositionWorldOnA();
        var ptB = pt.getPositionWorldOnB();
        var ptdist = pt.getDistance();

        // Do whatever else you need with the information...
    }
}

// Oh yeah! Ammo.js wants us to deallocate
// the objects with 'Ammo.destroy(obj)'

我将这个 C++ 代码转换成它的 JS 等价物。可能缺少一些语法,因此您可以检查Ammo.js API 绑定更改是否有任何不起作用的内容。

方案B:使用THREE的光线投射器

光线投射器不太准确,但通过在形状中添加额外的顶点数可以更精确。下面是一些检测两个盒子之间碰撞的代码:

// General box mesh data
var boxGeometry = new THREE.CubeGeometry(100, 100, 20, 1, 1, 1);
var boxMaterial = new THREE.MeshBasicMaterial({color: 0x8888ff, wireframe: true});

// Create box that detects collision
var dcube = new THREE.Mesh(boxGeometry, boxMaterial);

// Create box to check collision with
var ocube = new THREE.Mesh(boxGeometry, boxMaterial);

// Create ray caster
var rcaster = new THREE.Raycaster(new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 1, 0));

// Cast a ray through every vertex or extremity
for(var vi = 0, l = dcube.geometry.vertices.length; vi < l; vi++){      
    var glovert = dcube.geometry.vertices[vi].clone().applyMatrix4(dcube.matrix);

    var dirv = glovert.sub(dcube.position);

    // Setup ray caster
    rcaster.set(dcubeOrigin, dirv.clone().normalize());

    // Get collision result
    var hitResult = rcaster.intersectObject(ocube);

    // Check if collision is within range of other cube
    if(hitResult.length && hitResult[0].distance < dirv.length()){ 
        // There was a hit detected between dcube and ocube
    }
}

查看这些链接以获取更多信息(可能还有它们的源代码):

于 2016-10-10T19:41:07.680 回答