|
| 1 | +/* |
| 2 | +Copyright (c) 2024 Stephen Gold |
| 3 | +
|
| 4 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +of this software and associated documentation files (the "Software"), to deal |
| 6 | +in the Software without restriction, including without limitation the rights |
| 7 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +copies of the Software, and to permit persons to whom the Software is |
| 9 | +furnished to do so, subject to the following conditions: |
| 10 | +
|
| 11 | +The above copyright notice and this permission notice shall be included in all |
| 12 | +copies or substantial portions of the Software. |
| 13 | +
|
| 14 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 20 | +SOFTWARE. |
| 21 | + */ |
| 22 | +package testjoltjni.app; |
| 23 | +import com.github.stephengold.joltjni.*; |
| 24 | +import com.github.stephengold.joltjni.enumerate.*; |
| 25 | +import testjoltjni.TestUtils; |
| 26 | + |
| 27 | +/** |
| 28 | + * A straightforward Java translation of the Jolt Physics "pyramid scene" |
| 29 | + * performance test. |
| 30 | + * <p> |
| 31 | + * Derived from PerformanceTest/PyramidScene.h by Jorrit Rouwe. |
| 32 | + * |
| 33 | + * @author Stephen Gold sgold@sonic.net |
| 34 | + */ |
| 35 | +// A scene that creates a pyramid of boxes to create a very large island |
| 36 | +public class PyramidScene |
| 37 | +{ |
| 38 | + private static final int OBJ_LAYER_NON_MOVING = 0; |
| 39 | + private static final int OBJ_LAYER_MOVING = 1; |
| 40 | + private static final int OBJ_NUM_LAYERS = 2; |
| 41 | + private static final int BP_LAYER_NON_MOVING = 0; |
| 42 | + private static final int BP_LAYER_MOVING = 1; |
| 43 | + private static final int BP_NUM_LAYERS = 2; |
| 44 | + public static void main(String[] argv) { |
| 45 | + TestUtils.loadAndInitializeNativeLibrary(); |
| 46 | + TempAllocatorImpl temp_allocator = new TempAllocatorImpl(32 << 20); // 32 MiB |
| 47 | + JobSystemThreadPool job_system = new JobSystemThreadPool(Jolt.cMaxPhysicsJobs, Jolt.cMaxPhysicsBarriers, TestUtils.numThreads()); |
| 48 | + final int cMaxBodies = 1_800; |
| 49 | + final int cNumBodyMutexes = 0; |
| 50 | + final int cMaxBodyPairs = 65_536; |
| 51 | + final int cMaxContactConstraints = 20_480; |
| 52 | + MapObj2Bp broad_phase_layer_interface = new MapObj2Bp(OBJ_NUM_LAYERS, BP_NUM_LAYERS) |
| 53 | + .add(OBJ_LAYER_NON_MOVING, BP_LAYER_NON_MOVING) |
| 54 | + .add(OBJ_LAYER_MOVING, BP_LAYER_MOVING); |
| 55 | + ObjVsBpFilter object_vs_broadphase_layer_filter = new ObjVsBpFilter(OBJ_NUM_LAYERS, BP_NUM_LAYERS) |
| 56 | + .disablePair(OBJ_LAYER_NON_MOVING, BP_LAYER_NON_MOVING); |
| 57 | + ObjVsObjFilter object_vs_object_layer_filter = new ObjVsObjFilter(OBJ_NUM_LAYERS) |
| 58 | + .disablePair(OBJ_LAYER_NON_MOVING, OBJ_LAYER_NON_MOVING); |
| 59 | + PhysicsSystem physics_system = new PhysicsSystem(); |
| 60 | + physics_system.init(cMaxBodies, cNumBodyMutexes, cMaxBodyPairs, cMaxContactConstraints, broad_phase_layer_interface, object_vs_broadphase_layer_filter, object_vs_object_layer_filter); |
| 61 | + StartTest(physics_system, EMotionQuality.LinearCast); |
| 62 | + final float cDeltaTime = 1.0f / 60.0f; |
| 63 | + physics_system.optimizeBroadPhase(); |
| 64 | + int step = 0; |
| 65 | + for (;;) { |
| 66 | + ++step; |
| 67 | + if (step < 300) break; |
| 68 | + final int cCollisionSteps = 1; |
| 69 | + physics_system.update(cDeltaTime, cCollisionSteps, temp_allocator, job_system); |
| 70 | + } |
| 71 | + TestUtils.cleanup(); |
| 72 | + } |
| 73 | + |
| 74 | + static void StartTest(PhysicsSystem inPhysicsSystem, EMotionQuality inMotionQuality) |
| 75 | + { |
| 76 | + BodyInterface bi = inPhysicsSystem.getBodyInterface(); |
| 77 | + |
| 78 | + // Floor |
| 79 | + bi.createAndAddBody(new BodyCreationSettings(new BoxShape(new Vec3(50.0f, 1.0f, 50.0f), 0.0f), new RVec3(new Vec3(0.0f, -1.0f, 0.0f)), Quat.sIdentity(), EMotionType.Static, OBJ_LAYER_NON_MOVING), EActivation.DontActivate); |
| 80 | + |
| 81 | + final float cBoxSize = 2.0f; |
| 82 | + final float cBoxSeparation = 0.5f; |
| 83 | + final float cHalfBoxSize = 0.5f * cBoxSize; |
| 84 | + final int cPyramidHeight = 15; |
| 85 | + |
| 86 | + ShapeRef box_shape = new BoxShape(Vec3.sReplicate(cHalfBoxSize), 0.0f).toRef(); // No convex radius to force more collisions |
| 87 | + |
| 88 | + // Pyramid |
| 89 | + for (int i = 0; i < cPyramidHeight; ++i) |
| 90 | + for (int j = i / 2; j < cPyramidHeight - (i + 1) / 2; ++j) |
| 91 | + for (int k = i / 2; k < cPyramidHeight - (i + 1) / 2; ++k) |
| 92 | + { |
| 93 | + RVec3 position = new RVec3(-cPyramidHeight + cBoxSize * j + (((i & 1)!=0)? cHalfBoxSize : 0.0f), 1.0f + (cBoxSize + cBoxSeparation) * i, -cPyramidHeight + cBoxSize * k + (((i & 1)!=0)? cHalfBoxSize : 0.0f)); |
| 94 | + BodyCreationSettings settings = new BodyCreationSettings(box_shape, position, Quat.sIdentity(), EMotionType.Dynamic, OBJ_LAYER_MOVING); |
| 95 | + settings.setAllowSleeping(false); // No sleeping to force the large island to stay awake |
| 96 | + bi.createAndAddBody(settings, EActivation.Activate); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments