Skip to content

Commit d87ebfc

Browse files
committed
Track children models when using parenting
1 parent 3ef01c1 commit d87ebfc

File tree

1 file changed

+20
-3
lines changed
  • api/src/main/java/com/flowpowered/caustic/api/model

1 file changed

+20
-3
lines changed

api/src/main/java/com/flowpowered/caustic/api/model/Model.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
*/
2424
package com.flowpowered.caustic.api.model;
2525

26-
import com.flowpowered.math.imaginary.Quaternionf;
27-
import com.flowpowered.math.matrix.Matrix4f;
28-
import com.flowpowered.math.vector.Vector3f;
26+
import java.util.HashSet;
27+
import java.util.Set;
2928

3029
import com.flowpowered.caustic.api.Material;
3130
import com.flowpowered.caustic.api.data.UniformHolder;
3231
import com.flowpowered.caustic.api.gl.VertexArray;
32+
import com.flowpowered.math.imaginary.Quaternionf;
33+
import com.flowpowered.math.matrix.Matrix4f;
34+
import com.flowpowered.math.vector.Vector3f;
3335

3436
/**
3537
* Represents a model. Each model has it's own position and rotation and set of uniforms. The vertex array provides the vertex data (mesh), while the material provides uniforms and textures for the
@@ -50,6 +52,7 @@ public class Model implements Comparable<Model> {
5052
private final UniformHolder uniforms = new UniformHolder();
5153
// Optional parent model
5254
private Model parent = null;
55+
private final Set<Model> children = new HashSet<>();
5356
private Matrix4f lastParentMatrix = null;
5457
private Matrix4f childMatrix = null;
5558

@@ -257,6 +260,15 @@ public Model getParent() {
257260
return parent;
258261
}
259262

263+
/**
264+
* Returns the children models.
265+
*
266+
* @return The children models
267+
*/
268+
public Set<Model> getChildren() {
269+
return children;
270+
}
271+
260272
/**
261273
* Sets the parent model. This model's position, rotation and scale will be relative to that model if not null.
262274
*
@@ -266,6 +278,11 @@ public void setParent(Model parent) {
266278
if (parent == this) {
267279
throw new IllegalArgumentException("The model can't be its own parent");
268280
}
281+
if (parent == null) {
282+
this.parent.children.remove(this);
283+
} else {
284+
parent.children.add(this);
285+
}
269286
this.parent = parent;
270287
}
271288

0 commit comments

Comments
 (0)