-
Notifications
You must be signed in to change notification settings - Fork 0
/
rigid-bodies.html
169 lines (141 loc) · 6.46 KB
/
rigid-bodies.html
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<!DOCTYPE html>
<html>
<head>
<meta name = viewport content = "width=device-width,initial-scale=1">
<link rel = icon href = data/images/favicon.ico sizes = "16x16 32x32 48x48 64x64">
<script src = data/js/setup.js></script>
<script src = data/js/code.js></script>
<link rel = stylesheet href = data/css/fontawesome.css>
<link rel = stylesheet href = data/css/all.css>
<link rel = stylesheet href = data/css/main.css>
<title>JoBase · Rigid Bodies</title>
</head>
<body>
<section class = top>
<a href = "/">JoBase</a>
<i class = "fa-solid fa-bars"></i>
<i class = "fa-solid fa-sun"></i>
<a href = lessons class = this>Lessons</a>
<a href = demos>Demos</a>
<a href = games>Games</a>
<a href = reference>Reference</a>
<a href = download>Download</a>
<a href = https://github.com/JoBase target = _blank>
<i class = "fa-brands fa-github"></i>
</a>
<a href = https://discord.gg/DzHyvZfa5q target = _blank>
<i class = "fa-brands fa-discord"></i>
</a>
</section>
<section class = main>
<a href = lessons><i class = "fa-solid fa-chevron-left"></i> Back</a>
<a class = right href = lessons>Next <i class = "fa-solid fa-chevron-right"></i></a>
<h1>Rigid Bodies</h1>
<p>
This lesson will introduce you to the basics of the physics engine.
It is a great tool for simulating realistic 2D physics in your game.
Let's set up our engine by calling the <a href = reference#Physics>Physics</a> class.
</p>
<script>
snippet([
"",
"import JoBase\n\nengine = Physics()\n\ndef loop():\n engine.update()\n\nrun()"
])
</script>
<p>
First we create our variable <mark>engine</mark>, then we call <mark>engine.update()</mark> in the game loop.
This calculates each frame for the physics simulation.
Now let's create a box and rotate it by <mark>10°</mark>.
</p>
<script>
snippet([
"import JoBase\n\nengine = Physics()",
"\n\nbox = Rectangle(angle = 10)",
"\n\ndef loop():\n engine.update()",
"\n\n box.draw()",
"\n\nrun()"
])
</script>
<p>
We can add it to the physics environment by giving it a body.
</p>
<script>
snippet([
"import JoBase\n\nengine = Physics()\n\nbox = Rectangle(angle = 10)",
"\nbox.body = engine.body()",
"\n\ndef loop():\n engine.update()\n\n box.draw()\n\nrun()"
])
</script>
<p>
Now our box falls from the sky!
We can give it something to land on by creating another rectangle for the ground.
</p>
<script>
snippet([
"import JoBase\n\nengine = Physics()\n\nbox = Rectangle(angle = 10)\nbox.body = engine.body()",
"\n\nfloor = Rectangle(0, -200, 600, 20)",
"\n\ndef loop():\n engine.update()\n\n box.draw()",
"\n floor.draw()",
"\n\nrun()"
])
</script>
<p>
But our box falls through the ground!
This is because we haven't given the ground a rigid body.
We don't want the ground to be affected by gravity, so we use the <mark>STATIC</mark> variable to keep it in the same place.
</p>
<script>
snippet([
"import JoBase\n\nengine = Physics()\n\nbox = Rectangle(angle = 10)\nbox.body = engine.body()\n\nfloor = Rectangle(0, -200, 600, 20)",
"\nfloor.body = engine.body(STATIC)",
"\n\ndef loop():\n engine.update()\n\n box.draw()\n floor.draw()\n\nrun()"
])
</script>
<div class = slot>
<img src = data/images/box.png>
</div>
<p>
Now our box falls and hits the ground.
How can we make it more interesting?
We could try changing the elasticity (bounciness) of the box.
</p>
<script>
snippet([
"import JoBase\n\nengine = Physics()\n\nbox = Rectangle(angle = 10)",
"\nbox.elasticity = 0.8",
"\nbox.body = engine.body()\n\nfloor = Rectangle(0, -200, 600, 20)",
"\nfloor.elasticity = 0.8",
"\nfloor.body = engine.body(STATIC)\n\ndef loop():\n engine.update()\n\n box.draw()\n floor.draw()\n\nrun()"
])
</script>
<p>
In the code above, we also change the ground's elasticity to make the bounce more effective.
An elasticity of <mark>0</mark> prevents the object from bouncing, and an elasticity of <mark>1</mark> makes the object bounce forever.
</p>
<div class = slot>
<img src = data/images/bounce.png>
</div>
<p>
Now you know the basics of simulating 2D physics!
You could even try changing the direction of gravity, but you'll need to add a ceiling for the box that falls upwards.
</p>
<script>
snippet([
"engine = Physics(",
"gravity_y = 500",
")"
])
</script>
<p>
Our <a href = basic-physics>basic physics</a> demo uses the concepts in this tutorial to create a particle simulation.
</p>
<a href = lessons><i class = "fa-solid fa-chevron-left"></i> Back</a>
<a class = right href = lessons>Next <i class = "fa-solid fa-chevron-right"></i></a>
</section>
<footer>
<span>
© Copyright <span id = year></span> JoBase · <a href = mailto:hello@jobase.org>hello@jobase.org</a>
</span>
</footer>
</body>
</html>