Skip to content

Commit 681d127

Browse files
orwell96paramat
authored andcommitted
Particles: Make attached particle spawners respect the parent's yaw
Position, velocity and acceleration vectors of particles are rotated by the yaw of the parent object so that they are truly relative to it. Clarify new attached particle spawner behavior in lua_api.txt.
1 parent 4a0a672 commit 681d127

File tree

5 files changed

+37
-10
lines changed

5 files changed

+37
-10
lines changed

doc/lua_api.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -4152,7 +4152,8 @@ The Biome API is still in an experimental phase and subject to change.
41524152
-- ^ collision_removal: if true then particle is removed when it collides,
41534153
-- ^ requires collisiondetection = true to have any effect
41544154
attached = ObjectRef,
4155-
-- ^ attached: if defined, makes particle positions relative to this object.
4155+
-- ^ attached: if defined, particle positions, velocities and accelerations
4156+
-- ^ are relative to this object's position and yaw.
41564157
vertical = false,
41574158
-- ^ vertical: if true faces player using y axis only
41584159
texture = "image.png",

src/clientobject.h

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ClientActiveObject : public ActiveObject
6161
virtual bool getCollisionBox(aabb3f *toset){return false;}
6262
virtual bool collideWithObjects(){return false;}
6363
virtual v3f getPosition(){return v3f(0,0,0);}
64+
virtual float getYaw() const {return 0;}
6465
virtual scene::ISceneNode *getSceneNode(){return NULL;}
6566
virtual scene::IMeshSceneNode *getMeshSceneNode(){return NULL;}
6667
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode(){return NULL;}

src/content_cao.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ class ItemCAO : public ClientActiveObject
313313
{return &m_selection_box;}
314314
v3f getPosition()
315315
{return m_position;}
316-
316+
inline float getYaw() const
317+
{return 0;}
317318
std::string infoText()
318319
{return m_infotext;}
319320

src/content_cao.h

+4
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ class GenericCAO : public ClientActiveObject
136136
aabb3f *getSelectionBox();
137137

138138
v3f getPosition();
139+
inline float getYaw() const
140+
{
141+
return m_yaw;
142+
}
139143

140144
scene::ISceneNode *getSceneNode();
141145

src/particles.cpp

+28-8
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,17 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
253253
m_time += dtime;
254254

255255
bool unloaded = false;
256-
v3f attached_offset = v3f(0,0,0);
256+
bool is_attached = false;
257+
v3f attached_pos = v3f(0,0,0);
258+
float attached_yaw = 0;
257259
if (m_attached_id != 0) {
258-
if (ClientActiveObject *attached = env->getActiveObject(m_attached_id))
259-
attached_offset = attached->getPosition() / BS;
260-
else
260+
if (ClientActiveObject *attached = env->getActiveObject(m_attached_id)) {
261+
attached_pos = attached->getPosition() / BS;
262+
attached_yaw = attached->getYaw();
263+
is_attached = true;
264+
} else {
261265
unloaded = true;
266+
}
262267
}
263268

264269
if (m_spawntime != 0) // Spawner exists for a predefined timespan
@@ -277,8 +282,15 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
277282
v3f pos = random_v3f(m_minpos, m_maxpos);
278283
v3f vel = random_v3f(m_minvel, m_maxvel);
279284
v3f acc = random_v3f(m_minacc, m_maxacc);
280-
// Make relative to offest
281-
pos += attached_offset;
285+
286+
if (is_attached) {
287+
// Apply attachment yaw and position
288+
pos.rotateXZBy(attached_yaw);
289+
pos += attached_pos;
290+
vel.rotateXZBy(attached_yaw);
291+
acc.rotateXZBy(attached_yaw);
292+
}
293+
282294
float exptime = rand()/(float)RAND_MAX
283295
*(m_maxexptime-m_minexptime)
284296
+m_minexptime;
@@ -321,10 +333,18 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
321333
{
322334
if (rand()/(float)RAND_MAX < dtime)
323335
{
324-
v3f pos = random_v3f(m_minpos, m_maxpos)
325-
+ attached_offset;
336+
v3f pos = random_v3f(m_minpos, m_maxpos);
326337
v3f vel = random_v3f(m_minvel, m_maxvel);
327338
v3f acc = random_v3f(m_minacc, m_maxacc);
339+
340+
if (is_attached) {
341+
// Apply attachment yaw and position
342+
pos.rotateXZBy(attached_yaw);
343+
pos += attached_pos;
344+
vel.rotateXZBy(attached_yaw);
345+
acc.rotateXZBy(attached_yaw);
346+
}
347+
328348
float exptime = rand()/(float)RAND_MAX
329349
*(m_maxexptime-m_minexptime)
330350
+m_minexptime;

0 commit comments

Comments
 (0)