import math from panda3d.core import Vec3, LineSegs, LVecBase3f, LPoint3f,Point3, CollisionNode, CollisionBox, CollisionSphere, CollisionRay, CollisionTraverser, CollisionHandlerQueue from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase): def init(self): ShowBase.init(self)

    # 已知的平面的4个点坐标
    p1 = Point3(0, 1, 0)
    p2 = Point3(2, 0, 0)
    p3 = Point3(2, 0, 2)
    p4 = Point3(0, 0, 2)

    # 创建LineSegs对象并设置颜色和宽度
    lines = LineSegs()
    lines.set_color(1, 0, 0, 1)
    lines.set_thickness(2)

    # 从p1开始绘制线段
    lines.move_to(p1)
    lines.draw_to(p2)
    lines.draw_to(p3)
    lines.draw_to(p4)
    lines.draw_to(p1)

    # 创建NodePath并将其附加到场景图中
    node = lines.create()
    np = self.render.attach_new_node(node)

    # 计算平面法向量
    v1 = p2 - p1
    v2 = p3 - p1
    normal = v1.cross(v2).normalized()
    print('平面法向量:', normal)

    # 沿着法向量方向拉伸出长方体
    dist = 1
    p1_ext = p1 + normal * dist
    p2_ext = p2 + normal * dist
    p3_ext = p3 + normal * dist
    p4_ext = p4 + normal * dist
    p5_ext = p1 - normal * dist
    p6_ext = p2 - normal * dist
    p7_ext = p3 - normal * dist
    p8_ext = p4 - normal * dist

    # 打印8个顶点坐标
    print('长方体8个顶点坐标:')
    print('p1_ext:', p1_ext)
    print('p2_ext:', p2_ext)
    print('p3_ext:', p3_ext)
    print('p4_ext:', p4_ext)
    print('p5_ext:', p5_ext)
    print('p6_ext:', p6_ext)
    print('p7_ext:', p7_ext)
    print('p8_ext:', p8_ext)

    # 创建LineSegs对象并设置颜色和宽度
    lines = LineSegs()
    lines.set_color(0, 1, 0, 1)
    lines.set_thickness(2)

    # 绘制长方体的12条边
    lines.move_to(p1_ext)
    lines.draw_to(p2_ext)
    lines.draw_to(p3_ext)
    lines.draw_to(p4_ext)
    lines.draw_to(p1_ext)
    lines.move_to(p5_ext)
    lines.draw_to(p6_ext)
    lines.draw_to(p7_ext)
    lines.draw_to(p8_ext)
    lines.draw_to(p5_ext)
    lines.move_to(p1_ext)
    lines.draw_to(p5_ext)
    lines.move_to(p2_ext)
    lines.draw_to(p6_ext)
    lines.move_to(p3_ext)
    lines.draw_to(p7_ext)
    lines.move_to(p4_ext)
    lines.draw_to(p8_ext)

    # 创建NodePath并将其附加到场景图中
    node = lines.create()
    np = self.render.attach_new_node(node)

    # 创建CollisionBox对象来表示长方体
    box = CollisionBox(Point3(0,0,0), abs(p2_ext.x - p1_ext.x)/2, abs(p4_ext.y - p1_ext.y)/2, abs(p5_ext.z - p1_ext.z)/2)

    # 创建CollisionSphere对象来表示待检测的点
    sphere = CollisionSphere(Point3(1,1,1), 0.1)

    # 创建CollisionNode对象并将其附加到场景图中
    cnode = CollisionNode('box_cnode')
    cnode.add_solid(box)
    cnode.set_from_collide_mask(BitMask32.allOff())
    cnode.set_into_collide_mask(BitMask32.bit(1))
    cnode_np = self.render.attach_new_node(cnode)

    cnode2 = CollisionNode('sphere_cnode')
    cnode2.add_solid(sphere)
    cnode2.set_from_collide_mask(BitMask32.bit(1))
    cnode2.set_into_collide_mask(BitMask32.allOff())
    cnode2_np = self.render.attach_new_node(cnode2)
    cnode2_np.set_pos(1,1,1)

    # 创建CollisionTraverser和CollisionHandlerQueue对象
    collider = CollisionTraverser()
    queue = CollisionHandlerQueue()

    # 将CollisionNode对象附加到CollisionTraverser中
    collider.add_collider(cnode_np, queue)

    # 进行碰撞检测
    collider.traverse(self.render)

    # 检查碰撞结果
    if queue.get_num_entries() > 0:
        print('点在长方体内部')
    else:
        print('点在长方体外部')

app = MyApp() app.run()

Panda3D: 判断点是否在长方体内部 - 碰撞检测方法

原文地址: https://www.cveoy.top/t/topic/oII2 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录