본문으로 바로가기

ReactPhysics3D 의 광선 교차 테스트

category Devlogs 2024. 3. 12. 19:24

ReactPhysics3D 의 광선 교차 테스트..

#include "reactphysics3d.h"
 
#include <iostream>
#include <stdexcept>
#include <vector>
#include <map>
#include <sstream>
#include <iomanip>
#include <memory>
#include <algorithm>
#include <set>
#include <string>
#include <fstream>
 
#define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/string_cast.hpp>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <glm/gtx/transform.hpp>
#include <glm/gtx/quaternion.hpp>
#include <glm/gtx/normal.hpp>
 
 
// https://github.com/DanielChappuis/reactphysics3d/blob/master/test/tests/collision/TestAABB.h
using namespace reactphysics3d;
 
 
class DynamicTreeRaycastCallback : public DynamicAABBTreeRaycastCallback {
 
    public:
 
        std::vector<int> mHitNodes;
 
        // Called when the AABB of a leaf node is hit by a ray
        virtual decimal raycastBroadPhaseShape(int32 nodeId, const Ray& ray) override {
            printf("--hit-- %d\n", nodeId);
            mHitNodes.push_back(nodeId);
            return 1.0;
        }
 
        void reset() {
            mHitNodes.clear();
        }
 
        bool isHit(int nodeId) const {
            return std::find(mHitNodes.begin(), mHitNodes.end(), nodeId) != mHitNodes.end();
        }
};
 
 
int main(void)
{
    DynamicTreeRaycastCallback mRaycastCallback;
 
    DynamicAABBTree tree(MemoryManager::getBaseAllocator());
 
    AABB aabb;
 
    std::vector<glm::vec3> vertices;
 
    int solverId = 1;
    vertices.push_back(glm::vec3(0, -2.5, 0));
    vertices.push_back(glm::vec3(-2.5, 2.5, 0));
    vertices.push_back(glm::vec3(2.5, 2.5, 0));
    aabb = AABB::createAABBForTriangle((Vector3*)&vertices[0]);
    int objectId = tree.addObject(aabb, &solverId);
    printf("%d\n", objectId);
 
    solverId = 2;
    glm::vec3 points2[] = {
        glm::vec3(0, -2.5, 2), glm::vec3(-2.5, 2.5, 2), glm::vec3(2.5, 2.5, 2)
    };
    aabb = AABB::createAABBForTriangle((Vector3*)points2);
    objectId = tree.addObject(aabb, &solverId);
    printf("%d\n", objectId);
     
    solverId = 3;
    vertices.clear();
    vertices.push_back(glm::vec3(0, -2.5, 4));
    vertices.push_back(glm::vec3(-2.5, 2.5, 4));
    vertices.push_back(glm::vec3(2.5, 2.5, 4));
    aabb = AABB::createAABBForTriangle((Vector3*)&vertices[0]);
    objectId = tree.addObject(aabb, &solverId);
    printf("%d\n", objectId);
 
 
    printf("Press any key to ray hit test..\n"); fflush(stdout);
    getchar();
 
    Ray ray(Vector3(0, 0, 10), Vector3(0, 0, -10));
 
    // hit test
    printf("Hit test..\n");
    mRaycastCallback.reset();
    tree.raycast(ray, mRaycastCallback);
 
    // hit test after remove
    printf("Hit test after remove..\n");
    tree.removeObject(1);
    mRaycastCallback.reset();
    tree.raycast(ray, mRaycastCallback);
 
#if 0
    printf("%f\n", tree.getMin().x);
    printf("%f\n", tree.getMin().y);
    printf("%f\n", tree.getMin().z);
    printf("%f\n", tree.getMax().x);
    printf("%f\n", tree.getMax().y);
    printf("%f\n", tree.getMax().z);
#endif
 
#if 0
    printf("%d\n", aabb.testRayIntersect(ray));
#endif
 
    return 0;
}

 


Korea Tcl/Tk Community
블로그 이미지 ihmin 님의 블로그
VISITOR 오늘 / 전체