-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics_restitution.bas
131 lines (102 loc) · 4.34 KB
/
physics_restitution.bas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
'********************************************************************************************
'
' Physac - Physics restitution
'
' This example uses Physac (https://github.com/victorfisac/Physac)
'
'********************************************************************************************
_DEFINE A-Z AS LONG
OPTION _EXPLICIT
$COLOR:32
$EXEICON:'./physac.ico'
' Include physac library
'$INCLUDE:'include/physac.bi'
' Initialization
'--------------------------------------------------------------------------------------
CONST SCREENWIDTH& = 800&
CONST SCREENHEIGHT& = 450&
CONST LOGOTEXT = "Powered by"
SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32)
DO: LOOP UNTIL _SCREENEXISTS
_TITLE "physac - Restitution demo"
_PRINTMODE _KEEPBACKGROUND
' Physac logo drawing position
DIM AS LONG logoX: logoX = SCREENWIDTH - _PRINTWIDTH(LOGOTEXT) - 10
DIM AS LONG logoY: logoY = 15
DIM logo AS LONG: logo = _LOADIMAGE("physac.ico")
' Initialize physics and default physics bodies
InitPhysics _TRUE
DIM vec AS Vector2, body AS PhysicsBody
' Create floor rectangle physics body
SetVector2 vec, SCREENWIDTH / 2!, SCREENHEIGHT
DIM AS _UNSIGNED _OFFSET floor: floor = CreatePhysicsBodyRectangle(vec, SCREENWIDTH, 100, 10)
GetPhysicsBodyOffset body, floor ' read type from ptr
body.enabled = _FALSE ' Disable body state to convert it to static (no dynamics, but collisions)
body.restitution = 1
SetPhysicsBodyOffset floor, body
' Create circles physics bodies
SetVector2 vec, SCREENWIDTH * 0.25!, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET circleA: circleA = CreatePhysicsBodyCircle(vec, 30, 10)
GetPhysicsBodyOffset body, circleA
body.restitution = 0.0
SetPhysicsBodyOffset circleA, body
SetVector2 vec, SCREENWIDTH * 0.5!, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET circleB: circleB = CreatePhysicsBodyCircle(vec, 30, 10)
GetPhysicsBodyOffset body, circleB
body.restitution = 0.5
SetPhysicsBodyOffset circleB, body
SetVector2 vec, SCREENWIDTH * 0.75!, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET circleC: circleC = CreatePhysicsBodyCircle(vec, 30, 10)
GetPhysicsBodyOffset body, circleC
body.restitution = 0.9
SetPhysicsBodyOffset circleC, body
' Main game loop
DO
' Update
'----------------------------------------------------------------------------------
' Add any physics updates here if needed
'----------------------------------------------------------------------------------
' Draw
'----------------------------------------------------------------------------------
CLS , Black
' Draw created physics bodies
DIM AS LONG bodiesCount: bodiesCount = GetPhysicsBodiesCount
DIM i AS LONG
FOR i = 0 TO bodiesCount - 1
DIM bodyPtr AS _UNSIGNED _OFFSET: bodyPtr = GetPhysicsBody(i)
IF bodyPtr <> NULL THEN
DIM vertexCount AS LONG: vertexCount = GetPhysicsShapeVerticesCount(i)
' Get physics bodies shape vertices to draw lines
DIM j AS LONG
DIM vertexA AS Vector2: GetPhysicsShapeVertex bodyPtr, 0, vertexA
PSET (vertexA.x, vertexA.y), Black
FOR j = 1 TO vertexCount - 1
DIM vertexB AS Vector2: GetPhysicsShapeVertex bodyPtr, j, vertexB
LINE -(vertexB.x, vertexB.y), Green
NEXT
LINE -(vertexA.x, vertexA.y), Green
END IF
NEXT
' Draw FPS
COLOR White
_PRINTSTRING (SCREENWIDTH - 90, SCREENHEIGHT - 30), STR$(GetHertz) + " FPS"
' Draw UI elements
_PRINTSTRING ((SCREENWIDTH - _PRINTWIDTH("Restitution amount")) \ 2, 75), "Restitution amount"
GetPhysicsBodyOffset body, circleA
_PRINTSTRING (body.position.x - _PRINTWIDTH("0%") \ 2, body.position.y - 7), "0%"
GetPhysicsBodyOffset body, circleB
_PRINTSTRING (body.position.x - _PRINTWIDTH("50%") \ 2, body.position.y - 7), "50%"
GetPhysicsBodyOffset body, circleC
_PRINTSTRING (body.position.x - _PRINTWIDTH("90%") \ 2, body.position.y - 7), "90%"
_PUTIMAGE (SCREENWIDTH - 100, 0)-(SCREENWIDTH - 1, 99), logo
COLOR Black
_PRINTSTRING (logoX, logoY), LOGOTEXT
_DISPLAY
_LIMIT 60
'----------------------------------------------------------------------------------
LOOP UNTIL _KEYHIT = _KEY_ESC
' De-Initialization
'--------------------------------------------------------------------------------------
ClosePhysics
'--------------------------------------------------------------------------------------
SYSTEM