From 84369fd2bb3d7d0e5bf534634f65f06c43583df2 Mon Sep 17 00:00:00 2001 From: stephengold Date: Thu, 11 Jul 2024 12:42:39 -0700 Subject: [PATCH] Body: add 19 public methods --- .../com/github/stephengold/joltjni/Body.java | 308 +++++++++++++++++- src/main/native/glue/Body.cpp | 231 +++++++++++++ 2 files changed, 535 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/github/stephengold/joltjni/Body.java b/src/main/java/com/github/stephengold/joltjni/Body.java index 591d0263..821ce85e 100644 --- a/src/main/java/com/github/stephengold/joltjni/Body.java +++ b/src/main/java/com/github/stephengold/joltjni/Body.java @@ -44,7 +44,107 @@ public class Body extends NonCopyable { // new methods exposed /** - * Return the net force acting on the body. + * Apply the specified angular impulse to the body. + * + * @param impulse the impulse vector (not null, unaffected) + */ + public void addAngularImpulse(Vec3Arg impulse) { + long bodyVa = va(); + float x = impulse.getX(); + float y = impulse.getY(); + float z = impulse.getZ(); + addAngularImpulse(bodyVa, x, y, z); + } + + /** + * Apply the specified force to the body's center of mass. + * + * @param force the force vector (not null, unaffected) + */ + public void addForce(Vec3Arg force) { + long bodyVa = va(); + float fx = force.getX(); + float fy = force.getY(); + float fz = force.getZ(); + addForce(bodyVa, fx, fy, fz); + } + + /** + * Apply the specified force at the specified location. + * + * @param force the force vector (not null, unaffected) + * @param location the location of application (not null, unaffected) + */ + public void addForce(Vec3Arg force, RVec3Arg location) { + long bodyVa = va(); + float fx = force.getX(); + float fy = force.getY(); + float fz = force.getZ(); + double locX = location.xx(); + double locY = location.yy(); + double locZ = location.zz(); + addForce(bodyVa, fx, fy, fz, locX, locY, locZ); + } + + /** + * Apply the specified impulse to the body's center of mass. + * + * @param impulse the impulse vector (not null, unaffected) + */ + public void addImpulse(Vec3Arg impulse) { + long bodyVa = va(); + float jx = impulse.getX(); + float jy = impulse.getY(); + float jz = impulse.getZ(); + addImpulse(bodyVa, jx, jy, jz); + } + + /** + * Apply the specified impulse at the specified location. + * + * @param impulse the impulse vector (not null, unaffected) + * @param location the location of application (not null, unaffected) + */ + public void addImpulse(Vec3Arg impulse, RVec3Arg location) { + long bodyVa = va(); + float jx = impulse.getX(); + float jy = impulse.getY(); + float jz = impulse.getZ(); + double locX = location.xx(); + double locY = location.yy(); + double locZ = location.zz(); + addImpulse(bodyVa, jx, jy, jz, locX, locY, locZ); + } + + /** + * Apply the specified torque to the body. + * + * @param torque the torque vector (not null, unaffected) + */ + public void addTorque(Vec3Arg torque) { + long bodyVa = va(); + float x = torque.getX(); + float y = torque.getY(); + float z = torque.getZ(); + addTorque(bodyVa, x, y, z); + } + + /** + * Test whether the body could be made kinematic or dynamic. The current + * instance is unaffected. + * + * @return true if possible, otherwise false + */ + public boolean canBeKinematicOrDynamic() { + long bodyVa = va(); + boolean result = canBeKinematicOrDynamic(bodyVa); + + return result; + } + + /** + * Return the net force acting on the body. The current instance is + * unaffected. * * @return a new vector */ @@ -74,7 +174,20 @@ public Vec3 getAccumulatedTorque() { } /** - * Return the body's angular velocity. + * Test whether the body is allowed to fall asleep. The current instance is + * unaffected. + * + * @return true if allowed, otherwise false + */ + public boolean getAllowSleeping() { + long bodyVa = va(); + boolean result = getAllowSleeping(bodyVa); + + return result; + } + + /** + * Return the body's angular velocity. The current instance is unaffected. * * @return a new vector in physics-system coordinates */ @@ -183,6 +296,18 @@ public EMotionType getMotionType() { return result; } + /** + * Return the body's object layer. The current instance is unaffected. + * + * @return a layer index (≥0) + */ + public int getObjectLayer() { + long bodyVa = va(); + int result = getObjectLayer(bodyVa); + + return result; + } + /** * Return the location of the body's origin (which might differ from its * center of mass). The current instance is unaffected. @@ -237,7 +362,46 @@ public Quat getRotation() { } /** - * Test whether the body is deactivated. + * Return the body's shape. + * + * @return a new Java instance + */ + public ConstShape getShape() { + long bodyVa = va(); + long shapeVa = getShape(bodyVa); + ConstShape result = Shape.newShape(shapeVa); + + return result; + } + + /** + * Return the body's user data: can be used for anything. The current + * instance is unaffected. + * + * @return the value + */ + public long getUserData() { + long bodyVa = va(); + long result = getUserData(bodyVa); + + return result; + } + + /** + * Return the body's bounding box. The current instance is unaffected. + * + * @return a new JVM instance + */ + public AaBox getWorldSpaceBounds() { + long bodyVa = va(); + long boxVa = getWorldSpaceBounds(bodyVa); + AaBox result = new AaBox(boxVa); + + return result; + } + + /** + * Test whether the body is deactivated. The current instance is unaffected. * * @return false if deactivated, otherwise true */ @@ -249,7 +413,44 @@ public boolean isActive() { } /** - * Test whether the body is static. + * Test whether the body is dynamic. The current instance is unaffected. + * + * @return true if dynamic, otherwise false + */ + public boolean isDynamic() { + long bodyVa = va(); + boolean result = isDynamic(bodyVa); + + return result; + } + + /** + * Test whether the body is kinematic. The current instance is unaffected. + * + * @return true if kinematic, otherwise false + */ + public boolean isKinematic() { + long bodyVa = va(); + boolean result = isKinematic(bodyVa); + + return result; + } + + /** + * Test whether the body is a rigid body. The current instance is + * unaffected. + * + * @return true if rigid body, otherwise false + */ + public boolean isRigidBody() { + long bodyVa = va(); + boolean result = isRigidBody(bodyVa); + + return result; + } + + /** + * Test whether the body is static. The current instance is unaffected. * * @return true if static, otherwise false */ @@ -260,6 +461,39 @@ public boolean isStatic() { return result; } + /** + * Reposition the body, assuming it's kinematic. + * + * @param location the desired location (in physics-system coordinates, not + * null, unaffected) + * @param orientation the desired orientation (relative to the + * physics-system axes, not null, unaffected) + * @param deltaTime time until the desired position is reached (in seconds, + * >0) + */ + public void moveKinematic( + RVec3Arg location, Quat orientation, float deltaTime) { + long bodyVa = va(); + double xx = location.xx(); + double yy = location.yy(); + double zz = location.zz(); + float qw = orientation.getW(); + float qx = orientation.getX(); + float qy = orientation.getY(); + float qz = orientation.getZ(); + moveKinematic(bodyVa, xx, yy, zz, qx, qy, qz, qw, deltaTime); + } + + /** + * Alter whether the body is allowed to fall asleep. + * + * @param allow true to allow, false to inhibit + */ + public void setAllowSleeping(boolean allow) { + long bodyVa = va(); + setAllowSleeping(bodyVa, allow); + } + /** * Directly alter the body's angular velocity. * @@ -299,6 +533,17 @@ public void setLinearVelocity(Vec3Arg velocity) { setLinearVelocity(bodyVa, vx, vy, vz); } + /** + * Alter the body's motion type. + * + * @param motionType the desired value (not null) + */ + public void setMotionType(EMotionType motionType) { + long bodyVa = va(); + int ordinal = motionType.ordinal(); + setMotionType(bodyVa, ordinal); + } + /** * Alter the body's restitution ratio. * @@ -309,9 +554,39 @@ public void setRestitution(float restitution) { long bodyVa = va(); setRestitution(bodyVa, restitution); } + + /** + * Alter the body's user data. + * + * @param value the desired value (default=0) + */ + public void setUserData(long value) { + long bodyVa = va(); + setUserData(bodyVa, value); + } // ************************************************************************* // native private methods + native private static void addAngularImpulse( + long bodyVa, float x, float y, float z); + + native private static void addForce( + long bodyVa, float fx, float fy, float fz); + + native private static void addForce(long bodyVa, float fx, float fy, + float fz, double locX, double locY, double locZ); + + native private static void addImpulse( + long bodyVa, float jx, float jy, float jz); + + native private static void addImpulse(long bodyVa, float jx, float jy, + float jz, double locX, double locY, double locZ); + + native private static void addTorque( + long bodyVa, float x, float y, float z); + + native private static boolean canBeKinematicOrDynamic(long bodyVa); + native private static float getAccumulatedForceX(long bodyVa); native private static float getAccumulatedForceY(long bodyVa); @@ -324,6 +599,8 @@ public void setRestitution(float restitution) { native private static float getAccumulatedTorqueZ(long bodyVa); + native private static boolean getAllowSleeping(long bodyVa); + native private static float getAngularVelocityX(long bodyVa); native private static float getAngularVelocityY(long bodyVa); @@ -350,6 +627,8 @@ public void setRestitution(float restitution) { native private static int getMotionType(long bodyVa); + native private static int getObjectLayer(long bodyVa); + native private static double getPositionX(long bodyVa); native private static double getPositionY(long bodyVa); @@ -366,10 +645,27 @@ public void setRestitution(float restitution) { native private static float getRotationW(long bodyVa); + native private static long getShape(long bodyVa); + + native private static long getUserData(long bodyVa); + + native private static long getWorldSpaceBounds(long bodyVa); + native private static boolean isActive(long bodyVa); + native private static boolean isDynamic(long bodyVa); + + native private static boolean isKinematic(long bodyVa); + + native private static boolean isRigidBody(long bodyVa); + native private static boolean isStatic(long bodyVa); + native private static void moveKinematic(long bodyVa, double xx, double yy, + double zz, float qx, float qy, float qz, float qw, float deltaTime); + + native private static void setAllowSleeping(long bodyVa, boolean allow); + native private static void setAngularVelocity( long bodyVa, float wx, float wy, float wz); @@ -378,5 +674,9 @@ native private static void setAngularVelocity( native private static void setLinearVelocity( long bodyVa, float vx, float vy, float vz); + native private static void setMotionType(long bodyVa, int ordinal); + native private static void setRestitution(long bodyVa, float restitution); + + native private static void setUserData(long bodyVa, long value); } diff --git a/src/main/native/glue/Body.cpp b/src/main/native/glue/Body.cpp index 908fe793..63586535 100644 --- a/src/main/native/glue/Body.cpp +++ b/src/main/native/glue/Body.cpp @@ -29,6 +29,94 @@ SOFTWARE. using namespace JPH; +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addAngularImpulse + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addAngularImpulse + (JNIEnv *, jclass, jlong bodyVa, jfloat x, jfloat y, jfloat z) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 impulse(x, y, z); + pBody->AddAngularImpulse(impulse); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addForce + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addForce__JFFF + (JNIEnv *, jclass, jlong bodyVa, jfloat fx, jfloat fy, jfloat fz) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 force(fx, fy, fz); + pBody->AddForce(force); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addForce + * Signature: (JFFFDDD)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addForce__JFFFDDD + (JNIEnv *, jclass, jlong bodyVa, jfloat fx, jfloat fy, jfloat fz, + jdouble posX, jdouble posY, jdouble posZ) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 force(fx, fy, fz); + RVec3 position(posX, posY, posZ); + pBody->AddForce(force, position); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addImpulse + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addImpulse__JFFF + (JNIEnv *, jclass, jlong bodyVa, jfloat jx, jfloat jy, jfloat jz) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 impulse(jx, jy, jz); + pBody->AddImpulse(impulse); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addImpulse + * Signature: (JFFFDDD)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addImpulse__JFFFDDD + (JNIEnv *, jclass, jlong bodyVa, jfloat jx, jfloat jy, jfloat jz, + jdouble posX, jdouble posY, jdouble posZ) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 impulse(jx, jy, jz); + RVec3 position(posX, posY, posZ); + pBody->AddImpulse(impulse, position); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: addTorque + * Signature: (JFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_addTorque + (JNIEnv *, jclass, jlong bodyVa, jfloat x, jfloat y, jfloat z) { + Body * const pBody = reinterpret_cast (bodyVa); + Vec3 torque(x, y, z); + pBody->AddTorque(torque); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: canBeKinematicOrDynamic + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_canBeKinematicOrDynamic + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->CanBeKinematicOrDynamic(); + return result; +} + inline static const Vec3 getAccumulatedForce(jlong bodyVa) { const Body * const pBody = reinterpret_cast (bodyVa); const Vec3 result = pBody->GetAccumulatedForce(); @@ -113,6 +201,18 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getAccumulated return result; } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getAllowSleeping + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_getAllowSleeping + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->GetAllowSleeping(); + return result; +} + inline static const Vec3 getAngularVelocity(jlong bodyVa) { const Body * const pBody = reinterpret_cast (bodyVa); const Vec3 result = pBody->GetAngularVelocity(); @@ -293,6 +393,18 @@ inline static const RVec3 getPosition(jlong bodyVa) { return result; } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getObjectLayer + * Signature: (J)I + */ +JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_Body_getObjectLayer + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + int result = pBody->GetObjectLayer(); + return result; +} + /* * Class: com_github_stephengold_joltjni_Body * Method: getPositionX @@ -395,6 +507,42 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Body_getRotationW return result; } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getShape + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Body_getShape + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const Shape * const pResult = pBody->GetShape(); + return reinterpret_cast (pResult); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getUserData + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Body_getUserData + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + uint64 result = pBody->GetUserData(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: getWorldSpaceBounds + * Signature: (J)J + */ +JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Body_getWorldSpaceBounds + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + const AABox& result = pBody->GetWorldSpaceBounds(); + return reinterpret_cast (&result); +} + /* * Class: com_github_stephengold_joltjni_Body * Method: isActive @@ -407,6 +555,42 @@ JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isActive return result; } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: isDynamic + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isDynamic + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->IsDynamic(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: isKinematic + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isKinematic + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->IsKinematic(); + return result; +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: isRigidBody + * Signature: (J)Z + */ +JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isRigidBody + (JNIEnv *, jclass, jlong bodyVa) { + const Body * const pBody = reinterpret_cast (bodyVa); + bool result = pBody->IsRigidBody(); + return result; +} + /* * Class: com_github_stephengold_joltjni_Body * Method: isStatic @@ -419,6 +603,31 @@ JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Body_isStatic return result; } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: moveKinematic + * Signature: (JDDDFFFFF)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_moveKinematic + (JNIEnv *, jclass, jlong bodyVa, jdouble xx, jdouble yy, jdouble zz, + jfloat qx, jfloat qy, jfloat qz, jfloat qw, jfloat deltaTime) { + Body * const pBody = reinterpret_cast (bodyVa); + RVec3 location(xx, yy, zz); + Quat orientation(qx, qy, qz, qw); + pBody->MoveKinematic(location, orientation, deltaTime); +} + +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setAllowSleeping + * Signature: (JZ)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setAllowSleeping + (JNIEnv *, jclass, jlong bodyVa, jboolean allow) { + Body * const pBody = reinterpret_cast (bodyVa); + pBody->SetAllowSleeping(allow); +} + /* * Class: com_github_stephengold_joltjni_Body * Method: setAngularVelocity @@ -454,6 +663,18 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setLinearVelocit pBody->SetLinearVelocity(velocity); } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setMotionType + * Signature: (JI)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setMotionType + (JNIEnv *, jclass, jlong bodyVa, jint ordinal) { + Body * const pBody = reinterpret_cast (bodyVa); + EMotionType motionType = (EMotionType) ordinal; + pBody->SetMotionType(motionType); +} + /* * Class: com_github_stephengold_joltjni_Body * Method: setRestitution @@ -465,3 +686,13 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setRestitution pBody->SetRestitution(restitution); } +/* + * Class: com_github_stephengold_joltjni_Body + * Method: setUserData + * Signature: (JJ)V + */ +JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Body_setUserData + (JNIEnv *, jclass, jlong bodyVa, jlong value) { + Body * const pBody = reinterpret_cast (bodyVa); + pBody->SetUserData(value); +}