Skip to content

Commit a905e41

Browse files
authored
Merge pull request #2407 from capdevon/capdevon-ParticleInfluencer
DefaultParticleInfluencer: incorrect cloning of variables
2 parents 3cc16ab + cd0eb24 commit a905e41

4 files changed

Lines changed: 88 additions & 23 deletions

File tree

jme3-core/src/main/java/com/jme3/effect/influencers/DefaultParticleInfluencer.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2021 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -40,6 +40,7 @@
4040
import com.jme3.math.FastMath;
4141
import com.jme3.math.Vector3f;
4242
import com.jme3.util.clone.Cloner;
43+
4344
import java.io.IOException;
4445

4546
/**
@@ -101,13 +102,10 @@ public void read(JmeImporter im) throws IOException {
101102

102103
@Override
103104
public DefaultParticleInfluencer clone() {
104-
try {
105-
DefaultParticleInfluencer clone = (DefaultParticleInfluencer) super.clone();
106-
clone.initialVelocity = initialVelocity.clone();
107-
return clone;
108-
} catch (CloneNotSupportedException e) {
109-
throw new AssertionError();
110-
}
105+
// Set up the cloner for the type of cloning we want to do.
106+
Cloner cloner = new Cloner();
107+
DefaultParticleInfluencer clone = cloner.clone(this);
108+
return clone;
111109
}
112110

113111
/**

jme3-core/src/main/java/com/jme3/effect/influencers/NewtonianParticleInfluencer.java

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2012 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -142,17 +142,6 @@ protected void applyVelocityVariation(Particle particle) {
142142
particle.velocity.addLocal(temp);
143143
}
144144

145-
@Override
146-
public NewtonianParticleInfluencer clone() {
147-
NewtonianParticleInfluencer result = new NewtonianParticleInfluencer();
148-
result.normalVelocity = normalVelocity;
149-
result.initialVelocity = initialVelocity;
150-
result.velocityVariation = velocityVariation;
151-
result.surfaceTangentFactor = surfaceTangentFactor;
152-
result.surfaceTangentRotation = surfaceTangentRotation;
153-
return result;
154-
}
155-
156145
@Override
157146
public void write(JmeExporter ex) throws IOException {
158147
super.write(ex);

jme3-core/src/main/java/com/jme3/effect/influencers/ParticleInfluencer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2009-2018 jMonkeyEngine
2+
* Copyright (c) 2009-2025 jMonkeyEngine
33
* All rights reserved.
44
*
55
* Redistribution and use in source and binary forms, with or without
@@ -42,7 +42,7 @@
4242
* An interface that defines the methods to affect initial velocity of the particles.
4343
* @author Marcin Roguski (Kaelthas)
4444
*/
45-
public interface ParticleInfluencer extends Savable, Cloneable, JmeCloneable {
45+
public interface ParticleInfluencer extends Savable, JmeCloneable {
4646

4747
/**
4848
* This method influences the particle.
@@ -57,7 +57,7 @@ public interface ParticleInfluencer extends Savable, Cloneable, JmeCloneable {
5757
* This method clones the influencer instance.
5858
* @return cloned instance
5959
*/
60-
public ParticleInfluencer clone();
60+
ParticleInfluencer clone();
6161

6262
/**
6363
* @param initialVelocity
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.jme3.effect.influencers;
2+
3+
import com.jme3.asset.AssetManager;
4+
import com.jme3.asset.DesktopAssetManager;
5+
import com.jme3.export.binary.BinaryExporter;
6+
import com.jme3.math.Vector3f;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
/**
11+
* Automated tests for the {@code ParticleInfluencer} class.
12+
*
13+
* @author capdevon
14+
*/
15+
public class ParticleInfluencerTest {
16+
17+
/**
18+
* Tests cloning, serialization and de-serialization of a {@code NewtonianParticleInfluencer}.
19+
*/
20+
@Test
21+
public void testNewtonianParticleInfluencer() {
22+
AssetManager assetManager = new DesktopAssetManager(true);
23+
24+
NewtonianParticleInfluencer inf = new NewtonianParticleInfluencer();
25+
inf.setNormalVelocity(1);
26+
inf.setSurfaceTangentFactor(0.5f);
27+
inf.setSurfaceTangentRotation(2.5f);
28+
inf.setInitialVelocity(new Vector3f(0, 1, 0));
29+
inf.setVelocityVariation(2f);
30+
31+
NewtonianParticleInfluencer clone = (NewtonianParticleInfluencer) inf.clone();
32+
assertEquals(inf, clone);
33+
Assert.assertNotSame(inf.temp, clone.temp);
34+
35+
NewtonianParticleInfluencer copy = BinaryExporter.saveAndLoad(assetManager, inf);
36+
assertEquals(inf, copy);
37+
}
38+
39+
private void assertEquals(NewtonianParticleInfluencer inf, NewtonianParticleInfluencer clone) {
40+
Assert.assertEquals(inf.getNormalVelocity(), clone.getNormalVelocity(), 0.001f);
41+
Assert.assertEquals(inf.getSurfaceTangentFactor(), clone.getSurfaceTangentFactor(), 0.001f);
42+
Assert.assertEquals(inf.getSurfaceTangentRotation(), clone.getSurfaceTangentRotation(), 0.001f);
43+
Assert.assertEquals(inf.getInitialVelocity(), clone.getInitialVelocity());
44+
Assert.assertEquals(inf.getVelocityVariation(), clone.getVelocityVariation(), 0.001f);
45+
}
46+
47+
/**
48+
* Tests cloning, serialization and de-serialization of a {@code RadialParticleInfluencer}.
49+
*/
50+
@Test
51+
public void testRadialParticleInfluencer() {
52+
AssetManager assetManager = new DesktopAssetManager(true);
53+
54+
RadialParticleInfluencer inf = new RadialParticleInfluencer();
55+
inf.setHorizontal(true);
56+
inf.setOrigin(new Vector3f(0, 1, 0));
57+
inf.setRadialVelocity(2f);
58+
inf.setInitialVelocity(new Vector3f(0, 1, 0));
59+
inf.setVelocityVariation(2f);
60+
61+
RadialParticleInfluencer clone = (RadialParticleInfluencer) inf.clone();
62+
assertEquals(inf, clone);
63+
Assert.assertNotSame(inf.temp, clone.temp);
64+
Assert.assertNotSame(inf.getOrigin(), clone.getOrigin());
65+
66+
RadialParticleInfluencer copy = BinaryExporter.saveAndLoad(assetManager, inf);
67+
assertEquals(inf, copy);
68+
}
69+
70+
private void assertEquals(RadialParticleInfluencer inf, RadialParticleInfluencer clone) {
71+
Assert.assertEquals(inf.isHorizontal(), clone.isHorizontal());
72+
Assert.assertEquals(inf.getOrigin(), clone.getOrigin());
73+
Assert.assertEquals(inf.getRadialVelocity(), clone.getRadialVelocity(), 0.001f);
74+
Assert.assertEquals(inf.getInitialVelocity(), clone.getInitialVelocity());
75+
Assert.assertEquals(inf.getVelocityVariation(), clone.getVelocityVariation(), 0.001f);
76+
}
77+
78+
}

0 commit comments

Comments
 (0)