Skip to content

Commit 6ae0e73

Browse files
committed
Add physics demo
1 parent 7476e7b commit 6ae0e73

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed

examples/physics/demo.py

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
"""
2+
raylib [physac] example - physics demo
3+
"""
4+
5+
from pyray import Vector2
6+
from raylib import *
7+
from raylib.colors import (
8+
BLACK,
9+
GREEN,
10+
WHITE
11+
)
12+
13+
SCREEN_WIDTH = 800
14+
SCREEN_HEIGHT = 450
15+
16+
SetConfigFlags(FLAG_MSAA_4X_HINT)
17+
InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, b'[physac] Basic demo')
18+
19+
InitPhysics()
20+
21+
floor = CreatePhysicsBodyRectangle(Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT), 500, 100, 10)
22+
floor.enabled = False
23+
24+
circle = CreatePhysicsBodyCircle(Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), 45, 10)
25+
circle.enabled = False
26+
27+
SetTargetFPS(60)
28+
29+
while not WindowShouldClose():
30+
# Update
31+
# ----------------------------------------------------------------------
32+
UpdatePhysics() # Update physics system
33+
34+
if IsKeyPressed(KEY_R): # Reset physics system
35+
ResetPhysics()
36+
37+
floor = CreatePhysicsBodyRectangle(Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT), 500, 100, 10)
38+
floor.enabled = False
39+
40+
circle = CreatePhysicsBodyCircle(Vector2(SCREEN_WIDTH/2, SCREEN_HEIGHT/2), 45, 10)
41+
circle.enabled = False
42+
43+
# Physics body creation inputs
44+
if IsMouseButtonPressed(MOUSE_BUTTON_LEFT):
45+
CreatePhysicsBodyPolygon(GetMousePosition(), GetRandomValue(20, 80), GetRandomValue(3, 8), 10)
46+
elif IsMouseButtonPressed(MOUSE_BUTTON_RIGHT):
47+
CreatePhysicsBodyCircle(GetMousePosition(), GetRandomValue(10, 45), 10)
48+
49+
# Destroy falling physics bodies
50+
bodies_count = GetPhysicsBodiesCount()
51+
for i in reversed(range(bodies_count)):
52+
body = GetPhysicsBody(i)
53+
54+
if body and body.position.y > SCREEN_HEIGHT * 2:
55+
DestroyPhysicsBody(body)
56+
# ----------------------------------------------------------------------
57+
58+
# Draw
59+
# ----------------------------------------------------------------------
60+
BeginDrawing()
61+
62+
ClearBackground(BLACK)
63+
DrawFPS(SCREEN_WIDTH - 90, SCREEN_HEIGHT - 30)
64+
65+
# Draw created physics bodies
66+
bodies_count = GetPhysicsBodiesCount()
67+
for i in range(bodies_count):
68+
body = GetPhysicsBody(i)
69+
70+
if body:
71+
vertex_count = GetPhysicsShapeVerticesCount(i)
72+
for j in range(vertex_count):
73+
# Get physics bodies shape vertices to draw lines
74+
# Note: GetPhysicsShapeVertex() already calculates rotation transformations
75+
vertexA = GetPhysicsShapeVertex(body, j)
76+
77+
# Get next vertex or first to close the shape
78+
jj = (j + 1) if (j+1) < vertex_count else 0
79+
vertexB = GetPhysicsShapeVertex(body, jj)
80+
81+
# Draw a line between two vertex positions
82+
DrawLineV(vertexA, vertexB, GREEN)
83+
84+
DrawText(b'Left mouse button to create a polygon', 10, 10, 10, WHITE)
85+
DrawText(b'Right mouse button to create a circle', 10, 25, 10, WHITE)
86+
DrawText(b"Press 'R' to reset example", 10, 40, 10, WHITE)
87+
88+
EndDrawing()
89+
# ----------------------------------------------------------------------
90+
91+
ClosePhysics()
92+
CloseWindow()

0 commit comments

Comments
 (0)