FabGL2 2.0.0
ESP32 Display Controller and Graphics Library for ESP-IDF
Loading...
Searching...
No Matches
CollisionDetection

Animated space scene that shows the sprite collision detection of FabGL.

Ten spaceships and four asteroids fly across a starfield at 320x200 (64 colors) while Jupiter slowly crosses the background. When a spaceship hits another spaceship or an asteroid, the spaceship plays an explosion animation and the asteroid disappears; after the explosion both objects come back from a random screen side. Asteroids do not collide with each other.

The demo runs on its own: there are no controls.

What it demonstrates

  • fabgl::Scene: a game loop running at a fixed rate (init(), update(), collisionDetected())
  • Pixel-precise collision detection between sprites (Scene::addSprite(), updateSpriteAndDetectCollisions())
  • Sprites with several frames (spaceship + 8-frame explosion), Sprite::nextFrame()
  • Sprites drawn over a static background painted with fabgl::Canvas
  • fabgl::VGAController (64 colors) at 320x200

Hardware

  • VGA output (default pins)

Build

See Building the examples.

Source code

Main file of the example, main/CollisionDetection.cpp. The complete project is in the examples/VGA/CollisionDetection folder.

1/*
2 Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3 Copyright (c) 2019-2026 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#include "esp_random.h"
28#include "fabgl.h"
29
30
31// random integer in the range [0, max)
32static inline int randomInt(int max) { return max > 0 ? (int) (esp_random() % (uint32_t) max) : 0; }
33// random integer in the range [min, max)
34static inline int randomInt(int min, int max) { return max > min ? min + (int) (esp_random() % (uint32_t) (max - min)) : min; }
35
36
37#include "bitmaps.h"
38
39
40
41fabgl::VGAController DisplayController;
42
43
44#define SPACESHIP_COUNT 10
45#define ASTEROID_COUNT 4
46
47#define OBJECTS_COUNT (SPACESHIP_COUNT + ASTEROID_COUNT + 1) // +1 = is for jupiter!
48
49
50struct MySprite : Sprite {
51 int velX;
52 int velY;
53 bool isAlive;
54 bool isAsteroid;
55};
56
57
58struct MyScene : public Scene {
59
60 MySprite objects_[OBJECTS_COUNT];
61
62 MyScene()
63 : Scene(OBJECTS_COUNT, 20, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight())
64 {
65 }
66
67 void startSprite(MySprite * sprite)
68 {
69 // set initial position to one of the four sides of the screen
70 int q = randomInt(4); // select beginning screen side
71 int x = (q == 0 ? randomInt(getWidth()) : (q == 1 ? getWidth() - sprite->getWidth() : (q == 2 ? randomInt(getWidth()) : 0)));
72 int y = (q == 0 ? 0 : (q == 1 ? randomInt(getHeight()) : (q == 2 ? getHeight() - sprite->getHeight() : randomInt(getHeight()))));
73 sprite->moveTo(x, y);
74 sprite->visible = true;
75 sprite->velX = - sprite->getWidth() / 4 + randomInt(sprite->getWidth() / 2);
76 sprite->velY = - sprite->getHeight() / 4 + randomInt(sprite->getHeight() / 2);
77 sprite->isAlive = true;
78 }
79
80 void init()
81 {
82 paintSpace();
83
84 // setup spaceship and asteroid sprites
85 for (int i = 0; i < OBJECTS_COUNT; ++i) {
86 MySprite * sprite = &objects_[i];
87 if (i == 0) {
88 // setup Jupiter sprite
89 sprite->addBitmap(&jupiter);
90 sprite->moveTo(0, getHeight() / 2);
91 } else if (i <= SPACESHIP_COUNT) {
92 // each spaceship sprite of type Ship has 9 bitmaps: one "spaceship" bitmap and eight "explosion[]" bitmaps
93 sprite->addBitmap(&spaceship)->addBitmap(explosion, 8);
94 sprite->isAsteroid = false;
95 addSprite(sprite); // add sprite to collision detector
96 startSprite(sprite);
97 } else {
98 // each spaceship sprite of type Asteroid has just one bitmap: "asteroid"
99 sprite->addBitmap(&asteroid);
100 sprite->isAsteroid = true;
101 addSprite(sprite); // add sprite to collision detector
102 startSprite(sprite);
103 }
104 }
105
106 DisplayController.setSprites(objects_, OBJECTS_COUNT);
107 }
108
109 void update(int updateCount)
110 {
111 for (int i = 1; i < OBJECTS_COUNT; ++i) {
112
113 MySprite * sprite = &objects_[i];
114
115 // just alive objects can be moved or checked for collisions
116 if (sprite->isAlive) {
117
118 // this object is alive, move to the next position
119 sprite->moveBy(sprite->velX, sprite->velY, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight());
120
121 // update the collision detector and check for a possible collision
122 updateSpriteAndDetectCollisions(sprite);
123
124 } else if ((updateCount % 8) == 0) {
125
126 // this sprite is dead, go to next frame (animate explosion) every 8 updates
127 sprite->nextFrame();
128 if (sprite->currentFrame == 0)
129 startSprite(sprite); // explosion animation ended, live again
130
131 }
132 }
133
134 // move Jupiter every 20 updates
135 if ((updateCount % 20) == 0)
136 objects_[0].moveBy(1, 0, DisplayController.getViewPortWidth(), DisplayController.getViewPortHeight());
137
138 DisplayController.refreshSprites();
139 }
140
141 void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
142 {
143 MySprite * a = (MySprite*) spriteA;
144 MySprite * b = (MySprite*) spriteB;
145 // two asteroids cannot colide
146 if (a->isAsteroid && b->isAsteroid)
147 return;
148 // collision ok, mark both objects as dead
149 a->isAlive = b->isAlive = false;
150 // if collided object is an asteroid no explosion is shown, just hide it. Otherwise go to next frame.
151 if (a->isAsteroid)
152 a->visible = false;
153 else
154 a->nextFrame();
155 if (b->isAsteroid)
156 b->visible = false;
157 else
158 b->nextFrame();
159 }
160
161 void paintSpace()
162 {
163 Canvas cv(&DisplayController);
164 cv.setBrushColor(Color::Black);
165 cv.clear();
166 cv.setPenColor(Color::White);
167 cv.setBrushColor(Color::White);
168 // far stars
169 cv.setPenColor(64, 64, 64);
170 for (int i = 0; i < 400; ++i)
171 cv.setPixel(randomInt(getWidth()), randomInt(getHeight()));
172 // near stars
173 cv.setPenColor(128, 128, 128);
174 for (int i = 0; i < 50; ++i)
175 cv.setPixel(randomInt(getWidth()), randomInt(getHeight()));
176 // galaxy
177 cv.drawBitmap(80, 35, &galaxy);
178 cv.drawBitmap(220, 130, &galaxy);
179 }
180
181};
182
183
184void setup()
185{
186 // just for debug
187
188 DisplayController.begin();
189 DisplayController.setResolution(VGA_320x200_75Hz);
190 //DisplayController.moveScreen(20, 0);
191 DisplayController.moveScreen(-8, 0);
192}
193
194
195void loop()
196{
197 MyScene scene;
198 scene.start();
199 vTaskSuspend(nullptr);
200}
201
202
203extern "C" void app_main()
204{
205 setup();
206 while (true)
207 loop();
208}
Scene is an abstract class that encapsulates the functionality of a scene (sprites,...
Definition scene.h:59
int getViewPortHeight()
Determines the height of the viewport.
int getViewPortWidth()
Determines the width of the viewport.
void setSprites(T *sprites, int count)
Sets the list of active sprites.
void refreshSprites()
Forces the sprites to be updated.
void begin(gpio_num_t redGPIO, gpio_num_t greenGPIO, gpio_num_t blueGPIO, gpio_num_t HSyncGPIO, gpio_num_t VSyncGPIO)
Initializer for 8 colors (5 GPIOs).
void moveScreen(int offsetX, int offsetY)
Moves the screen by the specified horizontal and vertical offsets.
VGA bitmapped display controller (64 colors).
Single include file of the FabGL library.
#define VGA_320x200_75Hz
Definition fabglconf.h:183
Represents a sprite.