-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathDacLinks.java
More file actions
1105 lines (999 loc) · 36 KB
/
DacLinks.java
File metadata and controls
1105 lines (999 loc) · 36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2018-2021 jMonkeyEngine
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.jme3.bullet.animation;
import com.jme3.anim.Armature;
import com.jme3.anim.Joint;
import com.jme3.anim.SkinningControl;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.collision.shapes.HullCollisionShape;
import com.jme3.bullet.joints.PhysicsJoint;
import com.jme3.bullet.joints.SixDofJoint;
import com.jme3.bullet.objects.PhysicsRigidBody;
import com.jme3.export.InputCapsule;
import com.jme3.export.JmeExporter;
import com.jme3.export.JmeImporter;
import com.jme3.export.OutputCapsule;
import com.jme3.export.Savable;
import com.jme3.math.Quaternion;
import com.jme3.math.Transform;
import com.jme3.math.Vector3f;
import com.jme3.scene.Mesh;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.util.clone.Cloner;
import java.io.IOException;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Access a DynamicAnimControl at the PhysicsLink level once it's been added to
* a Spatial.
* <p>
* This class is shared between JBullet and Native Bullet.
*
* @author Stephen Gold sgold@sonic.net
*
* Based on KinematicRagdollControl by Normen Hansen and Rémy Bouquet (Nehon).
*/
@Deprecated
public class DacLinks
extends DacConfiguration
implements PhysicsTickListener {
// *************************************************************************
// constants and loggers
/**
* message logger for this class
*/
final public static Logger logger3
= Logger.getLogger(DacLinks.class.getName());
/**
* local copy of {@link com.jme3.math.Quaternion#IDENTITY}
*/
final private static Quaternion rotateIdentity = new Quaternion();
/**
* local copy of {@link com.jme3.math.Transform#IDENTITY}
*/
final private static Transform transformIdentity = new Transform();
// *************************************************************************
// fields
/**
* false until the 1st physics tick, true thereafter, indicating that all
* links are ready for dynamic mode
*/
private boolean isReady = false;
/**
* bone links in a pre-order, depth-first traversal of the link hierarchy
*/
private List<BoneLink> boneLinkList = null;
/**
* map bone names to bone links
*/
private Map<String, BoneLink> boneLinks = new HashMap<>(32);
/**
* skeleton being controlled
*/
private Armature skeleton = null;
/**
* spatial that provides the mesh-coordinate transform
*/
private Spatial transformer = null;
/**
* torso link for this control
*/
private TorsoLink torsoLink = null;
// *************************************************************************
// constructors
/**
* Instantiate an enabled control without any linked bones (torso only).
*/
DacLinks() {
}
// *************************************************************************
// new methods exposed
/**
* Access the named bone.
* <p>
* Allowed only when the control IS added to a spatial.
*
* @param boneName the name of the skeleton bone to access
* @return the pre-existing instance, or null if not found
*/
public Joint findBone(String boneName) {
verifyAddedToSpatial("access a bone");
Joint result = skeleton.getJoint(boneName);
return result;
}
/**
* Access the BoneLink for the named bone. Returns null if bone is not
* linked, or if the control is not added to a spatial.
*
* @param boneName the name of the bone (not null, not empty)
* @return the pre-existing BoneLink, or null if not found
*/
public BoneLink findBoneLink(String boneName) {
BoneLink boneLink = boneLinks.get(boneName);
return boneLink;
}
/**
* Access the named link. Returns null if the name is invalid, or if the
* control is not added to a spatial.
*
* @param linkName the name of the link (not null, not empty)
* @return the pre-existing link, or null if not found
*/
public PhysicsLink findLink(String linkName) {
PhysicsLink link;
if (linkName.startsWith("Bone:")) {
String boneName = linkName.substring(5);
link = findBoneLink(boneName);
} else {
assert linkName.equals("Torso:");
link = torsoLink;
}
return link;
}
/**
* Access the skeleton. Returns null if the control is not added to a
* spatial.
*
* @return the pre-existing skeleton, or null
*/
public Armature getSkeleton() {
return skeleton;
}
/**
* Access the TorsoLink. Returns null if the control is not added to a
* spatial.
*
* @return the pre-existing TorsoLink, or null
*/
public TorsoLink getTorsoLink() {
return torsoLink;
}
/**
* Access the spatial with the mesh-coordinate transform. Returns null if
* the control is not added to a spatial.
*
* @return the pre-existing spatial, or null
*/
Spatial getTransformer() {
return transformer;
}
/**
* Test whether this control is ready for dynamic mode.
*
* @return true if ready, otherwise false
*/
public boolean isReady() {
return isReady;
}
/**
* Enumerate all physics links of the specified type managed by this
* control.
*
* @param <T> subclass of PhysicsLink
* @param linkType the subclass of PhysicsLink to search for (not null)
* @return a new array of links (not null, not empty)
*/
@SuppressWarnings("unchecked")
public <T extends PhysicsLink> List<T> listLinks(Class<T> linkType) {
int numLinks = countLinks();
List<T> result = new ArrayList<>(numLinks);
if (torsoLink != null
&& linkType.isAssignableFrom(torsoLink.getClass())) {
result.add((T) torsoLink);
}
for (BoneLink link : boneLinkList) {
if (linkType.isAssignableFrom(link.getClass())) {
result.add((T) link);
}
}
return result;
}
/**
* Enumerate all managed bones of the named link, in a pre-order,
* depth-first traversal of the skeleton, such that child bones never
* precede their ancestors.
*
* @param managerName the name of the managing link (not null)
* @return a new array of managed bones, including the manager if it is not
* the torso
*/
Joint[] listManagedBones(String managerName) {
List<Joint> list = new ArrayList<>(8);
if (torsoName.equals(managerName)) {
Joint[] roots = skeleton.getRoots();
for (Joint rootBone : roots) {
list.add(rootBone);
addUnlinkedDescendants(rootBone, list);
}
} else {
BoneLink manager = findBoneLink(managerName);
if (manager == null) {
String msg = "No link named " + managerName;
throw new IllegalArgumentException(msg);
}
Joint managerBone = manager.getBone();
list.add(managerBone);
addUnlinkedDescendants(managerBone, list);
}
/*
* Convert the list to an array.
*/
int numManagedBones = list.size();
Joint[] array = new Joint[numManagedBones];
list.toArray(array);
return array;
}
/**
* Enumerate all rigid bodies managed by this control.
* <p>
* Allowed only when the control IS added to a spatial.
*
* @return a new array of pre-existing rigid bodies (not null, not empty)
*/
public PhysicsRigidBody[] listRigidBodies() {
verifyAddedToSpatial("enumerate rigid bodies");
int numLinks = countLinks();
PhysicsRigidBody[] result = new PhysicsRigidBody[numLinks];
int linkIndex = 0;
if (torsoLink != null) {
result[0] = torsoLink.getRigidBody();
++linkIndex;
}
for (BoneLink boneLink : boneLinkList) {
result[linkIndex] = boneLink.getRigidBody();
++linkIndex;
}
assert linkIndex == numLinks;
return result;
}
/**
* Copy the model's mesh-to-world transform.
*
* @param storeResult storage for the result (modified if not null)
* @return the model's mesh transform (in world coordinates, either
* storeResult or a new transform, not null)
*/
Transform meshTransform(Transform storeResult) {
Transform result = transformer.getWorldTransform().clone();
return result;
}
/**
* Calculate the physics transform to match the specified skeleton bone.
*
* @param bone the skeleton bone to match (not null, unaffected)
* @param localOffset the location of the body's center (in the bone's local
* coordinates, not null, unaffected)
* @param storeResult storage for the result (modified if not null)
* @return the calculated physics transform (either storeResult or a new
* transform, not null)
*/
Transform physicsTransform(Joint bone, Vector3f localOffset,
Transform storeResult) {
Transform result
= (storeResult == null) ? new Transform() : storeResult;
/*
* Start with the body's transform in the bone's local coordinates.
*/
result.setTranslation(localOffset);
result.setRotation(rotateIdentity);
result.setScale(1f);
/*
* Convert to mesh coordinates.
*/
Transform localToMesh = bone.getModelTransform();
result.combineWithParent(localToMesh);
/*
* Convert to world (physics-space) coordinates.
*/
Transform meshToWorld = meshTransform(null);
result.combineWithParent(meshToWorld);
return result;
}
/**
* Rebuild the ragdoll. This is useful if you applied scale to the model
* after it was initialized.
* <p>
* Allowed only when the control IS added to a spatial.
*/
public void rebuild() {
verifyAddedToSpatial("rebuild the ragdoll");
Map<String, BoneLink> saveBones = new HashMap<>(boneLinks);
TorsoLink saveTorso = torsoLink;
Spatial controlledSpatial = getSpatial();
removeSpatialData(controlledSpatial);
createSpatialData(controlledSpatial);
for (Map.Entry<String, BoneLink> entry : boneLinks.entrySet()) {
String name = entry.getKey();
BoneLink newLink = entry.getValue();
BoneLink oldLink = saveBones.get(name);
newLink.postRebuild(oldLink);
}
if (torsoLink != null) {
torsoLink.postRebuild(saveTorso);
}
}
/**
* Alter the mass of the specified link.
*
* @param link the link to modify (not null)
* @param mass the desired mass (>0)
*/
public void setMass(PhysicsLink link, float mass) {
if (link instanceof BoneLink) {
String boneName = link.boneName();
setMass(boneName, mass);
} else {
assert link instanceof TorsoLink;
setMass(torsoName, mass);
}
}
/**
* Verify that this control is ready for dynamic mode, which implies that it
* is added to a Spatial.
*
* @param desiredAction (not null, not empty)
*/
public void verifyReadyForDynamicMode(String desiredAction) {
assert desiredAction != null;
verifyAddedToSpatial(desiredAction);
if (!isReady) {
String message = "Cannot " + desiredAction
+ " until the physics has been stepped.";
throw new IllegalStateException(message);
}
}
// *************************************************************************
// new protected methods
/**
* Access the list of bone links in a pre-order, depth-first traversal of
* the link hierarchy.
*
* @return the pre-existing list (not null)
*/
protected List<BoneLink> getBoneLinks() {
assert boneLinkList != null;
return boneLinkList;
}
/**
* Verify that this control is added to a Spatial.
*
* @param desiredAction (not null, not empty)
*/
protected void verifyAddedToSpatial(String desiredAction) {
assert desiredAction != null;
Spatial controlledSpatial = getSpatial();
if (controlledSpatial == null) {
String message = "Cannot " + desiredAction
+ " unless the Control is added to a Spatial.";
throw new IllegalStateException(message);
}
}
// *************************************************************************
// DacConfiguration methods
/**
* Add all managed physics objects to the PhysicsSpace.
*/
@Override
protected void addPhysics(PhysicsSpace space) {
Vector3f gravity = gravity(null);
PhysicsRigidBody rigidBody;
if (torsoLink != null) {
rigidBody = torsoLink.getRigidBody();
space.add(rigidBody);
rigidBody.setGravity(gravity);
}
for (BoneLink boneLink : boneLinkList) {
rigidBody = boneLink.getRigidBody();
space.add(rigidBody);
rigidBody.setGravity(gravity);
PhysicsJoint joint = boneLink.getJoint();
space.add(joint);
}
}
/**
* Callback from {@link com.jme3.util.clone.Cloner} to convert this
* shallow-cloned control into a deep-cloned one, using the specified cloner
* and original to resolve copied fields.
*
* @param cloner the cloner that's cloning this control (not null, modified)
* @param original the control from which this control was shallow-cloned
* (not null, unaffected)
*/
@Override
public void cloneFields(Cloner cloner, Object original) {
super.cloneFields(cloner, original);
DacLinks originalDac = (DacLinks) original;
boneLinkList = cloner.clone(boneLinkList);
boneLinks = new HashMap<>(32);
for (Map.Entry<String, BoneLink> entry
: originalDac.boneLinks.entrySet()) {
String boneName = entry.getKey();
BoneLink link = entry.getValue();
BoneLink copyLink = cloner.clone(link);
boneLinks.put(boneName, copyLink);
}
skeleton = cloner.clone(skeleton);
transformer = cloner.clone(transformer);
torsoLink = cloner.clone(torsoLink);
}
/**
* Create spatial-dependent data. Invoked each time the control is added to
* a spatial. Also invoked by {@link #rebuild()}.
*
* @param spatial the controlled spatial (not null)
*/
@Override
protected void createSpatialData(Spatial spatial) {
RagUtils.validate(spatial);
SkinningControl skeletonControl
= spatial.getControl(SkinningControl.class);
if (skeletonControl == null) {
throw new IllegalArgumentException(
"The controlled spatial must have a SkinningControl. "
+ "Make sure the control is there and not on a subnode.");
}
sortControls(skeletonControl);
skeletonControl.setHardwareSkinningPreferred(false);
/*
* Analyze the model's skeleton.
*/
skeleton = skeletonControl.getArmature();
validateSkeleton();
String[] tempManagerMap = managerMap(skeleton);
int numBones = skeleton.getJointCount();
/*
* Temporarily set all bones' local translations and rotations to bind.
*/
Transform[] savedTransforms = new Transform[numBones];
for (int boneIndex = 0; boneIndex < numBones; ++boneIndex) {
Joint bone = skeleton.getJoint(boneIndex);
savedTransforms[boneIndex] = bone.getLocalTransform().clone();
bone.applyBindPose(); // TODO adjust the scale?
}
skeleton.update();
/*
* Find the target meshes and choose the transform spatial.
*/
List<Mesh> targetList = RagUtils.listAnimatedMeshes(spatial, null);
Mesh[] targets = new Mesh[targetList.size()];
targetList.toArray(targets);
transformer = RagUtils.findAnimatedGeometry(spatial);
if (transformer == null) {
transformer = spatial;
}
/*
* Enumerate mesh-vertex coordinates and assign them to managers.
*/
Map<String, VectorSet> coordsMap
= RagUtils.coordsMap(targets, tempManagerMap);
/*
* Create the torso link.
*/
VectorSet vertexLocations = coordsMap.get(torsoName);
createTorsoLink(vertexLocations, targets);
/*
* Create bone links without joints.
*/
String[] linkedBoneNames = listLinkedBoneNames();
for (String boneName : linkedBoneNames) {
vertexLocations = coordsMap.get(boneName);
createBoneLink(boneName, vertexLocations);
}
int numLinkedBones = countLinkedBones();
assert boneLinks.size() == numLinkedBones;
/*
* Add joints to connect each BoneLink rigid body with its parent in the
* link hierarchy. Also initialize the boneLinkList.
*/
boneLinkList = new ArrayList<>(numLinkedBones);
addJoints(torsoLink);
assert boneLinkList.size() == numLinkedBones : boneLinkList.size();
/*
* Restore the skeleton's pose.
*/
for (int boneIndex = 0; boneIndex < numBones; ++boneIndex) {
Joint bone = skeleton.getJoint(boneIndex);
bone.setLocalTransform(savedTransforms[boneIndex]);
}
skeleton.update();
if (added) {
addPhysics(space);
}
logger3.log(Level.FINE, "Created ragdoll for skeleton.");
}
/**
* Create a shallow clone for the JME cloner.
*
* @return a new instance
*/
@Override
public DacLinks jmeClone() {
try {
DacLinks clone = (DacLinks) super.clone();
return clone;
} catch (CloneNotSupportedException exception) {
throw new RuntimeException(exception);
}
}
/**
* Read the mass of the named bone/torso.
*
* @param boneName the name of the bone/torso (not null)
* @return the mass (>0) or NaN if undetermined
*/
@Override
public float mass(String boneName) {
float mass;
if (getSpatial() == null) {
mass = super.mass(boneName);
} else if (torsoName.equals(boneName)) {
PhysicsRigidBody rigidBody = torsoLink.getRigidBody();
mass = rigidBody.getMass();
} else if (boneLinks.containsKey(boneName)) {
BoneLink link = boneLinks.get(boneName);
PhysicsRigidBody rigidBody = link.getRigidBody();
mass = rigidBody.getMass();
} else {
String msg = "No bone/torso named " + boneName;
throw new IllegalArgumentException(msg);
}
return mass;
}
/**
* De-serialize this control, for example when loading from a J3O file.
*
* @param im importer (not null)
* @throws IOException from importer
*/
@Override
@SuppressWarnings("unchecked")
public void read(JmeImporter im) throws IOException {
super.read(im);
InputCapsule ic = im.getCapsule(this);
boneLinkList
= ic.readSavableArrayList("boneLinkList", null);
for (BoneLink link : boneLinkList) {
String name = link.boneName();
boneLinks.put(name, link);
}
skeleton = (Armature) ic.readSavable("skeleton", null);
transformer = (Spatial) ic.readSavable("transformer", null);
torsoLink = (TorsoLink) ic.readSavable("torsoLink", null);
}
/**
* Remove all managed physics objects from the PhysicsSpace.
*/
@Override
protected void removePhysics(PhysicsSpace space) {
assert added;
PhysicsRigidBody rigidBody;
if (torsoLink != null) {
rigidBody = torsoLink.getRigidBody();
space.remove(rigidBody);
}
for (BoneLink boneLink : boneLinks.values()) {
rigidBody = boneLink.getRigidBody();
space.remove(rigidBody);
PhysicsJoint joint = boneLink.getJoint();
space.remove(joint);
}
}
/**
* Remove spatial-dependent data. Invoked each time this control is rebuilt
* or removed from a spatial.
*
* @param spat the previously controlled spatial (unused)
*/
@Override
protected void removeSpatialData(Spatial spat) {
if (added) {
removePhysics(space);
}
skeleton = null;
boneLinks.clear();
boneLinkList = null;
torsoLink = null;
transformer = null;
}
/**
* Alter the viscous damping ratio for all rigid bodies, including new ones.
*
* @param dampingRatio the desired damping ratio (non-negative, 0→no
* damping, 1→critically damped, default=0.6)
*/
@Override
public void setDamping(float dampingRatio) {
super.setDamping(dampingRatio);
if (getSpatial() != null) {
PhysicsRigidBody[] bodies = listRigidBodies();
for (PhysicsRigidBody rigidBody : bodies) {
rigidBody.setDamping(dampingRatio, dampingRatio);
}
}
}
/**
* Alter this control's gravitational acceleration for Ragdoll mode.
*
* @param gravity the desired acceleration vector (in physics-space
* coordinates, not null, unaffected, default=0,-9.8,0)
*/
@Override
public void setGravity(Vector3f gravity) {
super.setGravity(gravity);
if (getSpatial() != null) { // TODO make sure it's in ragdoll mode
PhysicsRigidBody[] bodies = listRigidBodies();
for (PhysicsRigidBody rigidBody : bodies) {
rigidBody.setGravity(gravity);
}
}
}
/**
* Alter the range of motion of the joint connecting the named BoneLink to
* its parent in the link hierarchy.
*
* @param boneName the name of the BoneLink (not null, not empty)
* @param rom the desired range of motion (not null)
*/
@Override
public void setJointLimits(String boneName, RangeOfMotion rom) {
if (!hasBoneLink(boneName)) {
String msg = "No linked bone named " + boneName;
throw new IllegalArgumentException(msg);
}
super.setJointLimits(boneName, rom);
if (getSpatial() != null) {
BoneLink boneLink = findBoneLink(boneName);
SixDofJoint joint = (SixDofJoint) boneLink.getJoint();
rom.setupJoint(joint);
}
}
/**
* Alter the mass of the named bone/torso.
*
* @param boneName the name of the bone, or torsoName (not null)
* @param mass the desired mass (>0)
*/
@Override
public void setMass(String boneName, float mass) {
super.setMass(boneName, mass);
if (getSpatial() != null) {
PhysicsRigidBody rigidBody;
if (torsoName.equals(boneName)) {
rigidBody = torsoLink.getRigidBody();
} else {
BoneLink link = findBoneLink(boneName);
rigidBody = link.getRigidBody();
}
rigidBody.setMass(mass);
}
}
/**
* Translate the torso to the specified location.
*
* @param vec desired location (not null, unaffected)
*/
@Override
protected void setPhysicsLocation(Vector3f vec) {
torsoLink.getRigidBody().setPhysicsLocation(vec);
}
/**
* Rotate the torso to the specified orientation.
*
* @param quat desired orientation (not null, unaffected)
*/
@Override
protected void setPhysicsRotation(Quaternion quat) {
torsoLink.getRigidBody().setPhysicsRotation(quat);
}
/**
* Update this control. Invoked once per frame during the logical-state
* update, provided the control is added to a scene. Do not invoke directly
* from user code.
*
* @param tpf the time interval between frames (in seconds, ≥0)
*/
@Override
public void update(float tpf) {
verifyAddedToSpatial("update the control");
if (!isEnabled()) {
return;
}
if (torsoLink != null) {
torsoLink.update(tpf);
}
for (BoneLink boneLink : boneLinkList) {
boneLink.update(tpf);
}
}
/**
* Serialize this control, for example when saving to a J3O file.
*
* @param ex exporter (not null)
* @throws IOException from exporter
*/
@Override
public void write(JmeExporter ex) throws IOException {
super.write(ex);
OutputCapsule oc = ex.getCapsule(this);
int count = countLinkedBones();
Savable[] savableArray = new Savable[count];
boneLinkList.toArray(savableArray);
oc.write(savableArray, "boneLinkList", null);
oc.write(skeleton, "skeleton", null);
oc.write(transformer, "transformer", null);
oc.write(torsoLink, "torsoLink", null);
}
// *************************************************************************
// PhysicsTickListener methods
/**
* Callback from Bullet, invoked just after the physics has been stepped.
* Used to re-activate any deactivated rigid bodies.
*
* @param space the space that was just stepped (not null)
* @param timeStep the time per physics step (in seconds, ≥0)
*/
@Override
public void physicsTick(PhysicsSpace space, float timeStep) {
assert space == getPhysicsSpace();
torsoLink.postTick();
for (BoneLink boneLink : boneLinkList) {
boneLink.postTick();
}
isReady = true;
}
/**
* Callback from Bullet, invoked just before the physics is stepped. A good
* time to clear/apply forces.
*
* @param space the space that is about to be stepped (not null)
* @param timeStep the time per physics step (in seconds, ≥0)
*/
@Override
public void prePhysicsTick(PhysicsSpace space, float timeStep) {
assert space == getPhysicsSpace();
torsoLink.preTick(timeStep);
for (BoneLink boneLink : boneLinkList) {
boneLink.preTick(timeStep);
}
}
// *************************************************************************
// private methods
/**
* Add joints to connect the named bone/torso link with each of its
* children. Also fill in the boneLinkList. Note: recursive!
*
* @param parentLink the parent bone/torso link (not null)
*/
private void addJoints(PhysicsLink parentLink) {
List<String> childNames = childNames(parentLink);
for (String childName : childNames) {
/*
* Add the joint and configure its range of motion.
* Also initialize the BoneLink's parent and its array
* of managed bones.
*/
BoneLink childLink = findBoneLink(childName);
childLink.addJoint(parentLink);
/*
* Add the BoneLink to the pre-order list.
*/
boneLinkList.add(childLink);
addJoints(childLink);
}
}
/**
* Enumerate all immediate child BoneLinks of the specified bone/torso link.
*
* @param link the bone/torso link (not null)
* @return a new list of bone names
*/
private List<String> childNames(PhysicsLink link) {
assert link != null;
String linkName;
if (link == torsoLink) {
linkName = torsoName;
} else {
linkName = link.boneName();
}
List<String> result = new ArrayList<>(8);
for (String childName : listLinkedBoneNames()) {
Joint bone = findBone(childName);
Joint parent = bone.getParent();
if (parent != null && findManager(parent).equals(linkName)) {
result.add(childName);
}
}
return result;
}
/**
* Create a jointless BoneLink for the named bone, and add it to the
* boneLinks map.
*
* @param boneName the name of the bone to be linked (not null)
* @param vertexLocations the set of vertex locations (not null, not empty)
*/
private void createBoneLink(String boneName, VectorSet vertexLocations) {
Joint bone = findBone(boneName);
Transform boneToMesh = bone.getModelTransform();
Transform meshToBone = boneToMesh.invert();
//logger3.log(Level.INFO, "meshToBone = {0}", meshToBone);
/*
* Create the CollisionShape and locate the center of mass.
*/
CollisionShape shape;
Vector3f center;
if (vertexLocations == null || vertexLocations.numVectors() == 0) {
throw new IllegalStateException("no vertex for " + boneName);
} else {
center = vertexLocations.mean(null);
center.subtractLocal(bone.getModelTransform().getTranslation());
shape = createShape(meshToBone, center, vertexLocations);
}
meshToBone.getTranslation().zero();
float mass = super.mass(boneName);
Vector3f offset = meshToBone.transformVector(center, null);
BoneLink link = new BoneLink(this, bone, shape, mass, offset);
boneLinks.put(boneName, link);
}
/**
* Create a CollisionShape for the specified transform, center, and vertex
* locations.
*
* @param vertexToShape the transform from vertex coordinates to de-scaled
* shape coordinates (not null, unaffected)
* @param center the location of the shape's center, in vertex coordinates
* (not null, unaffected)
* @param vertexLocations the set of vertex locations (not null, not empty,
* TRASHED)
* @return a new CollisionShape
*/
private CollisionShape createShape(Transform vertexToShape, Vector3f center,
VectorSet vertexLocations) {
int numVectors = vertexLocations.numVectors();
assert numVectors > 0 : numVectors;
Vector3f tempLocation = new Vector3f();
int numPoints = vertexLocations.numVectors();
float points[] = new float[3 * numPoints];
FloatBuffer buffer = vertexLocations.toBuffer();
buffer.rewind();
int floatIndex = 0;
while (buffer.hasRemaining()) {
tempLocation.x = buffer.get();
tempLocation.y = buffer.get();
tempLocation.z = buffer.get();
/*
* Translate so that vertex coordinates are relative to
* the shape's center.
*/
tempLocation.subtractLocal(center);
/*