From f61fe774f1f2ca923a5ec7d83f2b0a14c22d2bdd Mon Sep 17 00:00:00 2001 From: stephengold Date: Thu, 16 Jan 2025 13:28:02 -0800 Subject: [PATCH] add the VehicleAntiRollBar class --- .../joltjni/VehicleAntiRollBar.java | 128 ++++++++++++++++++ src/main/native/glue/v/VehicleAntiRollBar.cpp | 105 ++++++++++++++ 2 files changed, 233 insertions(+) create mode 100644 src/main/java/com/github/stephengold/joltjni/VehicleAntiRollBar.java create mode 100644 src/main/native/glue/v/VehicleAntiRollBar.cpp diff --git a/src/main/java/com/github/stephengold/joltjni/VehicleAntiRollBar.java b/src/main/java/com/github/stephengold/joltjni/VehicleAntiRollBar.java new file mode 100644 index 00000000..ba099432 --- /dev/null +++ b/src/main/java/com/github/stephengold/joltjni/VehicleAntiRollBar.java @@ -0,0 +1,128 @@ +/* +Copyright (c) 2025 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 spring that connects two wheels and reduces a vehicle's rolling motion. + * + * @author Stephen Gold sgold@sonic.net + */ +public class VehicleAntiRollBar extends JoltPhysicsObject { + // ************************************************************************* + // constructors + + /** + * Instantiate with the specified container and native object. + * + * @param container the containing object, or {@code null} if none + * @param barVa the virtual address of the native object to assign (not + * zero) + */ + VehicleAntiRollBar(JoltPhysicsObject container, long barVa) { + super(container, barVa); + } + // ************************************************************************* + // new methods exposed + + /** + * Return the index of the left wheel. The bar is unaffected. (native + * attribute: mLeftWheel) + * + * @return the index of the wheel + */ + public int getLeftWheel() { + long barVa = va(); + int result = getLeftWheel(barVa); + + return result; + } + + /** + * Return the index of the right wheel. The bar is unaffected. (native + * attribute: mRightWheel) + * + * @return the index of the wheel + */ + public int getRightWheel() { + long barVa = va(); + int result = getRightWheel(barVa); + return result; + } + + /** + * Return the stiffness of the bar. The bar is unaffected. (native + * attribute: mStiffness) + * + * @return the spring constant (in Newtons per meter) + */ + public float getStiffness() { + long barVa = va(); + float result = getStiffness(barVa); + + return result; + } + + /** + * Alter the index of the left wheel. (native attribute: mLeftWheel) + * + * @param wheelIndex the index of the desired wheel (default=0) + */ + public void setLeftWheel(int wheelIndex) { + long barVa = va(); + setLeftWheel(barVa, wheelIndex); + } + + /** + * Alter the index of the right wheel. (native attribute: mRightWheel) + * + * @param wheelIndex the index of the desired wheel (default=1) + */ + public void setRightWheel(int wheelIndex) { + long barVa = va(); + setRightWheel(barVa, wheelIndex); + } + + /** + * Alter the stiffness of the bar. (native attribute: mStiffness) + * + * @param stiffness the desired spring constant (in Newtons per meter, + * default=1000) + */ + public void setStiffness(float stiffness) { + long barVa = va(); + setStiffness(barVa, stiffness); + } + // ************************************************************************* + // native private methods + + native private static int getLeftWheel(long barVa); + + native private static int getRightWheel(long barVa); + + native private static float getStiffness(long barVa); + + native private static void setLeftWheel(long barVa, int wheelIndex); + + native private static void setRightWheel(long barVa, int wheelIndex); + + native private static void setStiffness(long barVa, float stiffness); +} diff --git a/src/main/native/glue/v/VehicleAntiRollBar.cpp b/src/main/native/glue/v/VehicleAntiRollBar.cpp new file mode 100644 index 00000000..5d3ad538 --- /dev/null +++ b/src/main/native/glue/v/VehicleAntiRollBar.cpp @@ -0,0 +1,105 @@ +/* +Copyright (c) 2025 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. + */ + +/* + * Author: Stephen Gold + */ +#include "Jolt/Jolt.h" +#include "Jolt/Physics/Vehicle/VehicleAntiRollBar.h" +#include "auto/com_github_stephengold_joltjni_VehicleAntiRollBar.h" + +using namespace JPH; + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: getLeftWheel + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_getLeftWheel + (JNIEnv *, jclass, jlong barVa) { + const VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + const int result = pBar->mLeftWheel; + return result; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: getRightWheel + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_getRightWheel + (JNIEnv *, jclass, jlong barVa) { + const VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + const int result = pBar->mRightWheel; + return result; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: getStiffness + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_getStiffness + (JNIEnv *, jclass, jlong barVa) { + const VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + const float result = pBar->mStiffness; + return result; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: setLeftWheel + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_setLeftWheel + (JNIEnv *, jclass, jlong barVa, jint wheelIndex) { + VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + pBar->mLeftWheel = wheelIndex; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: setRightWheel + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_setRightWheel + (JNIEnv *, jclass, jlong barVa, jint wheelIndex) { + VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + pBar->mRightWheel = wheelIndex; +} + +/* + * Class: com_github_stephengold_joltjni_VehicleAntiRollBar + * Method: setStiffness + * Signature: (JF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_VehicleAntiRollBar_setStiffness + (JNIEnv *, jclass, jlong barVa, jfloat stiffness) { + VehicleAntiRollBar * const pBar + = reinterpret_cast (barVa); + pBar->mStiffness = stiffness; +} \ No newline at end of file