|
| 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.samples; |
| 23 | +import com.github.stephengold.joltjni.*; |
| 24 | +import com.github.stephengold.joltjni.enumerate.*; |
| 25 | +import com.github.stephengold.joltjni.operator.Op; |
| 26 | +/** |
| 27 | + * A line-for-line Java translation of the Jolt Physics pulley-constraint test. |
| 28 | + * <p> |
| 29 | + * Compare with the original by Jorrit Rouwe at |
| 30 | + * https://github.com/jrouwe/JoltPhysics/blob/master/Samples/Tests/Constraints/PulleyConstraintTest.cpp |
| 31 | + */ |
| 32 | +class PulleyConstraintTest extends Test{ |
| 33 | + |
| 34 | +void Initialize() |
| 35 | +{ |
| 36 | + // Floor |
| 37 | + CreateFloor(); |
| 38 | + |
| 39 | + // Variation 0: Max length (rope) |
| 40 | + // Variation 1: Fixed length (rigid rod) |
| 41 | + // Variation 2: Min/max length |
| 42 | + // Variation 3: With ratio (block and tackle) |
| 43 | + for (int variation = 0; variation < 4; ++variation) |
| 44 | + { |
| 45 | + RVec3 position1=new RVec3(-10, 10, -10.0f * variation); |
| 46 | + Body body1 = mBodyInterface.createBody(new BodyCreationSettings(new BoxShape(Vec3.sReplicate(0.5f)), position1, Quat.sIdentity(), EMotionType.Dynamic, Layers.MOVING)); |
| 47 | + mBodyInterface.addBody(body1.getId(), EActivation.Activate); |
| 48 | + |
| 49 | + RVec3 position2=new RVec3(10, 10, -10.0f * variation); |
| 50 | + Body body2 = mBodyInterface.createBody(new BodyCreationSettings(new BoxShape(Vec3.sReplicate(0.5f)), position2, Quat.sIdentity(), EMotionType.Dynamic, Layers.MOVING)); |
| 51 | + mBodyInterface.addBody(body2.getId(), EActivation.Activate); |
| 52 | + |
| 53 | + PulleyConstraintSettings settings=new PulleyConstraintSettings(); |
| 54 | + settings.setBodyPoint1 ( Op.add(position1 ,new Vec3(0, 0.5f, 0))); // Connect at the top of the block |
| 55 | + settings.setBodyPoint2 ( Op.add(position2 ,new Vec3(0, 0.5f, 0))); |
| 56 | + settings.setFixedPoint1 ( Op.add(settings.getBodyPoint1() ,new Vec3(0, 10, 0))); |
| 57 | + settings.setFixedPoint2 ( Op.add(settings.getBodyPoint2() ,new Vec3(0, 10, 0))); |
| 58 | + |
| 59 | + switch (variation) |
| 60 | + { |
| 61 | + case 0: |
| 62 | + // Can't extend but can contract |
| 63 | + break; |
| 64 | + |
| 65 | + case 1: |
| 66 | + // Fixed size |
| 67 | + settings.setMinLength ( settings.setMaxLength ( -1)); |
| 68 | + break; |
| 69 | + |
| 70 | + case 2: |
| 71 | + // With range |
| 72 | + settings.setMinLength ( 18.0f); |
| 73 | + settings.setMaxLength ( 22.0f); |
| 74 | + break; |
| 75 | + |
| 76 | + case 3: |
| 77 | + // With ratio |
| 78 | + settings.setRatio ( 4.0f); |
| 79 | + break; |
| 80 | + } |
| 81 | + |
| 82 | + mPhysicsSystem.addConstraint(settings.create(body1, body2)); |
| 83 | + } |
| 84 | +} |
| 85 | +} |
0 commit comments