Rays 0.3.2 - ZackWilde27/Z3dPy GitHub Wiki

z3dpy.Ray(vStartPos, vEndPos, *bIsArrow)

Rays are used for debugging, and collisions.

start = z3dpy.CamGetPos(myCamera)
end = z3dpy.ThingGetPos(myCharacter)

myRay = z3dpy.Ray(start, end)

print(z3dpy.RayGetDirection(myRay))

Drawing

To draw rays, add them to the internal rays list, and they will be drawn with DebugRaster()

# Rays in the global list will be drawn with DebugRaster()
z3dpy.rays.append(myRay)

for tri in z3dpy.DebugRaster():
    # Debug items will have an id of -1
    if z3dpy.TriGetId(tri) == -1:
        # DrawTriOutl() converts 0-1 to 0-255
        z3dpy.PgDrawTriOutl(tri, [1, 0, 0], screen, pygame)
    else:
        z3dpy.PgDrawTriFL(tri, screen, pygame)

In 0.2.8+, rays now have bIsArrow, which will draw the ray as an arrow pointing at the end location.

myRay = z3dpy.Ray(start, end, True)

Collisions

For collisions, use RayIntersect functions.

# Simple checks for collisions against the hitbox of the Thing
hit = z3dpy.RayIntersectThingSimple(myRay, myCharacter)
# Hit: [0] is wether or not it hit. [1] is location, [2] is distance, and [3] is the normal of the triangle that was hit
if hit[0]:
    print(hit[1])

# Complex checks for collisions against each triangle of each mesh of the Thing
hit = z3dpy.RayIntersectThingComplex(myRay, thatTree)
if hit[0]:
    print(hit[2])

hit = z3dpy.RayIntersectMesh(myRay, myMesh)
if hit[0]:
    print(hit[3])

hit = z3dpy.RayIntersectTri(myRay, thatTri)
if hit[0]:
    print("Yep")

When calculating Thing/Mesh collisions, it's triangles are temporarily transformed and translated to match their location in world-space. (Mesh triangles are always located relative to 0, 0, 0 until rendering)