diff --git a/build.gradle b/build.gradle new file mode 100644 index 00000000..b68827ac --- /dev/null +++ b/build.gradle @@ -0,0 +1,29 @@ +// Gradle script to build jolt-jni Maven artifacts + +plugins { + id 'java-library' // to build JVM libraries +} + +ext { + group = 'com.github.stephengold' + artifact = 'joltjni' + version = '0.1.0' + baseName = "${artifact}-${version}" // for artifacts +} + +sourceSets.main.java { + srcDir 'src/main/java' +} + +java { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 +} + +tasks.withType(JavaCompile) { // Java compile-time options: + options.compilerArgs << '-Xdiags:verbose' + options.compilerArgs << '-Xlint:unchecked' + options.deprecation = true // to provide detailed deprecation warnings + options.encoding = 'UTF-8' +} + diff --git a/settings.gradle b/settings.gradle new file mode 100644 index 00000000..15e175c0 --- /dev/null +++ b/settings.gradle @@ -0,0 +1,11 @@ +// global build settings for the jolt-jni project + +pluginManagement { + repositories { + gradlePluginPortal() + } +} + +rootProject.name = 'jolt-jni' + +// no subprojects diff --git a/src/main/java/com/github/stephengold/joltjni/Body.java b/src/main/java/com/github/stephengold/joltjni/Body.java new file mode 100644 index 00000000..0dc8b4e2 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/Body.java @@ -0,0 +1,56 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * An object with mass, position, and shape that can be added to a + * {@code PhysicsSystem}. Bodies may be dynamic, kinematic, or static. + * + * @author Stephen Gold sgold@sonic.net + */ +public class Body extends NonCopyable { + // ************************************************************************* + // constructors + + Body(long bodyVa) { + super(bodyVa); + } + // ************************************************************************* + // new methods exposed + + /** + * Acquire the body's ID for use with {@code BodyInterface}. + * + * @return the value + */ + public BodyId getId() { + long bodyVa = va(); + long bodyIdVa = getId(bodyVa); + BodyId result = new BodyId(bodyIdVa); + + return result; + } + // ************************************************************************* + // native private methods + + native private static long getId(long bodyVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/BodyCreationSettings.java b/src/main/java/com/github/stephengold/joltjni/BodyCreationSettings.java new file mode 100644 index 00000000..53c6707c --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/BodyCreationSettings.java @@ -0,0 +1,98 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Settings used to create a {@code Body}. + * + * @author Stephen Gold sgold@sonic.net + */ +public class BodyCreationSettings extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate settings for the specified shape. + * + * @param shape the desired shape (not null) + * @param loc the desired location (not null, unaffected) + * @param orient the desired orientation (not null, unaffected) + * @param motionType the desired motion type (not null) + * @param objLayer the ID of the desired object layer + */ + public BodyCreationSettings(Shape shape, + RVec3 loc, Quat orient, EMotionType motionType, int objLayer) { + long shapeVa = shape.va(); + int motionTypeOrdinal = motionType.ordinal(); + long bodySettingsVa = createBodyCreationSettingsFromShape( + shapeVa, loc.xx(), loc.yy(), loc.zz(), + orient.getX(), orient.getY(), orient.getZ(), orient.getW(), + motionTypeOrdinal, objLayer); + setVirtualAddress(bodySettingsVa, true); + } + + /** + * Instantiate settings for the specified shape settings. + * + * @param shapeSettings the desired shape settings (not null) + * @param loc the desired location (not null, unaffected) + * @param orient the desired orientation (not null, unaffected) + * @param motionType the desired motion type (not null) + * @param objLayer the ID of the desired object layer + */ + public BodyCreationSettings(ShapeSettings shapeSettings, + RVec3 loc, Quat orient, EMotionType motionType, int objLayer) { + long shapeSettingsVa = shapeSettings.va(); + int motionTypeOrdinal = motionType.ordinal(); + long bodySettingsVa = createBodyCreationSettingsFromShapeSettings( + shapeSettingsVa, loc.xx(), loc.yy(), loc.zz(), + orient.getX(), orient.getY(), orient.getZ(), orient.getW(), + motionTypeOrdinal, objLayer); + setVirtualAddress(bodySettingsVa, true); + } + // ************************************************************************* + // JoltPhysicsObject methods + + @Override + public void close() { + if (ownsNativeObject()) { + long settingsVa = va(); + free(settingsVa); + } + + unassignNativeObject(); + } + // ************************************************************************* + // native private methods + + native private static long createBodyCreationSettingsFromShape( + long shapeVa, double locX, double locY, double locZ, + float qx, float qy, float qz, float qw, + int motionTypeOrdinal, int objLayer); + + native private static long createBodyCreationSettingsFromShapeSettings( + long shapeSettingsVa, double locX, double locY, double locZ, + float qx, float qy, float qz, float qw, + int motionTypeOrdinal, int objLayer); + + native private static void free(long bodySettingsVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/BodyId.java b/src/main/java/com/github/stephengold/joltjni/BodyId.java new file mode 100644 index 00000000..6a7b7c3d --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/BodyId.java @@ -0,0 +1,36 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Identify a particular {@code Body} to a {@code BodyInterface}. + * + * @author Stephen Gold sgold@sonic.net + */ +public class BodyId extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + BodyId(long bodyIdVa) { + super(bodyIdVa); + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/BodyInterface.java b/src/main/java/com/github/stephengold/joltjni/BodyInterface.java new file mode 100644 index 00000000..41ff85f9 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/BodyInterface.java @@ -0,0 +1,206 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * An interface to a {@code PhysicsSystem}, used to add, modify, query, and + * remove bodies. + * + * @author Stephen Gold sgold@sonic.net + */ +public class BodyInterface extends NonCopyable { + // ************************************************************************* + // constructors + + BodyInterface(long bodyInterfaceVa) { + setVirtualAddress(bodyInterfaceVa, false); + } + // ************************************************************************* + // new methods exposed + + /** + * Add the specified body to the physics system. + * + * @param bodyId the ID of the body to add (not null) + * @param activation whether to activate the body (not null) + */ + public void addBody(BodyId bodyId, EActivation activation) { + long bodyInterfaceVa = va(); + long bodyIdVa = bodyId.va(); + int activationOrdinal = activation.ordinal(); + addBody(bodyInterfaceVa, bodyIdVa, activationOrdinal); + } + + /** + * Create a body using the specified settings and add it to the physics + * system. + * + * @param settings the settings to use (not null) + * @param activation whether to activate the body (not null) + * @return the ID of the new body + */ + public BodyId createAndAddBody( + BodyCreationSettings settings, EActivation activation) { + long bodyInterfaceVa = va(); + long settingsVa = settings.va(); + int activationOrdinal = activation.ordinal(); + long bodyIdVa = createAndAddBody( + bodyInterfaceVa, settingsVa, activationOrdinal); + BodyId result = new BodyId(bodyIdVa); + + return result; + } + + /** + * Create a body using the specified settings. + * + * @param settings the settings to use (not null) + * @return the new body + */ + public Body createBody(BodyCreationSettings settings) { + long bodyInterfaceVa = va(); + long settingsVa = settings.va(); + long bodyVa = createBody(bodyInterfaceVa, settingsVa); + Body result = new Body(bodyVa); + + return result; + } + + /** + * Destroy the specified body. + * + * @param bodyId the ID of the body to destroy (not null) + */ + public void destroyBody(BodyId bodyId) { + long bodyInterfaceVa = va(); + long bodyIdVa = bodyId.va(); + destroyBody(bodyInterfaceVa, bodyIdVa); + } + + /** + * Locate the center of mass of the specified body. + * + * @param bodyId the ID of the body to locate (not null) + * @return a new vector + */ + public RVec3 getCenterOfMassPosition(BodyId bodyId) { + long bodyInterfaceVa = va(); + long bodyIdVa = bodyId.va(); + double xx = getCenterOfMassPositionX(bodyInterfaceVa, bodyIdVa); + double yy = getCenterOfMassPositionY(bodyInterfaceVa, bodyIdVa); + double zz = getCenterOfMassPositionZ(bodyInterfaceVa, bodyIdVa); + RVec3 result = new RVec3(xx, yy, zz); + + return result; + } + + /** + * Return the linear velocity of the specified body. + * + * @param bodyId the ID of the body (not null) + * @return a new vector + */ + public Vec3 getLinearVelocity(BodyId bodyId) { + long bodyInterfaceVa = va(); + long bodyIdVa = bodyId.va(); + float x = getLinearVelocityX(bodyInterfaceVa, bodyIdVa); + float y = getLinearVelocityY(bodyInterfaceVa, bodyIdVa); + float z = getLinearVelocityZ(bodyInterfaceVa, bodyIdVa); + Vec3 result = new Vec3(x, y, z); + + return result; + } + + /** + * Remove the specified body from the physics system. + * + * @param bodyId the ID of the body to remove (not null) + */ + public void removeBody(BodyId bodyId) { + long bodyIdVa = bodyId.va(); + removeBody(va(), bodyIdVa); + } + + /** + * Test whether the specified body is active. + * + * @param bodyId the ID of the body to test (not null) + * @return true if active, otherwise false + */ + public boolean isActive(BodyId bodyId) { + long bodyIdVa = bodyId.va(); + boolean result = isActive(va(), bodyIdVa); + return result; + } + + /** + * Alter the linear velocity of the specified body. + * + * @param bodyId the ID of the body to test (not null) + * @param velocity the desired velocity (not null, unaffected) + */ + public void setLinearVelocity(BodyId bodyId, Vec3 velocity) { + long bodyIdVa = bodyId.va(); + setLinearVelocity(va(), bodyIdVa, + velocity.getX(), velocity.getY(), velocity.getZ()); + } + // ************************************************************************* + // native private methods + + native private static void addBody( + long bodyInterfaceVa, long bodyIdVa, int activationOrdinal); + + native private static long createAndAddBody( + long bodyInterfaceVa, long settingsVa, int activationOrdinal); + + native private static long createBody( + long bodyInterfaceVa, long settingsVa); + + native private static void destroyBody( + long bodyInterfaceVa, long bodyIdVa); + + native private static double getCenterOfMassPositionX( + long bodyInterfaceVa, long bodyIdVa); + + native private static double getCenterOfMassPositionY( + long bodyInterfaceVa, long bodyIdVa); + + native private static double getCenterOfMassPositionZ( + long bodyInterfaceVa, long bodyIdVa); + + native private static float getLinearVelocityX( + long bodyInterfaceVa, long bodyIdVa); + + native private static float getLinearVelocityY( + long bodyInterfaceVa, long bodyIdVa); + + native private static float getLinearVelocityZ( + long bodyInterfaceVa, long bodyIdVa); + + native private static boolean isActive(long bodyInterfaceVa, long bodyIdVa); + + native private static void removeBody( + long bodyInterfaceVa, long bodyIdVa); + + native private static void setLinearVelocity( + long bodyInterfaceVa, long bodyIdVa, float vx, float vy, float vz); +} diff --git a/src/main/java/com/github/stephengold/joltjni/BoxShape.java b/src/main/java/com/github/stephengold/joltjni/BoxShape.java new file mode 100644 index 00000000..46190a54 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/BoxShape.java @@ -0,0 +1,55 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A {@code Shape} to generate axis-aligned rectangular solids. + * + * @author Stephen Gold sgold@sonic.net + */ +public class BoxShape extends ConvexShape { + // ************************************************************************* + // constructors + + BoxShape(long va) { + super(va); + } + + /** + * Instantiate a box shape with the specified half extents. + * + * @param halfExtents the desired half extents on each local axis (not null, + * all components ≥0, unaffected) + */ + public BoxShape(Vec3 halfExtents) { + float xHalfExtent = halfExtents.getX(); + float yHalfExtent = halfExtents.getY(); + float zHalfExtent = halfExtents.getZ(); + long shapeVa = createBoxShape(xHalfExtent, yHalfExtent, zHalfExtent); + setVirtualAddress(shapeVa, true); + } + // ************************************************************************* + // native private methods + + native private static long createBoxShape( + float xHalfExtent, float yHalfExtent, float zHalfExtent); +} diff --git a/src/main/java/com/github/stephengold/joltjni/BoxShapeSettings.java b/src/main/java/com/github/stephengold/joltjni/BoxShapeSettings.java new file mode 100644 index 00000000..6a9f62b9 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/BoxShapeSettings.java @@ -0,0 +1,68 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * + * @author Stephen Gold sgold@sonic.net + */ +public class BoxShapeSettings extends ConvexShapeSettings { + // ************************************************************************* + // constructors + + /** + * Instantiate box settings for the specified half extents. + * + * @param halfExtents the desired half extents on each local axis (not null, + * all components ≥0, unaffected) + */ + public BoxShapeSettings(Vec3 halfExtents) { + float xHalfExtent = halfExtents.getX(); + float yHalfExtent = halfExtents.getY(); + float zHalfExtent = halfExtents.getZ(); + long settingsVa + = createBoxShapeSettings(xHalfExtent, yHalfExtent, zHalfExtent); + setVirtualAddress(settingsVa, true); + } + // ************************************************************************* + // new methods exposed + + /** + * Generate a shape from these settings. + * + * @return a new instance + */ + public Shape createShape() { + long settingsVa = va(); + long shapeVa = createBoxShape(settingsVa); + BoxShape result = new BoxShape(shapeVa); + + return result; + } + // ************************************************************************* + // native private methods + + native private static long createBoxShapeSettings( + float xHalfExtent, float yHalfExtent, float zHalfExtent); + + native private static long createBoxShape(long settingsVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/ConvexShape.java b/src/main/java/com/github/stephengold/joltjni/ConvexShape.java new file mode 100644 index 00000000..8f546e1e --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ConvexShape.java @@ -0,0 +1,48 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A type of {@code Shape} that inherently possesses the convex property. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class ConvexShape extends Shape { + // ************************************************************************* + // constructors + + /** + * Instantiate a shape with no native object assigned. + */ + protected ConvexShape() { + } + + /** + * Instantiate a shape with the specified native object assigned. + * + * @param virtualAddress the virtual address of the native object to assign + * (not zero) + */ + protected ConvexShape(long virtualAddress) { + super(virtualAddress); + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/ConvexShapeSettings.java b/src/main/java/com/github/stephengold/joltjni/ConvexShapeSettings.java new file mode 100644 index 00000000..b4188658 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ConvexShapeSettings.java @@ -0,0 +1,37 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class ConvexShapeSettings extends ShapeSettings { + // ************************************************************************* + // constructors + + /** + * Instantiate settings with no native object assigned. + */ + protected ConvexShapeSettings() { + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/EActivation.java b/src/main/java/com/github/stephengold/joltjni/EActivation.java new file mode 100644 index 00000000..19e79c1d --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/EActivation.java @@ -0,0 +1,41 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Enumerate actions passed to {@code BodyInterface} when adding a body. + * + * @author Stephen Gold sgold@sonic.net + */ +public enum EActivation { + // ************************************************************************* + // values - order must match + + /** + * activate the added body + */ + Activate, + /** + * leave the added body's activation state unchanged + */ + DontActivate +} diff --git a/src/main/java/com/github/stephengold/joltjni/EMotionType.java b/src/main/java/com/github/stephengold/joltjni/EMotionType.java new file mode 100644 index 00000000..121f2463 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/EMotionType.java @@ -0,0 +1,45 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Enumerate motion types for bodies. + * + * @author Stephen Gold sgold@sonic.net + */ +public enum EMotionType { + // ************************************************************************* + // values - order must match + + /** + * non-moving bodies + */ + Static, + /** + * bodies whose motion can be controlled directly + */ + Kinematic, + /** + * bodies moved by forces, impulses, and torques + */ + Dynamic +} diff --git a/src/main/java/com/github/stephengold/joltjni/EPhysicsUpdateError.java b/src/main/java/com/github/stephengold/joltjni/EPhysicsUpdateError.java new file mode 100644 index 00000000..e9e0f2e0 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/EPhysicsUpdateError.java @@ -0,0 +1,58 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Error conditions reported by {@code PhysicsSystem.update()}. + * + * @author Stephen Gold sgold@sonic.net + */ +final public class EPhysicsUpdateError { + // ************************************************************************* + // constants + + /** + * no errors + */ + final public static int None = 0x0; + /** + * manifold cache is full + *

