Skip to content

Commit 9543b84

Browse files
Pedro GimenoClobberXD
authored andcommittedAug 31, 2019
Fix rotation of attached particlespawner
Co-authored-by: ANAND <ClobberXD@gmail.com>
1 parent 0492565 commit 9543b84

File tree

5 files changed

+44
-41
lines changed

5 files changed

+44
-41
lines changed
 

‎src/client/clientobject.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ class ClientActiveObject : public ActiveObject
4848
virtual bool getCollisionBox(aabb3f *toset) const { return false; }
4949
virtual bool getSelectionBox(aabb3f *toset) const { return false; }
5050
virtual bool collideWithObjects() const { return false; }
51-
virtual v3f getPosition(){ return v3f(0,0,0); }
52-
virtual float getYaw() const { return 0; }
51+
virtual const v3f getPosition() const { return v3f(0.0f); }
5352
virtual scene::ISceneNode *getSceneNode() { return NULL; }
5453
virtual scene::IAnimatedMeshSceneNode *getAnimatedMeshSceneNode() { return NULL; }
5554
virtual bool isLocalPlayer() const {return false;}

‎src/client/content_cao.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ bool GenericCAO::getSelectionBox(aabb3f *toset) const
401401
return true;
402402
}
403403

404-
v3f GenericCAO::getPosition()
404+
const v3f GenericCAO::getPosition() const
405405
{
406406
if (getParent() != nullptr) {
407407
if (m_matrixnode)

‎src/client/content_cao.h

+8-5
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,9 @@ class GenericCAO : public ClientActiveObject
154154

155155
virtual bool getSelectionBox(aabb3f *toset) const;
156156

157-
v3f getPosition();
157+
const v3f getPosition() const;
158158

159-
inline const v3f &getRotation()
160-
{
161-
return m_rotation;
162-
}
159+
inline const v3f &getRotation() const { return m_rotation; }
163160

164161
const bool isImmortal();
165162

@@ -180,6 +177,12 @@ class GenericCAO : public ClientActiveObject
180177
return m_matrixnode->getRelativeTransformationMatrix();
181178
}
182179

180+
inline const core::matrix4 &getAbsolutePosRotMatrix() const
181+
{
182+
assert(m_matrixnode);
183+
return m_matrixnode->getAbsoluteTransformation();
184+
}
185+
183186
inline f32 getStepHeight() const
184187
{
185188
return m_prop.stepheight;

‎src/client/particles.cpp

+33-31
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2121
#include <cmath>
2222
#include "client.h"
2323
#include "collision.h"
24+
#include "client/content_cao.h"
2425
#include "client/clientevent.h"
2526
#include "client/renderingengine.h"
2627
#include "util/numeric.h"
@@ -38,9 +39,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
3839

3940
v3f random_v3f(v3f min, v3f max)
4041
{
41-
return v3f( rand()/(float)RAND_MAX*(max.X-min.X)+min.X,
42-
rand()/(float)RAND_MAX*(max.Y-min.Y)+min.Y,
43-
rand()/(float)RAND_MAX*(max.Z-min.Z)+min.Z);
42+
return v3f(
43+
rand() / (float)RAND_MAX * (max.X - min.X) + min.X,
44+
rand() / (float)RAND_MAX * (max.Y - min.Y) + min.Y,
45+
rand() / (float)RAND_MAX * (max.Z - min.Z) + min.Z);
4446
}
4547

4648
Particle::Particle(
@@ -299,16 +301,21 @@ ParticleSpawner::ParticleSpawner(
299301
}
300302

301303
void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
302-
bool is_attached, const v3f &attached_pos, float attached_yaw)
304+
const core::matrix4 *attached_absolute_pos_rot_matrix)
303305
{
304306
v3f ppos = m_player->getPosition() / BS;
305307
v3f pos = random_v3f(m_minpos, m_maxpos);
306308

307309
// Need to apply this first or the following check
308310
// will be wrong for attached spawners
309-
if (is_attached) {
310-
pos.rotateXZBy(attached_yaw);
311-
pos += attached_pos;
311+
if (attached_absolute_pos_rot_matrix) {
312+
pos *= BS;
313+
attached_absolute_pos_rot_matrix->transformVect(pos);
314+
pos /= BS;
315+
v3s16 camera_offset = m_particlemanager->m_env->getCameraOffset();
316+
pos.X += camera_offset.X;
317+
pos.Y += camera_offset.Y;
318+
pos.Z += camera_offset.Z;
312319
}
313320

314321
if (pos.getDistanceFrom(ppos) > radius)
@@ -317,18 +324,19 @@ void ParticleSpawner::spawnParticle(ClientEnvironment *env, float radius,
317324
v3f vel = random_v3f(m_minvel, m_maxvel);
318325
v3f acc = random_v3f(m_minacc, m_maxacc);
319326

320-
if (is_attached) {
321-
// Apply attachment yaw
322-
vel.rotateXZBy(attached_yaw);
323-
acc.rotateXZBy(attached_yaw);
327+
if (attached_absolute_pos_rot_matrix) {
328+
// Apply attachment rotation
329+
attached_absolute_pos_rot_matrix->rotateVect(vel);
330+
attached_absolute_pos_rot_matrix->rotateVect(acc);
324331
}
325332

326333
float exptime = rand() / (float)RAND_MAX
327-
* (m_maxexptime - m_minexptime)
328-
+ m_minexptime;
334+
* (m_maxexptime - m_minexptime)
335+
+ m_minexptime;
336+
329337
float size = rand() / (float)RAND_MAX
330-
* (m_maxsize - m_minsize)
331-
+ m_minsize;
338+
* (m_maxsize - m_minsize)
339+
+ m_minsize;
332340

333341
m_particlemanager->addParticle(new Particle(
334342
m_gamedef,
@@ -359,14 +367,10 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
359367
g_settings->getS16("max_block_send_distance") * MAP_BLOCKSIZE;
360368

361369
bool unloaded = false;
362-
bool is_attached = false;
363-
v3f attached_pos = v3f(0,0,0);
364-
float attached_yaw = 0;
365-
if (m_attached_id != 0) {
366-
if (ClientActiveObject *attached = env->getActiveObject(m_attached_id)) {
367-
attached_pos = attached->getPosition() / BS;
368-
attached_yaw = attached->getYaw();
369-
is_attached = true;
370+
const core::matrix4 *attached_absolute_pos_rot_matrix = nullptr;
371+
if (m_attached_id) {
372+
if (GenericCAO *attached = dynamic_cast<GenericCAO *>(env->getActiveObject(m_attached_id))) {
373+
attached_absolute_pos_rot_matrix = &attached->getAbsolutePosRotMatrix();
370374
} else {
371375
unloaded = true;
372376
}
@@ -382,7 +386,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
382386
// Pretend to, but don't actually spawn a particle if it is
383387
// attached to an unloaded object or distant from player.
384388
if (!unloaded)
385-
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
389+
spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
386390

387391
i = m_spawntimes.erase(i);
388392
} else {
@@ -398,7 +402,7 @@ void ParticleSpawner::step(float dtime, ClientEnvironment* env)
398402

399403
for (int i = 0; i <= m_amount; i++) {
400404
if (rand() / (float)RAND_MAX < dtime)
401-
spawnParticle(env, radius, is_attached, attached_pos, attached_yaw);
405+
spawnParticle(env, radius, attached_absolute_pos_rot_matrix);
402406
}
403407
}
404408
}
@@ -419,7 +423,7 @@ void ParticleManager::step(float dtime)
419423
stepSpawners (dtime);
420424
}
421425

422-
void ParticleManager::stepSpawners (float dtime)
426+
void ParticleManager::stepSpawners(float dtime)
423427
{
424428
MutexAutoLock lock(m_spawner_list_lock);
425429
for (auto i = m_particle_spawners.begin(); i != m_particle_spawners.end();) {
@@ -433,7 +437,7 @@ void ParticleManager::stepSpawners (float dtime)
433437
}
434438
}
435439

436-
void ParticleManager::stepParticles (float dtime)
440+
void ParticleManager::stepParticles(float dtime)
437441
{
438442
MutexAutoLock lock(m_particle_list_lock);
439443
for (auto i = m_particles.begin(); i != m_particles.end();) {
@@ -448,7 +452,7 @@ void ParticleManager::stepParticles (float dtime)
448452
}
449453
}
450454

451-
void ParticleManager::clearAll ()
455+
void ParticleManager::clearAll()
452456
{
453457
MutexAutoLock lock(m_spawner_list_lock);
454458
MutexAutoLock lock2(m_particle_list_lock);
@@ -457,9 +461,7 @@ void ParticleManager::clearAll ()
457461
m_particle_spawners.erase(i++);
458462
}
459463

460-
for(std::vector<Particle*>::iterator i =
461-
m_particles.begin();
462-
i != m_particles.end();)
464+
for(auto i = m_particles.begin(); i != m_particles.end();)
463465
{
464466
(*i)->remove();
465467
delete *i;

‎src/client/particles.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,7 @@ class ParticleSpawner
144144

145145
private:
146146
void spawnParticle(ClientEnvironment *env, float radius,
147-
bool is_attached, const v3f &attached_pos,
148-
float attached_yaw);
147+
const core::matrix4 *attached_absolute_pos_rot_matrix);
149148

150149
ParticleManager *m_particlemanager;
151150
float m_time;

0 commit comments

Comments
 (0)
Please sign in to comment.