Skip to content

Commit

Permalink
SubShape: add 7 public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jan 8, 2025
1 parent c7964d0 commit ecef956
Show file tree
Hide file tree
Showing 2 changed files with 282 additions and 2 deletions.
149 changes: 148 additions & 1 deletion src/main/java/com/github/stephengold/joltjni/SubShape.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-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
Expand All @@ -21,6 +21,9 @@ of this software and associated documentation files (the "Software"), to deal
*/
package com.github.stephengold.joltjni;

import com.github.stephengold.joltjni.readonly.QuatArg;
import com.github.stephengold.joltjni.readonly.Vec3Arg;

/**
* An element of a compound shape. (native type:
* {@code CompoundShape::SubShape})
Expand All @@ -44,6 +47,40 @@ public class SubShape extends JoltPhysicsObject {
// *************************************************************************
// new methods exposed

/**
* Calculate the local transform for this shape, given the scale of the
* child. The subshape is unaffected.
*
* @param scale the scale of the child in the local space of this shape (not
* null, unaffected)
* @return a new transform matrix
*/
public Mat44 getLocalTransformNoScale(Vec3Arg scale) {
long subshapeVa = va();
float sx = scale.getX();
float sy = scale.getY();
float sz = scale.getZ();
long resultVa = getLocalTransformNoScale(subshapeVa, sx, sy, sz);
Mat44 result = new Mat44(resultVa, true);

return result;
}

/**
* Decompress the center-of-mass position. The subshape is unaffected.
*
* @return a new vector
*/
public Vec3 getPositionCom() {
long subshapeVa = va();
float x = getPositionComX(subshapeVa);
float y = getPositionComY(subshapeVa);
float z = getPositionComZ(subshapeVa);
Vec3 result = new Vec3(x, y, z);

return result;
}

/**
* Decompress the rotation. The subshape is unaffected.
*
Expand All @@ -59,14 +96,124 @@ public Quat getRotation() {

return result;
}

/**
* Test whether the specified scale is valid for this subshape. The subshape
* is unaffected.
*
* @param scale the scale factors to validate (not null, unaffected)
* @return {@code true} if valid, otherwise {@code false}
*/
public boolean isValidScale(Vec3Arg scale) {
long subshapeVa = va();
float sx = scale.getX();
float sy = scale.getY();
float sz = scale.getZ();
boolean result = isValidScale(subshapeVa, sx, sy, sz);

return result;
}

/**
* Compress the center-of-mass location.
*
* @param location the desired location (not null, unaffected)
*/
public void setPositionCom(Vec3Arg location) {
long subshapeVa = va();
float x = location.getX();
float y = location.getY();
float z = location.getZ();
setPositionCom(subshapeVa, x, y, z);
}

/**
* Compress the rotation.
*
* @param rotation the desired rotation (not null, unaffected)
*/
public void setRotation(QuatArg rotation) {
long subshapeVa = va();
float qw = rotation.getW();
float qx = rotation.getX();
float qy = rotation.getY();
float qz = rotation.getZ();
setRotation(subshapeVa, qx, qy, qz, qw);
}

/**
* Update the transform of this subshape.
*
* @param offset the desired translation (not null, unaffected)
* @param rotation the desired rotation (not null, unaffected)
* @param centerOfMass the desired center of mass (not null, unaffected)
*/
public void setTransform(
Vec3Arg offset, QuatArg rotation, Vec3Arg centerOfMass) {
long subshapeVa = va();
float ox = offset.getX();
float oy = offset.getY();
float oz = offset.getZ();
float qw = rotation.getW();
float qx = rotation.getX();
float qy = rotation.getY();
float qz = rotation.getZ();
float cx = centerOfMass.getX();
float cy = centerOfMass.getY();
float cz = centerOfMass.getZ();
setTransform(subshapeVa, ox, oy, oz, qx, qy, qz, qw, cx, cy, cz);
}

/**
* Transform the specified scale to the local space of the child.
*
* @param scale the scale to transform (not null, unaffected)
* @return a new vector
*/
public Vec3 transformScale(Vec3Arg scale) {
long subshapeVa = va();
float sx = scale.getX();
float sy = scale.getY();
float sz = scale.getZ();
float[] storeFloats = new float[3];
transformScale(subshapeVa, sx, sy, sz, storeFloats);
Vec3 result = new Vec3(storeFloats);

return result;
}
// *************************************************************************
// native private methods

native private static long getLocalTransformNoScale(
long subshapeVa, float sx, float sy, float sz);

native private static float getPositionComX(long subShapeVa);

native private static float getPositionComY(long subShapeVa);

native private static float getPositionComZ(long subShapeVa);

native private static float getRotationW(long subShapeVa);

