-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics_movement.bas
140 lines (104 loc) · 4.92 KB
/
physics_movement.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
'********************************************************************************************
'
' Physac - Physics movement
'
' 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'
' Constants
CONST VELOCITY! = 0.5!
' Initialization
'--------------------------------------------------------------------------------------
CONST SCREENWIDTH& = 800&
CONST SCREENHEIGHT& = 450&
CONST LOGOTEXT = "Powered by"
SCREEN _NEWIMAGE(SCREENWIDTH, SCREENHEIGHT, 32)
DO: LOOP UNTIL _SCREENEXISTS
_TITLE "physac - Body controller 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 and wall rectangle physics bodies
SetVector2 vec, SCREENWIDTH / 2!, SCREENHEIGHT
DIM AS _UNSIGNED _OFFSET floor: floor = CreatePhysicsBodyRectangle(vec, SCREENWIDTH, 100, 10)
SetVector2 vec, SCREENWIDTH * 0.25!, SCREENHEIGHT * 0.6!
DIM AS _UNSIGNED _OFFSET platformLeft: platformLeft = CreatePhysicsBodyRectangle(vec, SCREENWIDTH * 0.25!, 10, 10)
SetVector2 vec, SCREENWIDTH * 0.75!, SCREENHEIGHT * 0.6!
DIM AS _UNSIGNED _OFFSET platformRight: platformRight = CreatePhysicsBodyRectangle(vec, SCREENWIDTH * 0.25!, 10, 10)
SetVector2 vec, -5, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET wallLeft: wallLeft = CreatePhysicsBodyRectangle(vec, 10, SCREENHEIGHT, 10)
SetVector2 vec, SCREENWIDTH + 4, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET wallRight: wallRight = CreatePhysicsBodyRectangle(vec, 10, SCREENHEIGHT, 10)
' Disable dynamics to floor and walls physics bodies
GetPhysicsBodyOffset body, floor: body.enabled = FALSE: SetPhysicsBodyOffset floor, body
GetPhysicsBodyOffset body, platformLeft: body.enabled = FALSE: SetPhysicsBodyOffset platformLeft, body
GetPhysicsBodyOffset body, platformRight: body.enabled = FALSE: SetPhysicsBodyOffset platformRight, body
GetPhysicsBodyOffset body, wallLeft: body.enabled = FALSE: SetPhysicsBodyOffset wallLeft, body
GetPhysicsBodyOffset body, wallRight: body.enabled = FALSE: SetPhysicsBodyOffset wallRight, body
' Create movement physics body
SetVector2 vec, SCREENWIDTH / 2!, SCREENHEIGHT / 2!
DIM AS _UNSIGNED _OFFSET playerBody: playerBody = CreatePhysicsBodyRectangle(vec, 50, 50, 1)
GetPhysicsBodyOffset body, playerBody
body.freezeOrient = TRUE ' Constrain body rotation to avoid collision torque
SetPhysicsBodyOffset playerBody, body
' Main game loop
DO
' Update
'----------------------------------------------------------------------------------
GetPhysicsBodyOffset body, playerBody
' Horizontal movement input
IF _KEYDOWN(19712) THEN body.velocity.x = VELOCITY ' Right arrow key
IF _KEYDOWN(19200) THEN body.velocity.x = -VELOCITY ' Left arrow key
' Vertical movement input checking if player physics body is grounded
IF _KEYDOWN(18432) AND body.isGrounded THEN body.velocity.y = -VELOCITY * 4 ' Up arrow key
SetPhysicsBodyOffset playerBody, body
'----------------------------------------------------------------------------------
' 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 (10, 10), "Use 'ARROWS' to move player"
_PUTIMAGE (SCREENWIDTH - 100, 0)-(SCREENWIDTH - 1, 99), logo
COLOR Black
_PRINTSTRING (logoX, logoY), LOGOTEXT
_DISPLAY
_LIMIT 60
'----------------------------------------------------------------------------------
LOOP UNTIL _KEYHIT = 27
' De-Initialization
'--------------------------------------------------------------------------------------
ClosePhysics
'--------------------------------------------------------------------------------------
SYSTEM