-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics_friction.bas
149 lines (118 loc) · 5.14 KB
/
physics_friction.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
'********************************************************************************************
'
' Physac - Physics friction
'
' 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 - Friction 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 (PhysicsBody)
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)
SetPhysicsBodyOffset floor, body ' write type to ptr
' Create wall rectangle physics body
SetVector2 vec, SCREENWIDTH / 2!, SCREENHEIGHT * 0.8!
DIM AS _UNSIGNED _OFFSET wall: wall = CreatePhysicsBodyRectangle(vec, 10, 80, 10)
GetPhysicsBodyOffset body, wall ' read type from ptr
body.enabled = _FALSE ' Disable body state to convert it to static (no dynamics, but collisions)
SetPhysicsBodyOffset wall, body ' write type to ptr
' Create left ramp rectangle physics body
SetVector2 vec, 25, SCREENHEIGHT - 5
DIM AS _UNSIGNED _OFFSET rectLeft: rectLeft = CreatePhysicsBodyRectangle(vec, 250, 250, 10)
GetPhysicsBodyOffset body, rectLeft
body.enabled = _FALSE
SetPhysicsBodyOffset rectLeft, body
SetPhysicsBodyRotation rectLeft, 30! * PHYSAC_DEG2RAD
' Create right ramp rectangle physics body
SetVector2 vec, SCREENWIDTH - 25, SCREENHEIGHT - 5
DIM AS _UNSIGNED _OFFSET rectRight: rectRight = CreatePhysicsBodyRectangle(vec, 250, 250, 10)
GetPhysicsBodyOffset body, rectRight
body.enabled = _FALSE
SetPhysicsBodyOffset rectRight, body
SetPhysicsBodyRotation rectRight, 330! * PHYSAC_DEG2RAD
' Create dynamic physics bodies
SetVector2 vec, 35, SCREENHEIGHT * 0.6!
DIM AS _UNSIGNED _OFFSET bodyA: bodyA = CreatePhysicsBodyRectangle(vec, 40, 40, 10)
GetPhysicsBodyOffset body, bodyA
body.staticFriction = 0.1!
body.dynamicFriction = 0.1!
SetPhysicsBodyOffset bodyA, body
SetPhysicsBodyRotation bodyA, 30! * PHYSAC_DEG2RAD
SetVector2 vec, SCREENWIDTH - 35, SCREENHEIGHT * 0.6!
DIM AS _UNSIGNED _OFFSET bodyB: bodyB = CreatePhysicsBodyRectangle(vec, 40, 40, 10)
GetPhysicsBodyOffset body, bodyB
body.staticFriction = 1!
body.dynamicFriction = 1!
SetPhysicsBodyOffset bodyB, body
SetPhysicsBodyRotation bodyB, 330! * PHYSAC_DEG2RAD
' 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("Friction amount")) \ 2, 75), "Friction amount"
GetPhysicsBodyOffset body, bodyA
_PRINTSTRING (body.position.x - _PRINTWIDTH("0.1") \ 2, body.position.y - 7), "0.1"
GetPhysicsBodyOffset body, bodyB
_PRINTSTRING (body.position.x - _PRINTWIDTH("1") \ 2, body.position.y - 7), "1"
_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