native private static float getRotationX(long subShapeVa);

native private static float getRotationY(long subShapeVa);

native private static float getRotationZ(long subShapeVa);

native private static boolean isValidScale(
long subShapeVa, float sx, float sy, float sz);

native private static void setPositionCom(
long subshapeVa, float x, float y, float z);

native private static void setRotation(
long subshapeVa, float qx, float qy, float qz, float qw);

native private static void setTransform(long subshapeVa, float ox, float oy,
float oz, float qx, float qy, float qz, float qw, float cx,
float cy, float cz);

native private static void transformScale(
long subshapeVa, float sx, float sy, float sz, float[] storeFloats);
}
135 changes: 134 additions & 1 deletion src/main/native/glue/s/SubShape.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (c) 2024 Stephen Gold
Copyright (c) 2024-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
Expand All @@ -26,9 +26,65 @@ SOFTWARE.
#include "Jolt/Jolt.h"
#include "Jolt/Physics/Collision/Shape/CompoundShape.h"
#include "auto/com_github_stephengold_joltjni_SubShape.h"
#include "glue/glue.h"

using namespace JPH;

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: getLocalTransformNoScale
* Signature: (JFFF)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_SubShape_getLocalTransformNoScale
(JNIEnv *, jclass, jlong subShapeVa, jfloat sx, jfloat sy, jfloat sz) {
const CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Vec3 scale(sx, sy, sz);
Mat44 * const pResult = new Mat44();
TRACE_NEW("Mat44", pResult)
*pResult = pSubShape->GetLocalTransformNoScale(scale);
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: getPositionComX
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_SubShape_getPositionComX
(JNIEnv *, jclass, jlong subShapeVa) {
const CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const float result = pSubShape->GetPositionCOM().GetX();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: getPositionComY
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_SubShape_getPositionComY
(JNIEnv *, jclass, jlong subShapeVa) {
const CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const float result = pSubShape->GetPositionCOM().GetY();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: getPositionComZ
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_SubShape_getPositionComZ
(JNIEnv *, jclass, jlong subShapeVa) {
const CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const float result = pSubShape->GetPositionCOM().GetZ();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: getRotationW
Expand Down Expand Up @@ -79,4 +135,81 @@ JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_SubShape_getRotatio
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const float result = pSubShape->GetRotation().GetZ();
return result;
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: isValidScale
* Signature: (JFFF)Z
*/
JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_SubShape_isValidScale
(JNIEnv *, jclass, jlong subShapeVa, jfloat sx, jfloat sy, jfloat sz) {
const CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Vec3 scale(sx, sy, sz);
const bool result = pSubShape->IsValidScale(scale);
return result;
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: setPositionCom
* Signature: (JFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SubShape_setPositionCom
(JNIEnv *, jclass, jlong subShapeVa, jfloat x, jfloat y, jfloat z) {
CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Vec3 location(x, y, z);
pSubShape->SetPositionCOM(location);
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: setRotation
* Signature: (JFFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SubShape_setRotation
(JNIEnv *, jclass, jlong subShapeVa, jfloat qx, jfloat qy, jfloat qz, jfloat qw) {
CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Quat rotation(qx, qy, qz, qw);
pSubShape->SetRotation(rotation);
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: setTransform
* Signature: (JFFFFFFFFFF)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SubShape_setTransform
(JNIEnv *, jclass, jlong subShapeVa, jfloat ox, jfloat oy, jfloat oz,
jfloat qx, jfloat qy, jfloat qz, jfloat qw, jfloat cx, jfloat cy, jfloat cz) {
CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Vec3 offset(ox, oy, oz);
const Quat rotation(qx, qy, qz, qw);
const Vec3 com(cx, cy, cz);
pSubShape->SetTransform(offset, rotation, com);
}

/*
* Class: com_github_stephengold_joltjni_SubShape
* Method: transformScale
* Signature: (JFFF[F)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_SubShape_transformScale
(JNIEnv *pEnv, jclass, jlong subShapeVa, jfloat sx, jfloat sy, jfloat sz,
jfloatArray storeFloats) {
CompoundShape::SubShape * const pSubShape
= reinterpret_cast<CompoundShape::SubShape *> (subShapeVa);
const Vec3 scale(sx, sy, sz);
Vec3 result = pSubShape->TransformScale(scale);
jboolean isCopy;
jfloat * const pStoreFloats
= pEnv->GetFloatArrayElements(storeFloats, &isCopy);
pStoreFloats[0] = result.GetX();
pStoreFloats[1] = result.GetY();
pStoreFloats[2] = result.GetZ();
pEnv->ReleaseFloatArrayElements(storeFloats, pStoreFloats, 0);
}

0 comments on commit ecef956

Please sign in to comment.