Z3dPy Physics - ZackWilde27/Z3dPy GitHub Wiki
The built-in physics is quite basic but can get the job done.
Say we have a Thing object for our character, and a basic draw loop:
myCharacter = z3dpy.Thing([charBody, charSword], 0, 0, 0)
while True:
for tri in z3dpy.RenderThings([myCharacter]):
normal = z3dpy.TriGetNormal(tri)
pygame.draw.polygon(screen, normal[2], z3dpy.FormatTri(tri, False))
pygame.display.flip()
To enable physics, we need to add a physics body to our character, then add the HandlePhysicsFloor() function to our loop.
myCharacter.physics = z3dpy.PhysicsBody()
while True:
# Calculate delta to make everything run at the same speed, regardless of framerate
z3dpy.GetDelta()
# HandlePhysicsFloor(thingList, floorHeight)
z3dpy.HandlePhysicsFloor([myCharacter], 2)
HandlePhysicsFloor() needs a list of things, and a floor height, which things will not fall past. (Gravity is built in)
Now we can add a jump button for our character.
keys = pygame.key.get_pressed()
if keys[pygame.K_e]:
myCharacter.physics.velocity[1] = -12
# or
import keyboard
if keyboard.is_pressed("e"):
myCharacter.physics.velocity[1] = -12
Physics Bodies have 4 variables you can change:
-
Velocity: How much the position changes each frame
-
Acceleration: How much the velocity changes each frame
-
Mass: How much force an object exerts when colliding. (0+)
-
Friction: When colliding with another thing, how much is their velocity dampened. (0-1)
-
Bounciness: When hitting the ground or an object without physics, how much is the dominant direction reflected (0-1)
Air Drag is how much velocity falls off each frame, and is a global variable
z3dpy.airDrag = 0.02
If you want to change the gravity direction or strength, it can be accessed with the z3dpy.gravity variable
z3dpy.gravity = (0, 9.8, 0)
The physics collision system is on the collisions page