From c098536843595b40b98439510bcf3141ace4b871 Mon Sep 17 00:00:00 2001 From: stephengold Date: Fri, 28 Jun 2024 13:40:10 -0700 Subject: [PATCH] Body: add 15 public methods and 28 JNI methods --- .../com/github/stephengold/joltjni/Body.java | 260 ++++++++++++ .../com_github_stephengold_joltjni_Body.cpp | 371 ++++++++++++++++++ 2 files changed, 631 insertions(+) diff --git a/src/main/java/com/github/stephengold/joltjni/Body.java b/src/main/java/com/github/stephengold/joltjni/Body.java index 0dc8b4e2..5d95fa23 100644 --- a/src/main/java/com/github/stephengold/joltjni/Body.java +++ b/src/main/java/com/github/stephengold/joltjni/Body.java @@ -37,6 +37,77 @@ public class Body extends NonCopyable { // ************************************************************************* // new methods exposed + /** + * Return the net force acting on the body. + * + * @return a new vector + */ + public Vec3 getAccumulatedForce() { + long bodyVa = va(); + float x = getAccumulatedForceX(bodyVa); + float y = getAccumulatedForceY(bodyVa); + float z = getAccumulatedForceZ(bodyVa); + Vec3 result = new Vec3(x, y, z); + + return result; + } + + /** + * Return the net torque acting on the body. + * + * @return a new vector + */ + public Vec3 getAccumulatedTorque() { + long bodyVa = va(); + float x = getAccumulatedTorqueX(bodyVa); + float y = getAccumulatedTorqueY(bodyVa); + float z = getAccumulatedTorqueZ(bodyVa); + Vec3 result = new Vec3(x, y, z); + + return result; + } + + /** + * Return the angular velocity. + * + * @return a new vector in physics-system coordinates + */ + public Vec3 getAngularVelocity() { + long bodyVa = va(); + float wx = getAngularVelocityX(bodyVa); + float wy = getAngularVelocityY(bodyVa); + float wz = getAngularVelocityZ(bodyVa); + Vec3 result = new Vec3(wx, wy, wz); + + return result; + } + + /** + * Return the location of the body's center of mass. + * + * @return a new vector in physics-system coordinates + */ + public RVec3 getCenterOfMassPosition() { + long bodyVa = va(); + double xx = getCenterOfMassPositionX(bodyVa); + double yy = getCenterOfMassPositionY(bodyVa); + double zz = getCenterOfMassPositionZ(bodyVa); + RVec3 result = new RVec3(xx, yy, zz); + + return result; + } + + /** + * Return the body's friction ratio. + * + * @return the value + */ + public float getFriction() { + long bodyVa = va(); + float result = getFriction(bodyVa); + return result; + } + /** * Acquire the body's ID for use with {@code BodyInterface}. * @@ -49,8 +120,197 @@ public BodyId getId() { return result; } + + /** + * Return the linear velocity. + * + * @return a new vector in physics-system coordinates + */ + public Vec3 getLinearVelocity() { + long bodyVa = va(); + float vx = getLinearVelocityX(bodyVa); + float vy = getLinearVelocityY(bodyVa); + float vz = getLinearVelocityZ(bodyVa); + Vec3 result = new Vec3(vx, vy, vz); + + return result; + } + + /** + * Acquire the body's motion properties. + * + * @return a new instance, or null if none + */ + public MotionProperties getMotionProperties() { + MotionProperties result; + long bodyVa = va(); + if (isStatic(bodyVa)) { + result = null; + } else { + long propertiesVa = getMotionProperties(bodyVa); + result = new MotionProperties(propertiesVa); + } + + return result; + } + + /** + * Return the body's restitution ratio. + * + * @return the value + */ + public float getRestitution() { + long bodyVa = va(); + float result = getRestitution(bodyVa); + + return result; + } + + /** + * Return the body's orientation relative to the physics system axes. + * + * @return a new quaternion + */ + public Quat getRotation() { + long bodyVa = va(); + float qx = getRotationX(bodyVa); + float qy = getRotationY(bodyVa); + float qz = getRotationZ(bodyVa); + float qw = getRotationW(bodyVa); + Quat result = new Quat(qx, qy, qz, qw); + + return result; + } + + /** + * Test whether the body is deactivated. + * + * @return false if deactivated, otherwise true + */ + public boolean isActive() { + long bodyVa = va(); + boolean result = isActive(bodyVa); + + return result; + } + + /** + * Test whether the body is static. + * + * @return true if static, otherwise false + */ + public boolean isStatic() { + long bodyVa = va(); + boolean result = isStatic(bodyVa); + + return result; + } + + /** + * Directly alter the angular velocity. + * + * @param omega the desired angular velocity (not null, unaffected) + */ + public void setAngularVelocity(Vec3 omega) { + long bodyVa = va(); + float wx = omega.getX(); + float wy = omega.getY(); + float wz = omega.getZ(); + setAngularVelocity(bodyVa, wx, wy, wz); + } + + /** + * Alter the body's friction ratio. + * + * @param friction the desired value + */ + public void setFriction(float friction) { + long bodyVa = va(); + setFriction(bodyVa, friction); + } + + /** + * Directly alter the linear velocity. + * + * @param velocity the desired angular velocity (not null, unaffected) + */ + public void setLinearVelocity(Vec3 velocity) { + long bodyVa = va(); + float vx = velocity.getX(); + float vy = velocity.getY(); + float vz = velocity.getZ(); + setLinearVelocity(bodyVa, vx, vy, vz); + } + + /** + * Alter the body's restitution ratio. + * + * @param restitution the desired value + */ + public void setRestitution(float restitution) { + long bodyVa = va(); + setRestitution(bodyVa, restitution); + } // ************************************************************************* // native private methods + native private static float getAccumulatedForceX(long bodyVa); + + native private static float getAccumulatedForceY(long bodyVa); + + native private static float getAccumulatedForceZ(long bodyVa); + + native private static float getAccumulatedTorqueX(long bodyVa); + + native private static float getAccumulatedTorqueY(long bodyVa); + + native private static float getAccumulatedTorqueZ(long bodyVa); + + native private static float getAngularVelocityX(long bodyVa); + + native private static float getAngularVelocityY(long bodyVa); + + native private static float getAngularVelocityZ(long bodyVa); + + native private static double getCenterOfMassPositionX(long bodyVa); + + native private static double getCenterOfMassPositionY(long bodyVa); + + native private static double getCenterOfMassPositionZ(long bodyVa); + + native private static float getFriction(long bodyVa); + native private static long getId(long bodyVa); + + native private static float getLinearVelocityX(long bodyVa); + + native private static float getLinearVelocityY(long bodyVa); + + native private static float getLinearVelocityZ(long bodyVa); + + native private static long getMotionProperties(long bodyVa); + + native private static float getRestitution(long bodyVa); + + native private static float getRotationX(long bodyVa); + + native private static float getRotationY(long bodyVa); + + native private static float getRotationZ(long bodyVa); + + native private static float getRotationW(long bodyVa); + + native private static boolean isActive(long bodyVa); + + native private static boolean isStatic(long bodyVa); + + native private static void setAngularVelocity( + long bodyVa, float wx, float wy, float wz); + + native private static void setFriction(long bodyVa, float friction); + + native private static void setLinearVelocity( + long bodyVa, float vx, float vy, float vz); + + native private static void setRestitution(long bodyVa, float restitution); } diff --git a/src/main/native/glue/com_github_stephengold_joltjni_Body.cpp b/src/main/native/glue/com_github_stephengold_joltjni_Body.cpp index 4db23c33..d5dbff72 100644 --- a/src/main/native/glue/com_github_stephengold_joltjni_Body.cpp +++ b/src/main/native/glue/com_github_stephengold_joltjni_Body.cpp @@ -29,6 +29,186 @@ SOFTWARE. using namespace JPH; +inline static const Vec3& getAccumulatedForce(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Vec3& result = pBody->GetAccumulatedForce(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedForceX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedForceX + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedForce(bodyVa); + float result = vec3.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedForceY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedForceY + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedForce(bodyVa); + float result = vec3.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedForceZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedForceZ + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedForce(bodyVa); + float result = vec3.GetZ(); + return result; +} + +inline static const Vec3& getAccumulatedTorque(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Vec3& result = pBody->GetAccumulatedTorque(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedTorqueX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedTorqueX + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedTorque(bodyVa); + float result = vec3.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedTorqueY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedTorqueY + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedTorque(bodyVa); + float result = vec3.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAccumulatedTorqueZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulatedTorqueZ + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAccumulatedTorque(bodyVa); + float result = vec3.GetZ(); + return result; +} + +inline static const Vec3& getAngularVelocity(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Vec3& result = pBody->GetAngularVelocity(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAngularVelocityX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAngularVelocityX + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAngularVelocity(bodyVa); + float result = vec3.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAngularVelocityY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAngularVelocityY + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAngularVelocity(bodyVa); + float result = vec3.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAngularVelocityZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAngularVelocityZ + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getAngularVelocity(bodyVa); + float result = vec3.GetZ(); + return result; +} + +inline static const RVec3& getCenterOfMassPosition(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const RVec3& result = pBody->GetCenterOfMassPosition(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getCenterOfMassPositionX + * Signature: (J)D + */ +JNIEXPORT jdouble JNICALL Java_com_github_stephengold_joltjni_Body_getCenterOfMassPositionX + (JNIEnv *, jclass, jlong bodyVa) { + const RVec3& rvec3 = getCenterOfMassPosition(bodyVa); + jdouble result = rvec3.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getCenterOfMassPositionY + * Signature: (J)D + */ +JNIEXPORT jdouble JNICALL Java_com_github_stephengold_joltjni_Body_getCenterOfMassPositionY + (JNIEnv *, jclass, jlong bodyVa) { + const RVec3& rvec3 = getCenterOfMassPosition(bodyVa); + jdouble result = rvec3.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getCenterOfMassPositionZ + * Signature: (J)D + */ +JNIEXPORT jdouble JNICALL Java_com_github_stephengold_joltjni_Body_getCenterOfMassPositionZ + (JNIEnv *, jclass, jlong bodyVa) { + const RVec3& rvec3 = getCenterOfMassPosition(bodyVa); + jdouble result = rvec3.GetZ(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getFriction + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getFriction + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + float result = pBody->GetFriction(); + return result; +} + /* * Class: com_github_stephengold_joltjni_Body * Method: getId @@ -40,3 +220,194 @@ JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Body_getId const BodyID &result = pBody->GetID(); return reinterpret_cast (&result); } + +inline static const Vec3& getLinearVelocity(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Vec3& result = pBody->GetLinearVelocity(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getLinearVelocityX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getLinearVelocityX + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getLinearVelocity(bodyVa); + float result = vec3.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getLinearVelocityY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getLinearVelocityY + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getLinearVelocity(bodyVa); + float result = vec3.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getLinearVelocityZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getLinearVelocityZ + (JNIEnv *, jclass, jlong bodyVa) { + const Vec3& vec3 = getLinearVelocity(bodyVa); + float result = vec3.GetZ(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getMotionProperties + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Body_getMotionProperties + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const MotionProperties * const pResult = pBody->GetMotionProperties(); + return reinterpret_cast (pResult); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getRestitution + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRestitution + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + float result = pBody->GetRestitution(); + return result; +} + +inline static const Quat& getRotation(jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Quat& result = pBody->GetRotation(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getRotationX + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRotationX + (JNIEnv *, jclass, jlong bodyVa) { + const Quat& rotation = getRotation(bodyVa); + float result = rotation.GetX(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getRotationY + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRotationY + (JNIEnv *, jclass, jlong bodyVa) { + const Quat& rotation = getRotation(bodyVa); + float result = rotation.GetY(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getRotationZ + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRotationZ + (JNIEnv *, jclass, jlong bodyVa) { + const Quat& rotation = getRotation(bodyVa); + float result = rotation.GetZ(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getRotationW + * Signature: (J)F + */ +JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRotationW + (JNIEnv *, jclass, jlong bodyVa) { + const Quat& rotation = getRotation(bodyVa); + float result = rotation.GetW(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: isActive + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isActive + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->IsActive(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: isStatic + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isStatic + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->IsStatic(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setAngularVelocity + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setAngularVelocity + (JNIEnv *, jclass, jlong bodyVa, jfloat wx, jfloat wy, jfloat wz) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 omega(wx, wy, wz); + pBody->SetAngularVelocity(omega); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setFriction + * Signature: (JF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setFriction + (JNIEnv *, jclass, jlong bodyVa, jfloat friction) { + Body * const pBody = reinterpret_cast (bodyVa); + pBody->SetFriction(friction); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setLinearVelocity + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setLinearVelocity + (JNIEnv *, jclass, jlong bodyVa, jfloat vx, jfloat vy, jfloat vz) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 velocity(vx, vy, vz); + pBody->SetLinearVelocity(velocity); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setRestitution + * Signature: (JF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setRestitution + (JNIEnv *, jclass, jlong bodyVa, jfloat restitution) { + Body * const pBody = reinterpret_cast (bodyVa); + pBody->SetRestitution(restitution); +} +