Collisions - ZackWilde27/Z3dPy GitHub Wiki
Say we have myCharacter and thatTree as things, and a basic draw loop:
myCharacter = z3dpy.Thing([charBody, charSword], 0, 0, 0)
thatTree = z3dpy.Thing([treeMesh], 2, 5, 6)
z3dpy.AddThing(myCharacter)
z3dpy.AddThing(thatTree)
while True:
for tri in z3dpy.Raster()
z3dpy.PgDrawTriF(tri, z3dpy.TriGetNormal(tri)[2], screen, pygame)
pygame.display.flip()
In order to simulate collisions, each thing needs a hitbox.
# ThingSetupHitbox(thing, *type, *id, *radius, *height)
z3dpy.ThingSetupHitbox(myCharacter)
z3dpy.ThingSetupHitbox(thatTree)
# Parameters can be set when creating the hitbox, but there's also ThingSetCollision()
# ThingSetCollision(thing, type, id, radius, height)
z3dpy.ThingSetCollision(myCharacter, 2, 0, 1.0, 1.0)
Type determines the shape, 0 = sphere, and 2 = box.
Box is tested stable, sphere not as much.
Only hitboxes with the same Id will collide.
Radius determines the width/length of the box, or radius of the sphere
Height determines the height of the box
BasicHandleCollisions()
BasicHandleCollisions() requires two things that are colliding, so first GatherCollisions()
# GatherCollisions(thingList)
# returns a list of lists, containing the two items that are colliding
# no repeats
for collision in z3dpy.GatherCollisions([myCharacter, thatTree]):
firstThing = collision[0]
secondThing = collision[1]
# custom collision routine, or just
z3dpy.BasicHandleCollisions(collision)
GatherCollisions() will return a reference to the things, so everything will propagate back to the original thing
PhysicsCollisions(things)
PhysicsCollisions() does the gathering, and decides how to handle collisions.
It requires one or more things to have physics bodies.
z3dpy.ThingSetupHitbox(myCharacter)
z3dpy.ThingSetupPhysics(myCharacter)
while True:
z3dpy.HandlePhysicsFloor([myCharacter], 2)
for tri in z3dpy.Raster()
z3dpy.PgDrawTriF(tri, z3dpy.TriGetNormal(tri)[2], screen, pygame)
pygame.display.flip()
Now that we're using the physics engine, add the PhysicsCollisions() function to our loop.
Make sure it comes after HandlePhysics() so things are never drawn colliding.
while True:
z3dpy.HandlePhysicsFloor([myCharacter], 2)
z3dpy.PhysicsCollisions([myCharacter, thatTree])
for tri in z3dpy.Raster()
z3dpy.PgDrawTriF(tri, z3dpy.TriGetNormal(tri)[2], screen, pygame)
PhysicsCollisions() will GatherCollisions(), then applies velocity to push them apart.
Will only push things that have a PhysicsBody. When a non-physics thing is encountered, it'll use BasicHandleCollisions().