MCPcopy Create free account
hub / github.com/RavEngine/RavEngine / stepParticles

Function stepParticles

deps/SDL2/Xcode-iOS/Demos/src/fireworks.c:76–152  ·  view source on GitHub ↗

steps each active particle by timestep deltaTime */

Source from the content-addressed store, hash-verified

74 steps each active particle by timestep deltaTime
75*/
76void
77stepParticles(double deltaTime)
78{
79 float deltaMilliseconds = deltaTime * 1000;
80 int i;
81 struct particle *slot = particles;
82 struct particle *curr = particles;
83 for (i = 0; i < num_active_particles; i++) {
84 /* is the particle actually active, or is it marked for deletion? */
85 if (curr->isActive) {
86 /* is the particle off the screen? */
87 if (curr->y > screen_h)
88 curr->isActive = 0;
89 else if (curr->y < 0)
90 curr->isActive = 0;
91 if (curr->x > screen_w)
92 curr->isActive = 0;
93 else if (curr->x < 0)
94 curr->isActive = 0;
95
96 /* step velocity, then step position */
97 curr->yvel += ACCEL * deltaMilliseconds;
98 curr->xvel += 0.0f;
99 curr->y += curr->yvel * deltaMilliseconds;
100 curr->x += curr->xvel * deltaMilliseconds;
101
102 /* particle behavior */
103 if (curr->type == emitter) {
104 /* if we're an emitter, spawn a trail */
105 spawnTrailFromEmitter(curr);
106 /* if we've reached our peak, explode */
107 if (curr->yvel > 0.0) {
108 explodeEmitter(curr);
109 }
110 } else {
111 float speed =
112 sqrt(curr->xvel * curr->xvel + curr->yvel * curr->yvel);
113 /* if wind resistance is not powerful enough to stop us completely,
114 then apply winde resistance, otherwise just stop us completely */
115 if (WIND_RESISTANCE * deltaMilliseconds < speed) {
116 float normx = curr->xvel / speed;
117 float normy = curr->yvel / speed;
118 curr->xvel -=
119 normx * WIND_RESISTANCE * deltaMilliseconds;
120 curr->yvel -=
121 normy * WIND_RESISTANCE * deltaMilliseconds;
122 } else {
123 curr->xvel = curr->yvel = 0; /* stop particle */
124 }
125
126 if (curr->color[3] <= deltaMilliseconds * 0.1275f) {
127 /* if this next step will cause us to fade out completely
128 then just mark for deletion */
129 curr->isActive = 0;
130 } else {
131 /* otherwise, let's fade a bit more */
132 curr->color[3] -= deltaMilliseconds * 0.1275f;
133 }

Callers 1

mainFunction · 0.85

Calls 3

spawnTrailFromEmitterFunction · 0.85
explodeEmitterFunction · 0.85
sqrtFunction · 0.50

Tested by

no test coverage detected