+ * Increase the {@code maxContactConstraints} argument to the + * {@code PhysicsSystem} constructor. + */ + final public static int ManifoldCacheFull = 0x1; + /** + * body pair cache is full + *

+ * Increase the {@code maxBodyPairs} argument to the {@code PhysicsSystem} + * constructor. + */ + final public static int BodyPairCacheFull = 0x2; + /** + * contact constraints buffer is full + *

+ * Increase {@code maxContactConstraints} argument to the + * {@code PhysicsSystem} constructor. + */ + final public static int ContactConstraintsFull = 0x4; +} diff --git a/src/main/java/com/github/stephengold/joltjni/JobSystem.java b/src/main/java/com/github/stephengold/joltjni/JobSystem.java new file mode 100644 index 00000000..e630efe1 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/JobSystem.java @@ -0,0 +1,38 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A system for scheduling units of work for execution. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class JobSystem extends NonCopyable { + // ************************************************************************* + // constructors + + /** + * Instantiate a job system with no native object assigned. + */ + protected JobSystem() { + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java b/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java new file mode 100644 index 00000000..03dd99f1 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/JobSystemThreadPool.java @@ -0,0 +1,49 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A sample implementation of {@code JobSystem} using a pool of worker threads. + * + * @author Stephen Gold sgold@sonic.net + */ +public class JobSystemThreadPool extends JobSystem { + // ************************************************************************* + // constructors + + /** + * Instantiate a system with the specified limits. + * + * @param maxJobs the maximum number of jobs to be scheduled + * @param maxBarriers the maximum number of barriers + * @param numThreads the number of worker threads to be created + */ + public JobSystemThreadPool(int maxJobs, int maxBarriers, int numThreads) { + long systemVa = createJobSystem(maxJobs, maxBarriers, numThreads); + setVirtualAddress(systemVa, true); + } + // ************************************************************************* + // native private methods + + native private static long createJobSystem( + int maxJobs, int maxBarriers, int numThreads); +} diff --git a/src/main/java/com/github/stephengold/joltjni/Jolt.java b/src/main/java/com/github/stephengold/joltjni/Jolt.java new file mode 100644 index 00000000..25f9071d --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/Jolt.java @@ -0,0 +1,92 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Utility methods providing JNI access to Jolt Physics. + * + * @author Stephen Gold sgold@sonic.net + */ +final public class Jolt { + // ************************************************************************* + // constructors + + /** + * A private constructor to inhibit instantiation of this class. + */ + private Jolt() { + } + // ************************************************************************* + // new methods exposed + + /** + * Destroy the factory for deserialization of saved data. + * + * @see newFactory() + */ + final native public static void destroyFactory(); + + /** + * Install the default assert callback. + */ + final native public static void installDefaultAssertCallback(); + + /** + * Install the default trace callback. + */ + final native public static void installDefaultTraceCallback(); + + /** + * Test whether the native library uses double-precision arithmetic. + * + * @return true if double-precision, otherwise false + */ + final native public static boolean isDoublePrecision(); + + /** + * Create a factory for deserialization of saved data. + * + * @see destroyFactory() + */ + final native public static void newFactory(); + + /** + * Register the allocation hook to use malloc/free. This must be done before + * any other Jolt function is called. + */ + final native public static void registerDefaultAllocator(); + + /** + * Register all physics types with the factory and install their collision + * handlers. + * + * @see unregisterTypes() + */ + final native public static void registerTypes(); + + /** + * Unregister all physics types with the factory. + * + * @see registerTypes() + */ + final native public static void unregisterTypes(); +} diff --git a/src/main/java/com/github/stephengold/joltjni/JoltPhysicsObject.java b/src/main/java/com/github/stephengold/joltjni/JoltPhysicsObject.java new file mode 100644 index 00000000..05e3bd38 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/JoltPhysicsObject.java @@ -0,0 +1,140 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * An abstract class to represent a (native) Jolt Physics object. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class JoltPhysicsObject implements AutoCloseable { + // ************************************************************************* + // fields + + /** + * true if the current instance owns (is responsible for freeing) its native + * object, otherwise false + */ + private boolean isOwner = false; + /** + * virtual address of the native object assigned to the current instance, or + * 0 for none + */ + private long virtualAddress; + // ************************************************************************* + // constructors + + /** + * Instantiate with no native object assigned. + */ + protected JoltPhysicsObject() { + this.virtualAddress = 0L; + } + + /** + * Instantiate with the specified native object assigned. + * + * @param virtualAddress the virtual address of the native object to assign + * (not zero) + */ + protected JoltPhysicsObject(long virtualAddress) { + assert virtualAddress != 0L; + this.virtualAddress = virtualAddress; + } + // ************************************************************************* + // new methods exposed + + /** + * Test whether a native object is assigned to the current instance. + * + * @return true if one is assigned, otherwise false + */ + final public boolean hasAssignedNativeObject() { + if (virtualAddress == 0L) { + return false; + } else { + return true; + } + } + + /** + * Test whether the current instance owns (is responsible for freeing) its + * native object. + * + * @return true if owner, otherwise false + */ + final public boolean ownsNativeObject() { + return isOwner; + } + + /** + * Return the virtual address of the assigned native object, assuming one is + * assigned. + * + * @return the virtual address (not zero) + */ + final public long va() { + assert virtualAddress != 0L; + return virtualAddress; + } + // ************************************************************************* + // new protected methods + + /** + * Assign a native object to this instance, assuming none is already + * assigned. + * + * @param virtualAddress the virtual address of the native object to assign + * (not zero) + * @param owner true → make the current object the owner, false → + * the current object isn't the owner + */ + protected void setVirtualAddress(long virtualAddress, boolean owner) { + assert virtualAddress != 0L : "invalid virtual address"; + assert !hasAssignedNativeObject() : "native object already assigned"; + + this.isOwner = owner; + this.virtualAddress = virtualAddress; + } + + /** + * Unassign (but don't free) the assigned native object, assuming one is + * assigned. + */ + final protected void unassignNativeObject() { + assert hasAssignedNativeObject() : "no native object is assigned"; + + this.isOwner = false; + this.virtualAddress = 0L; + } + // ************************************************************************* + // AutoCloseable methods + + @Override + public void close() { + if (isOwner) { + System.out.println( + "I don't know how to free " + getClass().getSimpleName()); + } + unassignNativeObject(); + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/MapObj2Bp.java b/src/main/java/com/github/stephengold/joltjni/MapObj2Bp.java new file mode 100644 index 00000000..660e62ec --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/MapObj2Bp.java @@ -0,0 +1,79 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A configurable one-way mapping from object layers to their corresponding broadphase layers. + * + * @author Stephen Gold sgold@sonic.net + */ +public class MapObj2Bp extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate an empty map with the specified capacity. + * + * @param numObjLayers the desired capacity (number of object layers) + * @param numBpLayers the number of broadphase layers + */ + public MapObj2Bp(int numObjLayers, int numBpLayers) { + long mapVa = createMapObj2Bp(numObjLayers, numBpLayers); + setVirtualAddress(mapVa, true); + } + // ************************************************************************* + // new methods exposed + + /** + * Add a mapping from the specified object layer to the specified broadphase + * layer. + * + * @param objLayer the index of the object layer (< numObjectLayers) + * @param bpLayer the index of the broadphase layer (< numBpLayers) + * @return the (modified) current instance (for chaining) + */ + public MapObj2Bp add(int objLayer, int bpLayer) { + add(va(), objLayer, bpLayer); + return this; + } + // ************************************************************************* + // JoltPhysicsObject methods + + @Override + public void close() { + if (ownsNativeObject()) { + long mapVa = va(); + free(mapVa); + } + + unassignNativeObject(); + } + // ************************************************************************* + // native private methods + + native private static void add(long mapVa, int objLayer, int bpLayer); + + native private static long createMapObj2Bp( + int numObjLayers, int numBpLayers); + + native private static void free(long mapVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/NonCopyable.java b/src/main/java/com/github/stephengold/joltjni/NonCopyable.java new file mode 100644 index 00000000..bc88252b --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/NonCopyable.java @@ -0,0 +1,64 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A non-copyable Jolt Physics object. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class NonCopyable extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate with no native object assigned. + */ + protected NonCopyable() { + } + + /** + * Instantiate with the specified native object assigned. + * + * @param virtualAddress the virtual address of the native object to assign + * (not zero) + */ + protected NonCopyable(long virtualAddress) { + super(virtualAddress); + } + // ************************************************************************* + // JoltPhysicsObject methods + + @Override + public void close() { + if (ownsNativeObject()) { + long virtualAddress = va(); + free(virtualAddress); + } + + unassignNativeObject(); + } + // ************************************************************************* + // native private methods + + native private static void free(long virtualAddress); +} diff --git a/src/main/java/com/github/stephengold/joltjni/ObjVsBpFilter.java b/src/main/java/com/github/stephengold/joltjni/ObjVsBpFilter.java new file mode 100644 index 00000000..e5c53b17 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ObjVsBpFilter.java @@ -0,0 +1,80 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A configurable filter to prevent specific object layers from interacting with + * specific broadphase layers. + * + * @author Stephen Gold sgold@sonic.net + */ +public class ObjVsBpFilter extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate a filter with all interactions enabled. + * + * @param numObjectLayers the number of object layers + * @param numBpLayers the number of broadphase layers + */ + public ObjVsBpFilter(int numObjectLayers, int numBpLayers) { + long filterVa = createObjVsBpFilter(numObjectLayers, numBpLayers); + setVirtualAddress(filterVa, true); + } + // ************************************************************************* + // new methods exposed + + /** + * Disable interactions between the specified layers. + * + * @param objLayer the index of the object layer (< numObjectLayers) + * @param bpLayer the index of the broadphase layer (< numBpLayers) + * @return the (modified) current instance (for chaining) + */ + public ObjVsBpFilter disablePair(int objLayer, int bpLayer) { + disablePair(va(), objLayer, bpLayer); + return this; + } + // ************************************************************************* + // JoltPhysicsObject methods + + @Override + public void close() { + if (ownsNativeObject()) { + long filterVa = va(); + free(filterVa); + } + + unassignNativeObject(); + } + // ************************************************************************* + // native private methods + + native private static long createObjVsBpFilter( + int numObjectLayers, int numBpLayers); + + native private static void disablePair( + long filterVa, int objLayer, int bpLayer); + + native private static void free(long filterVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/ObjVsObjFilter.java b/src/main/java/com/github/stephengold/joltjni/ObjVsObjFilter.java new file mode 100644 index 00000000..e7e23caa --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ObjVsObjFilter.java @@ -0,0 +1,77 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A configurable filter to disable interactions between specific object layers. + * + * @author Stephen Gold sgold@sonic.net + */ +public class ObjVsObjFilter extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate a filter with all interactions enabled. + * + * @param numObjectLayers the number of object layers + */ + public ObjVsObjFilter(int numObjectLayers) { + long filterVa = createObjVsObjFilter(numObjectLayers); + setVirtualAddress(filterVa, true); + } + // ************************************************************************* + // new methods exposed + + /** + * Disable interactions between the specified layers. + * + * @param layer1 the index of the first object layer (< numObjectLayers) + * @param layer2 the index of the 2nd object layer (< numObjectLayers) + * @return the (modified) current instance (for chaining) + */ + public ObjVsObjFilter disablePair(int layer1, int layer2) { + disablePair(va(), layer1, layer2); + return this; + } + // ************************************************************************* + // JoltPhysicsObject methods + + @Override + public void close() { + if (ownsNativeObject()) { + long filterVa = va(); + free(filterVa); + } + + unassignNativeObject(); + } + // ************************************************************************* + // native private methods + + native private static long createObjVsObjFilter(int numObjectLayers); + + native private static void disablePair( + long filterVa, int layer1, int layer2); + + native private static void free(long filterVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java b/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java new file mode 100644 index 00000000..22a54f29 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/PhysicsSystem.java @@ -0,0 +1,118 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * + * @author Stephen Gold sgold@sonic.net + */ +public class PhysicsSystem extends NonCopyable { + // ************************************************************************* + // fields + + final private BodyInterface bodyInterface; + // ************************************************************************* + // constructors + + /** + * Instantiate a physics system with the specified limits. + * + * @param maxBodies the desired maximum number of rigid bodies that can be + * added + * @param numBodyMutexes the desired number of mutexes to allocate, or 0 for + * the default number + * @param maxBodyPairs the desired maximum number of body pairs that can be + * queued at a time + * @param maxContactConstraints the desired capacity of the + * contact-constraint buffer + * @param map (not null) + * @param ovbFilter (not null) + * @param ovoFilter (not null) + */ + public PhysicsSystem(int maxBodies, int numBodyMutexes, int maxBodyPairs, + int maxContactConstraints, MapObj2Bp map, ObjVsBpFilter ovbFilter, + ObjVsObjFilter ovoFilter) { + long mapVa = map.va(); + long ovbFilterVa = ovbFilter.va(); + long ovoFilterVa = ovoFilter.va(); + long systemVa = createPhysicsSystem( + maxBodies, numBodyMutexes, maxBodyPairs, maxContactConstraints, + mapVa, ovbFilterVa, ovoFilterVa); + setVirtualAddress(systemVa, true); + + long bodyInterfaceVa = getBodyInterface(systemVa); + this.bodyInterface = new BodyInterface(bodyInterfaceVa); + } + // ************************************************************************* + // new methods exposed + + /** + * Access the system's {@code BodyInterface}. + * + * @return the pre-existing instance (not null) + */ + public BodyInterface getBodyInterface() { + assert bodyInterface != null; + return bodyInterface; + } + + /** + * Improve the performance of future collision detections. + */ + public void optimizeBroadPhase() { + optimizeBroadPhase(va()); + } + + /** + * Advance the simulation by the specified amount. + * + * @param deltaTime the total time to advance (in seconds) + * @param collisionSteps the number of simulation steps to perform + * @param tempAllocator the allocator to use (not null) + * @param jobSystem the job system to use (not null) + * @return a bitmask of error conditions, or-ed together + * + * @see com.github.stephengold.joltjni.EPhysicsUpdateError + */ + public int update(float deltaTime, int collisionSteps, + TempAllocatorImpl tempAllocator, JobSystemThreadPool jobSystem) { + long allocatorVa = tempAllocator.va(); + long jobSystemVa = jobSystem.va(); + int result = update( + va(), deltaTime, collisionSteps, allocatorVa, jobSystemVa); + + return result; + } + // ************************************************************************* + // native private methods + + native private static long createPhysicsSystem(int maxBodies, + int numBodyMutexes, int maxBodyPairs, int maxContactConstraints, + long mapVa, long ovbFilterVa, long ovoFilterVa); + + native private static long getBodyInterface(long systemVa); + + native private static void optimizeBroadPhase(long systemVa); + + native private static int update(long physicsSystemVa, float deltaTime, + int collisionSteps, long allocatorVa, long jobSystemVa); +} diff --git a/src/main/java/com/github/stephengold/joltjni/Quat.java b/src/main/java/com/github/stephengold/joltjni/Quat.java new file mode 100644 index 00000000..038a4a53 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/Quat.java @@ -0,0 +1,136 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * + * @author Stephen Gold sgold@sonic.net + */ +final public class Quat { + // ************************************************************************* + // fields + + /** + * the real (W) component + */ + private float w; + /** + * the first imaginary (X) component + */ + private float x; + /** + * the 2nd imaginary(Y) component + */ + private float y; + /** + * the 3rd imaginary (Z) component + */ + private float z; + // ************************************************************************* + // constructors + + /** + * Instantiate an identity quaternion (0,0,0,1). + */ + public Quat() { + this.x = 0f; + this.y = 0f; + this.z = 0f; + this.w = 1f; + } + + /** + * Instantiate a quaternion with specified components. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + * @param w the desired W component + */ + public Quat(float x, float y, float z, float w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + // ************************************************************************* + // new methods exposed + + /** + * Set all 4 components to specified values. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + * @param w the desired W component + */ + public void set(float x, float y, float z, float w) { + this.x = x; + this.y = y; + this.z = z; + this.w = w; + } + + /** + * Return the real (W) component in single precision. + * + * @return the component value + */ + public float getW() { + return w; + } + + /** + * Return the first imaginary (X) component in single precision. + * + * @return the component value + */ + public float getX() { + return x; + } + + /** + * Return the 2nd imaginary (Y) component in single precision. + * + * @return the component value + */ + public float getY() { + return y; + } + + /** + * Return the 3rd imaginary (Y) component in single precision. + * + * @return the component value + */ + public float getZ() { + return z; + } + // ************************************************************************* + // Object methods + + @Override + public String toString() { + String result = "Quat(" + x + " " + y + " " + z + " " + w + ")"; + return result; + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/RVec3.java b/src/main/java/com/github/stephengold/joltjni/RVec3.java new file mode 100644 index 00000000..391ed838 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/RVec3.java @@ -0,0 +1,195 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A vector composed of 3 double-precision components, used to represent + * locations in 3-dimensional space. + * + * @author Stephen Gold sgold@sonic.net + */ +final public class RVec3 { + // ************************************************************************* + // fields + + /** + * the first (X) component + */ + private double xx; + /** + * the 2nd (Y) component + */ + private double yy; + /** + * the 3rd (Z) component + */ + private double zz; + // ************************************************************************* + // constructors + + /** + * Instantiate an all-zero vector (0,0,0). + */ + public RVec3() { + this.xx = 0.; + this.yy = 0.; + this.zz = 0.; + } + + /** + * Instantiate a vector with specified components. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + */ + public RVec3(double x, double y, double z) { + this.xx = x; + this.yy = y; + this.zz = z; + } + // ************************************************************************* + // new methods exposed + + /** + * Return the first (X) component at positional precision. + * + * @return the component value + */ + public Object getX() { + Object result; + if (Jolt.isDoublePrecision()) { + result = xx; + } else { + result = (float) xx; + } + + return result; + } + + /** + * Return the 2nd (Y) component at positional precision. + * + * @return the component value + */ + public Object getY() { + Object result; + if (Jolt.isDoublePrecision()) { + result = yy; + } else { + result = (float) yy; + } + + return result; + } + + /** + * Return the 3rd (Z) component at positional precision. + * + * @return the component value + */ + public Object getZ() { + Object result; + if (Jolt.isDoublePrecision()) { + result = zz; + } else { + result = (float) zz; + } + + return result; + } + + /** + * Set all 3 components to specified values. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + */ + public void set(double x, double y, double z) { + this.xx = x; + this.yy = y; + this.zz = z; + } + + /** + * Return the first (X) component in single precision. + * + * @return the component value + */ + public float x() { + return (float) xx; + } + + /** + * Return the first (X) component in double precision. + * + * @return the component value + */ + public double xx() { + return xx; + } + + /** + * Return the 2nd (Y) component in single precision. + * + * @return the component value + */ + public float y() { + return (float) yy; + } + + /** + * Return the 2nd (Y) component in double precision. + * + * @return the component value + */ + public double yy() { + return yy; + } + + /** + * Return the 3rd (Z) component in single precision. + * + * @return the component value + */ + public float z() { + return (float) zz; + } + + /** + * Return the 3rd (Z) component in double precision. + * + * @return the component value + */ + public double zz() { + return zz; + } + // ************************************************************************* + // Object methods + + @Override + public String toString() { + String result = "RVec3(" + getX() + " " + getY() + " " + getZ() + ")"; + return result; + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/SerializableObject.java b/src/main/java/com/github/stephengold/joltjni/SerializableObject.java new file mode 100644 index 00000000..118cf6d8 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/SerializableObject.java @@ -0,0 +1,38 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A physics object that can be serialized (saved) and de-serialized (restored). + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class SerializableObject extends NonCopyable { + // ************************************************************************* + // constructors + + /** + * Instantiate with no native object assigned. + */ + protected SerializableObject() { + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/Shape.java b/src/main/java/com/github/stephengold/joltjni/Shape.java new file mode 100644 index 00000000..59a943ef --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/Shape.java @@ -0,0 +1,47 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class Shape extends NonCopyable { + // ************************************************************************* + // constructors + + /** + * Instantiate a shape with no native object assigned. + */ + protected Shape() { + } + + /** + * Instantiate a shape with the specified native object assigned. + * + * @param virtualAddress the virtual address of the native object to assign + * (not zero) + */ + protected Shape(long virtualAddress) { + super(virtualAddress); + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/ShapeSettings.java b/src/main/java/com/github/stephengold/joltjni/ShapeSettings.java new file mode 100644 index 00000000..731ae709 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/ShapeSettings.java @@ -0,0 +1,38 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * Settings used to create a {@code Shape}. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class ShapeSettings extends SerializableObject { + // ************************************************************************* + // constructors + + /** + * Instantiate settings with no native object assigned. + */ + protected ShapeSettings() { + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/SphereShape.java b/src/main/java/com/github/stephengold/joltjni/SphereShape.java new file mode 100644 index 00000000..50e0daf5 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/SphereShape.java @@ -0,0 +1,46 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A spherical {@code Shape}. + * + * @author Stephen Gold sgold@sonic.net + */ +public class SphereShape extends ConvexShape { + // ************************************************************************* + // constructors + + /** + * Instantiate a shape with the specified radius. + * + * @param radius the desired radius + */ + public SphereShape(float radius) { + long shapeVa = createSphereShape(radius); + setVirtualAddress(shapeVa, true); + } + // ************************************************************************* + // native private methods + + native private static long createSphereShape(float radius); +} diff --git a/src/main/java/com/github/stephengold/joltjni/TempAllocator.java b/src/main/java/com/github/stephengold/joltjni/TempAllocator.java new file mode 100644 index 00000000..35bb4c63 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/TempAllocator.java @@ -0,0 +1,38 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A memory allocator suitable for use by {@code PhysicsSystem.update()}. + * + * @author Stephen Gold sgold@sonic.net + */ +abstract public class TempAllocator extends NonCopyable { + // ************************************************************************* + // constructors + + /** + * Instantiate an allocator. + */ + protected TempAllocator() { + } +} diff --git a/src/main/java/com/github/stephengold/joltjni/TempAllocatorImpl.java b/src/main/java/com/github/stephengold/joltjni/TempAllocatorImpl.java new file mode 100644 index 00000000..1b591456 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/TempAllocatorImpl.java @@ -0,0 +1,46 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A last-in/first-out implementation of {@code TempAllocator}. + * + * @author Stephen Gold sgold@sonic.net + */ +public class TempAllocatorImpl extends TempAllocator { + // ************************************************************************* + // constructors + + /** + * Instantiate a LIFO allocator with the specified capacity. + * + * @param numBytes the desired capacity (in bytes) + */ + public TempAllocatorImpl(int numBytes) { + long allocatorVa = create(numBytes); + setVirtualAddress(allocatorVa, true); + } + // ************************************************************************* + // private methods + + native private static long create(int numBytes); +} diff --git a/src/main/java/com/github/stephengold/joltjni/Vec3.java b/src/main/java/com/github/stephengold/joltjni/Vec3.java new file mode 100644 index 00000000..4115a6bf --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/Vec3.java @@ -0,0 +1,120 @@ +/* +Copyright (c) 2024 Stephen Gold + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + */ +package com.github.stephengold.joltjni; + +/** + * A vector composed of 3 single-precision components, used to represent + * velocities and directions in 3-dimensional space. + * + * @author Stephen Gold sgold@sonic.net + */ +final public class Vec3 { + // ************************************************************************* + // fields + + /** + * the first (X) component + */ + private float x; + /** + * the 2nd (Y) component + */ + private float y; + /** + * the 3rd (Z) component + */ + private float z; + // ************************************************************************* + // constructors + + /** + * Instantiate an all-zero vector (0,0,0). + */ + public Vec3() { + this.x = 0f; + this.y = 0f; + this.z = 0f; + } + + /** + * Instantiate a vector with specified components. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + */ + public Vec3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + // ************************************************************************* + // new methods exposed + + /** + * Return the first (X) component in single precision. + * + * @return the component value + */ + public float getX() { + return x; + } + + /** + * Return the 2nd (Y) component in single precision. + * + * @return the component value + */ + public float getY() { + return y; + } + + /** + * Return the 3rd (Z) component in single precision. + * + * @return the component value + */ + public float getZ() { + return z; + } + + /** + * Set all 3 components to specified values. + * + * @param x the desired X component + * @param y the desired Y component + * @param z the desired Z component + */ + public void set(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } + // ************************************************************************* + // Object methods + + @Override + public String toString() { + String result = "Vec3(" + x + " " + y + " " + z + ")"; + return result; + } +}