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
|
//
// 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 NVIDIA CORPORATION 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 ``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.
//
// Copyright (c) 2008-2018 NVIDIA Corporation. All rights reserved.
// Copyright (c) 2004-2008 AGEIA Technologies, Inc. All rights reserved.
// Copyright (c) 2001-2004 NovodeX AG. All rights reserved.
#include "SamplePreprocessor.h"
#include "SampleBridges.h"
#include "SampleUtils.h"
#include "RendererMemoryMacros.h"
#include "KinematicPlatform.h"
#include "RenderMaterial.h"
#include "RenderBaseActor.h"
#include <SamplePlatform.h>
#include <SampleUserInput.h>
#include <SampleBaseInputEventIds.h>
#include "SampleBridgesInputEventIds.h"
#include "PxPhysicsAPI.h"
#include "extensions/PxExtensionsAPI.h"
#include "PsMathUtils.h"
#ifdef CCT_ON_BRIDGES
#include "characterkinematic/PxControllerManager.h"
#include "SampleCCTActor.h"
#include "SampleCCTCameraController.h"
#define CONTACT_OFFSET 0.01f
// #define CONTACT_OFFSET 0.1f
// #define STEP_OFFSET 0.01f
#define STEP_OFFSET 0.05f
// #define STEP_OFFSET 0.1f
// #define STEP_OFFSET 0.2f
// #define SLOPE_LIMIT 0.8f
#define SLOPE_LIMIT 0.0f
// #define INVISIBLE_WALLS_HEIGHT 6.0f
#define INVISIBLE_WALLS_HEIGHT 0.0f
// #define MAX_JUMP_HEIGHT 4.0f
#define MAX_JUMP_HEIGHT 0.0f
static const float gScaleFactor = 1.5f;
static const float gStandingSize = 1.0f * gScaleFactor;
static const float gCrouchingSize = 0.25f * gScaleFactor;
static const float gControllerRadius = 0.3f * gScaleFactor;
const char* gPlankName = "Plank";
const char* gPlatformName = "Platform";
#endif
using namespace SampleRenderer;
using namespace SampleFramework;
REGISTER_SAMPLE(SampleBridges, "SampleBridges")
//using namespace physx;
static const bool gBreakableBridges = false;
static const PxReal gBreakForce = 1.0f;
//static const PxReal gPlatformSpeed = 100.0f;
static const PxReal gPlatformSpeed = 10.0f;
//static const PxReal gPlatformSpeed = 5.0f;
static const PxReal gGlobalScale = 2.0f;
static const PxReal gBoxUVs[] =
{
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
0.0f, 180.0f/256.0f,
0.0f, 0.0f,
1.0f, 0.0f,
1.0f, 180.0f/256.0f,
};
///////////////////////////////////////////////////////////////////////////////
// PT: TODO: use PhysXSampleApplication helper for this
static PxRigidActor* createRigidActor( PxScene& scene, PxPhysics& physics,
const PxTransform& pose, const PxGeometry& geometry, PxMaterial& material,
const PxFilterData* fd, const PxReal* density, const PxReal* mass, PxU32 flags)
{
const bool isDynamic = (density && *density) || (mass && *mass);
PxRigidActor* actor = isDynamic ? static_cast<PxRigidActor*>(physics.createRigidDynamic(pose))
: static_cast<PxRigidActor*>(physics.createRigidStatic(pose));
if(!actor)
return NULL;
PxShape* shape = PxRigidActorExt::createExclusiveShape(*actor, geometry, material);
if(!shape)
{
actor->release();
return NULL;
}
if(fd)
shape->setSimulationFilterData(*fd);
if(isDynamic)
{
PxRigidDynamic* body = static_cast<PxRigidDynamic*>(actor);
{
if(density)
PxRigidBodyExt::updateMassAndInertia(*body, *density);
else
PxRigidBodyExt::setMassAndUpdateInertia(*body, *mass);
}
}
scene.addActor(*actor);
return actor;
}
static PxRigidActor* createBox( PxScene& scene, PxPhysics& physics,
const PxTransform& pose, const PxVec3& dimensions, const PxReal density, PxMaterial& m, const PxFilterData* fd, PxU32 flags)
{
return createRigidActor(scene, physics, pose, PxBoxGeometry(dimensions), m, fd, &density, NULL, flags);
}
enum BridgeJoint
{
BRIDGE_REVOLUTE,
BRIDGE_SPHERICAL,
BRIDGE_FIXED,
BRIDGE_COUNT
};
// PT: TODO: share this function with original code in Visual Tests?
static void buildBridge(PxScene& scene, PxPhysics& physics, PxMaterial& material, std::vector<PxRigidActor*>& actors, std::vector<PxJoint*>& joints, const PxTransform& tr, PxReal scale, BridgeJoint type)
{
const PxVec3 offset(0,0,0);
const PxReal groundLength = 5.0f;
const PxReal groundHeight = 5.0f;
// create ground under character
if(1)
{
PxTransform boxPose = tr * PxTransform(PxVec3(0.0f, -groundHeight, -10.0f-groundLength)*scale, PxQuat(PxIdentity));
boxPose.p += offset;
const PxVec3 boxSize = PxVec3(1.0f, groundHeight, groundLength) * scale;
PxRigidActor* box = createBox(scene, physics, boxPose, boxSize, 0.0f, material, NULL, 0);
actors.push_back(box);
}
// create bridge...
if(1)
{
const PxReal plankWidth = 1.0f;
const PxReal plankHeight = 0.02f;
const PxReal plankDepth = 0.5f;
const PxReal plankDensity = 0.03f;
// const PxReal plankDensity = 0.3f;
PxRigidDynamic* lastPlank = 0;
for(PxReal z = -10.0f+plankDepth; z<10.0f; z+= plankDepth*2)
{
const PxVec3 boxSize = PxVec3(plankWidth, plankHeight, plankDepth) * scale;
PxTransform boxPose = tr * PxTransform(PxVec3(0,-plankHeight,z)*scale);
PxRigidDynamic* plank = createBox(scene, physics, boxPose, boxSize, plankDensity, material, NULL, 0)->is<PxRigidDynamic>();
if(plank)
{
#ifdef CCT_ON_BRIDGES
plank->setName(gPlankName);
#endif
actors.push_back(plank);
//plank->setSolverIterationCounts(1, 1);
// plank->setSolverIterationCounts(255, 255);
// plank->setSolverIterationCounts(10, 10);
plank->setSolverIterationCounts(8, 8);
plank->setLinearDamping(1.0f);
plank->setAngularDamping(1.0f);
// plank->putToSleep();
if(!lastPlank)
{
// plank = first plank...
plank->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
}
else
{
// joint together plank and lastPlank...
if(type==BRIDGE_REVOLUTE)
{
PxRevoluteJoint* j = PxRevoluteJointCreate(physics,
lastPlank, PxTransform(PxVec3(0,0, plankDepth)*scale),
plank, PxTransform(PxVec3(0,0,-plankDepth)*scale));
if(j)
{
j->setProjectionLinearTolerance(0.5f);
joints.push_back(j);
}
}
else if(type==BRIDGE_FIXED)
{
PxFixedJoint* j = PxFixedJointCreate(physics,
lastPlank, PxTransform(PxVec3(0,0, plankDepth)*scale),
plank, PxTransform(PxVec3(0,0,-plankDepth)*scale));
if(j)
{
j->setProjectionLinearTolerance(0.5f);
joints.push_back(j);
}
}
else if(type==BRIDGE_SPHERICAL)
{
const PxReal plankX = 0.1f;
PxSphericalJoint* j1 = PxSphericalJointCreate(physics,
lastPlank, PxTransform(PxVec3(plankX, 0.0f, plankDepth)*scale),
plank, PxTransform(PxVec3(plankX, 0.0f, -plankDepth)*scale));
if(j1)
{
j1->setProjectionLinearTolerance(0.5f);
joints.push_back(j1);
}
PxSphericalJoint* j2 = PxSphericalJointCreate(physics,
lastPlank, PxTransform(PxVec3(-plankX, 0.0f, plankDepth)*scale),
plank, PxTransform(PxVec3(-plankX, 0.0f, -plankDepth)*scale));
if(j2)
{
j2->setProjectionLinearTolerance(0.5f);
joints.push_back(j2);
}
}
else PX_ASSERT(0);
}
}
if(gBreakableBridges)
{
for(PxU32 i=0;i<joints.size();i++)
joints[i]->setBreakForce(gBreakForce * scale, gBreakForce * scale);
}
lastPlank = plank;
}
if(lastPlank)
{
lastPlank->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
}
}
// create ground after bridge
if(1)
{
PxTransform boxPose = tr * PxTransform(PxVec3(0.0f, -groundHeight, 10.0f+groundLength)*scale);
boxPose.p += offset;
const PxVec3 boxSize = PxVec3(1.0f, groundHeight, groundLength) * scale;
PxRigidActor* box = createBox(scene, physics, boxPose, boxSize, 0.0f, material, NULL, 0);
actors.push_back(box);
}
}
///////////////////////////////////////////////////////////////////////////////
SampleBridges::SampleBridges(PhysXSampleApplication& app) :
PhysXSample (app),
#ifdef CCT_ON_BRIDGES
mCCTCamera (NULL),
mActor (NULL),
mControllerManager (NULL),
#endif
#ifdef PLATFORMS_AS_OBSTACLES
mObstacleContext (NULL),
mBoxObstacles (NULL),
#endif
mGlobalScale (gGlobalScale),
mCurrentPlatform (0xffffffff),
mElapsedRenderTime (0.0f),
mRockMaterial (NULL),
mWoodMaterial (NULL),
mPlatformMaterial (NULL),
mBomb (NULL),
mBombTimer (0.0f),
mWaterY (0.0f),
mMustResync (false),
mEnableCCTDebugRender (false)
{
#ifdef CCT_ON_BRIDGES
mControllerInitialPosition = PxExtendedVec3(18.0f, 2.0f, -64.0f);
#endif
mCreateGroundPlane = false;
//mStepperType = FIXED_STEPPER;
mDefaultDensity = 0.1f;
// mDefaultDensity = 1.0f;
}
SampleBridges::~SampleBridges()
{
#ifdef CCT_ON_BRIDGES
DELETESINGLE(mActor);
#endif
}
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::customizeSample(SampleSetup& setup)
{
setup.mName = "SampleBridges";
}
///////////////////////////////////////////////////////////////////////////////
// PT: this is called from the RAW-loader when loading a scene. The default behavior
// in PhysXSampleApplication creates a physics and a graphics mesh for each exported object. We
// intercept the call here to disable the physics mesh for the "water plane".
void SampleBridges::newMesh(const RAWMesh& data)
{
if(data.mName && (Ps::strcmp(data.mName, "WaterPlane")==0 || Ps::strcmp(data.mName, "skyshape")==0))
{
// PT: create render mesh only (not physics) for the waterplane & skyboxes
createRenderMeshFromRawMesh(data);
if(Ps::strcmp(data.mName, "WaterPlane")==0)
mWaterY = data.mTransform.p.y;
return;
}
PhysXSample::newMesh(data);
}
// PT: this is a callback from the RAW-loader, used to import a new shape. We use
// simple segment shapes in the MAX scene to define where our bridges should be.
void SampleBridges::newShape(const RAWShape& data)
{
if(data.mName)
{
if(::strcmp(data.mName, "Bridge")==0)
{
PX_ASSERT(data.mNbVerts==2);
const PxVec3 p0 = data.mTransform.transform(data.mVerts[0]);
const PxVec3 p1 = data.mTransform.transform(data.mVerts[1]);
const PxVec3 center = (p0 + p1)*0.5f;
PxVec3 dir, right, up;
Ps::computeBasis(p0, p1, dir, right, up);
PxMat33 m(right, up, dir);
PxTransform tr;
tr.p = center;
tr.q = PxQuat(m);
const float l = (p1 - p0).magnitude();
const float coeff = l / 80.0f;
float scale = 2.0f * coeff;
// float scale = 4.0f * coeff;
// float scale = 1.0f;
BridgeJoint type = BRIDGE_REVOLUTE;
{
static int count=0;
if(count==0)
type = BRIDGE_REVOLUTE;
else if(count==1)
type = BRIDGE_SPHERICAL;
else if(count==2)
type = BRIDGE_FIXED;
else PX_ASSERT(0);
count++;
if(count==BRIDGE_COUNT)
count = 0;
}
std::vector<PxRigidActor*> bridgeActors;
buildBridge(getActiveScene(), getPhysics(), getDefaultMaterial(), bridgeActors, mJoints, tr, scale, type);
for(PxU32 i=0;i<bridgeActors.size();i++)
{
RenderMaterial* m = (i==0 || i==bridgeActors.size()-1) ? mRockMaterial : mWoodMaterial;
PX_ASSERT(bridgeActors[i]->getNbShapes()==1);
PxShape* boxShape;
PxU32 nb = bridgeActors[i]->getShapes(&boxShape, 1);
PX_ASSERT(nb==1);
PX_UNUSED(nb);
createRenderBoxFromShape(bridgeActors[i], boxShape, m, gBoxUVs);
}
}
else if(::strcmp(data.mName, "Platform")==0)
{
const PxTransform idt = PxTransform(PxIdentity);
PxRigidDynamic* platformActor = (PxRigidDynamic*)::createBox(getActiveScene(), getPhysics(), idt, gGlobalScale * PxVec3(1.0f, 5.0f, 5.0f), 1.0f, getDefaultMaterial(), NULL, 0);
#ifdef CCT_ON_BRIDGES
platformActor->setName(gPlatformName);
#endif
platformActor->setRigidBodyFlag(PxRigidBodyFlag::eKINEMATIC, true);
// createRenderObjectsFromActor(platformActor, mPlatformMaterial);
PxShape* platformShape = NULL;
platformActor->getShapes(&platformShape, 1);
PX_ASSERT(platformShape);
RenderBaseActor* renderActor = createRenderObjectFromShape(platformActor, platformShape, mPlatformMaterial);
#ifdef PLATFORMS_AS_OBSTACLES
// platformShape->userData = NULL;
renderActor->setPhysicsShape(NULL, NULL);
PxBounds3 maxBounds;
maxBounds.setMaximal();
renderActor->setWorldBounds(maxBounds);
#else
PX_UNUSED(renderActor);
#endif
#ifdef ROTATING_PLATFORMS
const PxF32 rotationSpeed = 0.5f;
#else
const PxF32 rotationSpeed = 0.0f;
#endif
const PxVec3 up(0.0f, 1.0f, 0.0f);
const PxQuat q = PxShortestRotation(PxVec3(1.0f, 0.0f, 0.0f), up);
mPlatformManager.createPlatform(data.mNbVerts, data.mVerts, data.mTransform, q, platformActor, gPlatformSpeed, rotationSpeed);
}
}
}
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::onInit()
{
PhysXSample::onInit();
PxSceneWriteLock scopedLock(*mScene);
mApplication.setMouseCursorHiding(true);
mApplication.setMouseCursorRecentering(true);
getRenderer()->setAmbientColor(RendererColor(170, 170, 170));
// mScene->setGravity(PxVec3(0.0f, -98.1f, 0.0f));
getActiveScene().setGravity(PxVec3(0.0f, -9.81f, 0.0f));
{
{
RAWTexture data;
data.mName = "rock_distance_diffuse.tga";
RenderTexture* rockTexture = createRenderTextureFromRawTexture(data);
mRockMaterial = SAMPLE_NEW(RenderMaterial)(*getRenderer(), PxVec3(1.0f, 1.0f, 1.0f), 1.0f, false, 0xffffffff, rockTexture);
mRenderMaterials.push_back(mRockMaterial);
}
{
RAWTexture data;
data.mName = "pier_diffuse.dds";
RenderTexture* woodTexture = createRenderTextureFromRawTexture(data);
mWoodMaterial = SAMPLE_NEW(RenderMaterial)(*getRenderer(), PxVec3(1.0f, 1.0f, 1.0f), 1.0f, false, 0xffffffff, woodTexture);
mRenderMaterials.push_back(mWoodMaterial);
}
{
RAWTexture data;
data.mName = "divingBoardFloor_diffuse.dds";
RenderTexture* platformTexture = createRenderTextureFromRawTexture(data);
mPlatformMaterial = SAMPLE_NEW(RenderMaterial)(*getRenderer(), PxVec3(1.0f, 1.0f, 1.0f), 1.0f, false, 0xffffffff, platformTexture);
mRenderMaterials.push_back(mPlatformMaterial);
}
importRAWFile("SampleBridges.raw", gGlobalScale);
importRAWFile("sky_mission_race1.raw", 1.0f);
}
#ifdef CCT_ON_BRIDGES
mControllerManager = PxCreateControllerManager(getActiveScene());
ControlledActorDesc desc;
desc.mType = PxControllerShapeType::eCAPSULE;
desc.mPosition = mControllerInitialPosition;
desc.mSlopeLimit = SLOPE_LIMIT;
desc.mContactOffset = CONTACT_OFFSET;
desc.mStepOffset = STEP_OFFSET;
desc.mInvisibleWallHeight = INVISIBLE_WALLS_HEIGHT;
desc.mMaxJumpHeight = MAX_JUMP_HEIGHT;
desc.mRadius = gControllerRadius;
desc.mHeight = gStandingSize;
desc.mCrouchHeight = gCrouchingSize;
desc.mReportCallback = this;
desc.mBehaviorCallback = this;
{
mActor = SAMPLE_NEW(ControlledActor)(*this);
mActor->init(desc, mControllerManager);
RenderBaseActor* actor0 = mActor->getRenderActorStanding();
RenderBaseActor* actor1 = mActor->getRenderActorCrouching();
if(actor0)
mRenderActors.push_back(actor0);
if(actor1)
mRenderActors.push_back(actor1);
}
mCCTCamera = SAMPLE_NEW(SampleCCTCameraController)(*this);
mCCTCamera->setControlled(&mActor, 0, 1);
// mCCTCamera->setFilterData();
mCCTCamera->setFilterCallback(this);
mCCTCamera->setGravity(-20.0f);
setCameraController(mCCTCamera);
mCCTCamera->setView(0,0);
#ifdef PLATFORMS_AS_OBSTACLES
mObstacleContext = mControllerManager->createObstacleContext();
mCCTCamera->setObstacleContext(mObstacleContext);
const PxVec3 up(0.0f, 1.0f, 0.0f);
const PxQuat q = PxShortestRotation(PxVec3(1.0f, 0.0f, 0.0f), up);
const PxU32 nbPlatforms = mPlatformManager.getNbPlatforms();
KinematicPlatform** platforms = mPlatformManager.getPlatforms();
mBoxObstacles = new PxBoxObstacle[nbPlatforms];
for(PxU32 i=0;i<nbPlatforms;i++)
{
mBoxObstacles[i].mPos.x = 0.0;
mBoxObstacles[i].mPos.y = 0.0;
mBoxObstacles[i].mPos.z = 0.0;
mBoxObstacles[i].mRot = q;
mBoxObstacles[i].mHalfExtents = gGlobalScale * PxVec3(1.0f, 5.0f, 5.0f);
// mBoxObstacles[i].mHalfExtents = gGlobalScale * PxVec3(5.0f, 1.0f, 5.0f);
const ObstacleHandle handle = mObstacleContext->addObstacle(mBoxObstacles[i]);
platforms[i]->setBoxObstacle(handle, mBoxObstacles + i);
}
#endif
// mCCTCamera->setCameraLinkToPhysics(true);
#endif
// mScene->setVisualizationParameter(PxVisualizationParameter::eCONTACT_NORMAL, 1.0f);
// mScene->setVisualizationParameter(PxVisualizationParameter::eJOINT_LOCAL_FRAMES, 1.0f);
// advanceSimulation(4.0f);
}
void SampleBridges::onShutdown()
{
{
PxSceneWriteLock scopedLock(*mScene);
#ifdef CCT_ON_BRIDGES
DELETESINGLE(mCCTCamera);
#ifdef PLATFORMS_AS_OBSTACLES
SAFE_RELEASE(mObstacleContext);
DELETEARRAY(mBoxObstacles);
#endif
mControllerManager->release();
#endif
const size_t nbJoints = mJoints.size();
for(PxU32 i=0;i<nbJoints;i++)
if(mJoints[i])
mJoints[i]->release();
mJoints.clear();
}
mPlatformManager.release();
PhysXSample::onShutdown();
}
// PT: very crude attempt here, just to have something running
static void applyBombForce(PxRigidActor* actor, const PxVec3& actorPos, const PxVec3& bombPos, PxReal bombRange)
{
PxRigidDynamic* dyna = actor->is<PxRigidDynamic>();
if(dyna)
{
PxVec3 dir = (actorPos - bombPos);
const PxReal m = dir.magnitude();
if(m<bombRange)
{
const PxReal coeff = (1.0f - m/bombRange) * 50.0f;
dir /= m;
// dyna->addForce(dir*coeff);
PxBounds3 worldBounds = dyna->getWorldBounds();
const PxVec3 center = worldBounds.getCenter();
const PxVec3 extents = worldBounds.getExtents();
const PxVec3 axis0(extents.x, 0.0f, 0.0f);
const PxVec3 axis1(0.0f, extents.y, 0.0f);
const PxVec3 axis2(0.0f, 0.0f, extents.z);
PxVec3 pts[8];
pts[0] = pts[3] = pts[4] = pts[7] = center - axis0;
pts[1] = pts[2] = pts[5] = pts[6] = center + axis0;
PxVec3 tmp = axis1 + axis2;
pts[0] -= tmp;
pts[1] -= tmp;
pts[6] += tmp;
pts[7] += tmp;
tmp = axis1 - axis2;
pts[2] += tmp;
pts[3] += tmp;
pts[4] -= tmp;
pts[5] -= tmp;
PxReal minDist = 1e+30f;
PxU32 index = 0;
for(PxU32 i=0;i<8;i++)
{
PxReal d2 = (pts[i] - bombPos).magnitudeSquared();
if(d2<minDist)
{
minDist = d2;
index = i;
}
}
PxRigidBodyExt::addForceAtPos(*dyna, dir*coeff, pts[index]);
// PxRigidBodyExt::addForceAtPos(*dyna, dir*coeff, actorPos);
}
}
}
template<class T>
bool findAndReplaceWithLast(std::vector<T*>& array, T* ptr)
{
const size_t s = array.size();
for(PxU32 j=0; j<s; j++)
{
if(array[j]==ptr)
{
T* last = array[s-1];
array.pop_back();
if(j!=s-1)
array[j] = last;
return true;
}
}
return false;
}
void SampleBridges::onTickPreRender(float dtime)
{
// shdfnd::printFormatted("SampleBridges::onTickPreRender\n");
const bool paused = isPaused();
// PT: keep track of elapsed render time
if(!paused)
mElapsedRenderTime += dtime;
#ifdef CCT_ON_BRIDGES
// const bool postUpdate = mCCTCamera->getCameraLinkToPhysics();
const bool postUpdate = true;
// const bool postUpdate = false;
if(!postUpdate && !mPause)
{
// shdfnd::printFormatted("CCT sync\n");
mActor->sync();
}
#endif
if(!paused)
{
// mPlatformManager.updatePhysicsPlatforms(dtime);
if(mBomb)
{
mBombTimer -= dtime;
if(mBombTimer<0.0f)
{
PxSceneWriteLock scopedLock(*mScene);
mBombTimer = 0.0f;
{
const PxVec3 bombPos = mBomb->getGlobalPose().p;
const PxReal bombActorRange = 30.0f;
const PxReal bombJointRange = 10.0f;
size_t nbJoints = mJoints.size();
for(PxU32 i=0;i<nbJoints;i++)
{
PxJoint* j = mJoints[i];
if(!j)
continue;
PxRigidActor* actor0;
PxRigidActor* actor1;
j->getActors(actor0, actor1);
const PxVec3 actorPos0 = actor0->getGlobalPose().p;
const PxVec3 actorPos1 = actor1->getGlobalPose().p;
applyBombForce(actor0, actorPos0, bombPos, bombActorRange);
applyBombForce(actor1, actorPos1, bombPos, bombActorRange);
const PxVec3 jointPos = (actorPos0 + actorPos1)*0.5f;
const PxReal d2 = (bombPos - jointPos).magnitudeSquared();
if(d2<bombJointRange*bombJointRange)
{
j->release();
mJoints[i] = NULL;
}
}
}
{
PxU32 nbShapes = mBomb->getNbShapes();
PxShape** shapes = (PxShape**)SAMPLE_ALLOC(sizeof(PxShape*)*nbShapes);
PxU32 nb = mBomb->getShapes(shapes, nbShapes);
PX_ASSERT(nb==nbShapes);
PX_UNUSED(nb);
for(PxU32 i=0;i<nbShapes;i++)
{
PxShape* currentShape = shapes[i];
RenderBaseActor* renderActor = getRenderActor(mBomb, currentShape);
unlink(renderActor, currentShape, mBomb);
findAndReplaceWithLast(mRenderActors, renderActor);
renderActor->release();
}
SAMPLE_FREE(shapes);
}
findAndReplaceWithLast(mPhysicsActors, mBomb);
SAFE_RELEASE(mBomb);
}
}
}
PhysXSample::onTickPreRender(dtime);
#ifdef PLATFORMS_AS_OBSTACLES
if(!paused)
updateRenderPlatforms(dtime);
#endif
#ifdef CCT_ON_BRIDGES
if(postUpdate && !mPause)
{
// shdfnd::printFormatted("CCT sync\n");
mActor->sync();
}
#endif
}
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::onTickPostRender(float dtime)
{
PhysXSample::onTickPostRender(dtime);
// PT: add CCT's internal debug rendering
if(mEnableCCTDebugRender)
{
mControllerManager->setDebugRenderingFlags(PxControllerDebugRenderFlag::eALL);
PxRenderBuffer& renderBuffer = mControllerManager->getRenderBuffer();
RenderPhysX3Debug* renderer = getDebugRenderer();
renderer->update(renderBuffer);
renderBuffer.clear();
}
else
{
mControllerManager->setDebugRenderingFlags(PxControllerDebugRenderFlag::eNONE);
}
#ifdef CCT_ON_BRIDGES
// mControllerManager->setDebugRenderingFlags(PxControllerDebugRenderFlag::eCACHED_BV|PxControllerDebugRenderFlag::eTEMPORAL_BV);
PxRenderBuffer& renderBuffer = mControllerManager->getRenderBuffer();
getDebugRenderer()->update(renderBuffer);
renderBuffer.clear();
#endif
const float lag = fabsf(mPlatformManager.getElapsedTime() - mElapsedRenderTime);
mMustResync = lag > 1.0f/60.0f;
if(0)
{
shdfnd::printFormatted("Render time: %f\n", mElapsedRenderTime);
shdfnd::printFormatted("Platform time: %f\n", mPlatformManager.getElapsedTime());
shdfnd::printFormatted("Delta: %f\n", lag);
}
}
///////////////////////////////////////////////////////////////////////////////
#ifdef PLATFORMS_AS_OBSTACLES
void SampleBridges::updateRenderPlatforms(float dtime)
{
// PT: compute new positions for (render) platforms, then move their corresponding obstacles to these positions.
const PxU32 nbPlatforms = mPlatformManager.getNbPlatforms();
KinematicPlatform** platforms = mPlatformManager.getPlatforms();
{
PxSceneReadLock scopedLock(*mScene);
for(PxU32 i=0;i<nbPlatforms;i++)
{
if(1)
{
PxRigidDynamic* actor = platforms[i]->getPhysicsActor();
PxShape* shape = NULL;
actor->getShapes(&shape, 1);
RenderBaseActor* renderActor = getRenderActor(actor, shape);
renderActor->setTransform(platforms[i]->getRenderPose());
}
platforms[i]->updateRender(dtime, mObstacleContext);
}
}
// PT: if render time & physics time varies too much, we must resync the physics to avoid large visual mismatchs
if(mMustResync)
{
PxSceneWriteLock scopedLock(*mScene);
// mElapsedPlatformTime = mElapsedRenderTime;
mPlatformManager.syncElapsedTime(mElapsedRenderTime);
// static int count=0; shdfnd::printFormatted("Resync %d\n", count++);
for(PxU32 i=0;i<nbPlatforms;i++)
{
PxRigidDynamic* actor = platforms[i]->getPhysicsActor();
actor->setKinematicTarget(platforms[i]->getRenderPose());
platforms[i]->resync();
}
}
}
#endif
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::onSubstep(float dtime)
{
// shdfnd::printFormatted("onSubstep\n");
mPlatformManager.updatePhysicsPlatforms(dtime);
}
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::helpRender(PxU32 x, PxU32 y, PxU8 textAlpha)
{
Renderer* renderer = getRenderer();
const PxU32 yInc = 18;
const PxReal scale = 0.5f;
const PxReal shadowOffset = 6.0f;
const RendererColor textColor(255, 255, 255, textAlpha);
const bool isMouseSupported = getApplication().getPlatform()->getSampleUserInput()->mouseSupported();
const bool isPadSupported = getApplication().getPlatform()->getSampleUserInput()->gamepadSupported();
const char* msg;
if (isMouseSupported && isPadSupported)
renderer->print(x, y += yInc, "Use mouse or right stick to rotate the camera", scale, shadowOffset, textColor);
else if (isMouseSupported)
renderer->print(x, y += yInc, "Use mouse to rotate the camera", scale, shadowOffset, textColor);
else if (isPadSupported)
renderer->print(x, y += yInc, "Use right stick to rotate the camera", scale, shadowOffset, textColor);
if (isPadSupported)
renderer->print(x, y += yInc, "Use left stick to move",scale, shadowOffset, textColor);
msg = mApplication.inputMoveInfoMsg("Press "," to move", CAMERA_MOVE_FORWARD,CAMERA_MOVE_BACKWARD, CAMERA_MOVE_LEFT, CAMERA_MOVE_RIGHT);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor);
msg = mApplication.inputInfoMsg("Press "," to move fast", CAMERA_SHIFT_SPEED, -1);
if(msg)
renderer->print(x, y += yInc, msg, scale, shadowOffset, textColor);
msg = mApplication.inputInfoMsg("Press "," to jump", CAMERA_JUMP,-1);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor);
msg = mApplication.inputInfoMsg("Press "," to crouch", CAMERA_CROUCH,-1);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor); ;
msg = mApplication.inputInfoMsg("Press "," to throw an object", SPAWN_DEBUG_OBJECT, -1);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor); ;
msg = mApplication.inputInfoMsg("Press "," to reset CCT position", RETRY,-1);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor);
msg = mApplication.inputInfoMsg("Press "," to teleport from one platform to another", TELEPORT,-1);
if(msg)
renderer->print(x, y += yInc, msg,scale, shadowOffset, textColor);
}
void SampleBridges::descriptionRender(PxU32 x, PxU32 y, PxU8 textAlpha)
{
bool print=(textAlpha!=0.0f);
if(print)
{
Renderer* renderer = getRenderer();
const PxU32 yInc = 24;
const PxReal scale = 0.5f;
const PxReal shadowOffset = 6.0f;
const RendererColor textColor(255, 255, 255, textAlpha);
char line0[256]="This sample demonstrates the configuration and run-time control of the";
char line1[256]="PhysX kinematic character controller (cct). In particular, the";
char line2[256]="interaction between the cct and both kinematic and dynamic scene objects";
char line3[256]="is presented. Jointed bridges with a variety of configurations are also";
char line4[256]="added to the scene to illustrate the setup code and behavior of some of";
char line5[256]="the different joint types supported by the PhysX SDK.";
renderer->print(x, y+=yInc, line0, scale, shadowOffset, textColor);
renderer->print(x, y+=yInc, line1, scale, shadowOffset, textColor);
renderer->print(x, y+=yInc, line2, scale, shadowOffset, textColor);
renderer->print(x, y+=yInc, line3, scale, shadowOffset, textColor);
renderer->print(x, y+=yInc, line4, scale, shadowOffset, textColor);
renderer->print(x, y+=yInc, line5, scale, shadowOffset, textColor);
}
}
///////////////////////////////////////////////////////////////////////////////
void SampleBridges::customizeRender()
{
// PT: add a simple screenquad when underwater
if(getCamera().getPos().y<mWaterY)
{
const RendererColor color(0, 90, 90);
ScreenQuad sq;
sq.mLeftUpColor = color;
sq.mRightUpColor = color;
sq.mLeftDownColor = color;
sq.mRightDownColor = color;
sq.mAlpha = 0.75;
getRenderer()->drawScreenQuad(sq);
}
}
|