Skip to content

Commit

Permalink
Shape: add 6 public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jul 13, 2024
1 parent 69d7410 commit 78fff74
Show file tree
Hide file tree
Showing 3 changed files with 243 additions and 7 deletions.
47 changes: 43 additions & 4 deletions src/main/java/com/github/stephengold/joltjni/ConstShape.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,32 @@ public interface ConstShape extends ConstJoltPhysicsObject {
int countDebugTriangles();

/**
* Copy the shape's mass properties.
* Locate the shape's center of mass. The shape is unaffected.
*
* @return a new instance
* @return a new location vector
*/
Vec3 getCenterOfMass();

/**
* Return the radius of the largest sphere that fits inside the shape. The
* shape is unaffected.
*
* @return the radius (≥0)
*/
float getInnerRadius();

/**
* Return a bounding box that includes the convex radius. The shape is
* unaffected.
*
* @return a new, mutable box
*/
AaBox getLocalBounds();

/**
* Copy the shape's mass properties. The shape is unaffected.
*
* @return a new, mutable properties object
*/
MassProperties getMassProperties();

Expand All @@ -71,9 +94,25 @@ public interface ConstShape extends ConstJoltPhysicsObject {
EShapeType getType();

/**
* Test whether the shape can be used in a dynamic/kinematic body.
* Return the shape's user data: can be used for anything. The shape is
* unaffected.
*
* @return the value
*/
long getUserData();

/**
* Test whether the shape can be used in a dynamic/kinematic body. The shape
* is unaffected.
*
* @return true if it can be only be static, otherwise false
*/
boolean mustBeStatic();
}

/**
* Create a counted reference to the native object.
*
* @return a new JVM object with a new native object assigned
*/
ShapeRefC toRefC();
}
107 changes: 104 additions & 3 deletions src/main/java/com/github/stephengold/joltjni/Shape.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ static Shape newShape(long shapeVa) {
}
return result;
}

/**
* Alter the shape's user data.
*
* @param value the desired value (default=0)
*/
public void setUserData(long value) {
long shapeVa = va();
setUserData(shapeVa, value);
}
// *************************************************************************
// ConstShape methods

Expand Down Expand Up @@ -125,9 +135,54 @@ public int countDebugTriangles() {
}

/**
* Copy the shape's mass properties.
* Locate the shape's center of mass. The shape is unaffected.
*
* @return a new location vector
*/
@Override
public Vec3 getCenterOfMass() {
long shapeVa = va();
float x = getCenterOfMassX(shapeVa);
float y = getCenterOfMassY(shapeVa);
float z = getCenterOfMassZ(shapeVa);
Vec3 result = new Vec3(x, y, z);

return result;
}

/**
* Return the radius of the largest sphere that fits inside the shape. The
* shape is unaffected.
*
* @return a new instance
* @return the radius (≥0)
*/
@Override
public float getInnerRadius() {
long shapeVa = va();
float result = getInnerRadius(shapeVa);

return result;
}

/**
* Return a bounding box that includes the convex radius, centered on the
* center of mass. The shape is unaffected.
*
* @return a new, mutable box
*/
@Override
public AaBox getLocalBounds() {
long shapeVa = va();
long boxVa = getLocalBounds(shapeVa);
AaBox result = new AaBox(boxVa, true);

return result;
}

/**
* Copy the shape's mass properties. The shape is unaffected.
*
* @return a new, mutable properties object
*/
@Override
public MassProperties getMassProperties() {
Expand Down Expand Up @@ -180,14 +235,44 @@ public EShapeType getType() {
}

/**
* Test whether the shape can be used in a dynamic/kinematic body.
* Return the shape's user data: can be used for anything. The shape is
* unaffected.
*
* @return the value
*/
@Override
public long getUserData() {
long shapeVa = va();
long result = getUserData(shapeVa);

return result;
}

/**
* Test whether the shape can be used in a dynamic/kinematic body. The shape
* is unaffected.
*
* @return true if it can be only be static, otherwise false
*/
@Override
public boolean mustBeStatic() {
long shapeVa = va();
boolean result = mustBeStatic(shapeVa);

return result;
}

/**
* Create a counted reference to the native object.
*
* @return a new JVM object with a new native object assigned
*/
@Override
public ShapeRefC toRefC() {
long shapeVa = va();
long refVa = toRefC(shapeVa);
ShapeRefC result = new ShapeRefC(refVa, true);

return result;
}
// *************************************************************************
Expand All @@ -198,6 +283,16 @@ native private static void copyDebugTriangles(

native private static int countDebugTriangles(long shapeVa);

native private static float getCenterOfMassX(long shapeVa);

native private static float getCenterOfMassY(long shapeVa);

native private static float getCenterOfMassZ(long shapeVa);

native private static float getInnerRadius(long shapeVa);

native private static long getLocalBounds(long shapeVa);

native private static long getMassProperties(long shapeVa);

native private static int getRefCount(long shapeVa);
Expand All @@ -206,5 +301,11 @@ native private static void copyDebugTriangles(

native private static int getType(long shapeVa);

native private static long getUserData(long shapeVa);

native private static boolean mustBeStatic(long shapeVa);

native private static void setUserData(long shapeVa, long value);

native private static long toRefC(long shapeVa);
}
96 changes: 96 additions & 0 deletions src/main/native/glue/Shape.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,67 @@ JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_Shape_countDebugTrian
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getCenterOfMassX
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Shape_getCenterOfMassX
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
float result = pShape->GetCenterOfMass().GetX();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getCenterOfMassY
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Shape_getCenterOfMassY
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
float result = pShape->GetCenterOfMass().GetY();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getCenterOfMassZ
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Shape_getCenterOfMassZ
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
float result = pShape->GetCenterOfMass().GetZ();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getInnerRadius
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_Shape_getInnerRadius
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
float result = pShape->GetInnerRadius();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getLocalBounds
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Shape_getLocalBounds
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
AABox *pResult = new AABox();
*pResult = pShape->GetLocalBounds();
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getMassProperties
Expand Down Expand Up @@ -148,6 +209,18 @@ JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_Shape_getType
return (jint) type;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: getUserData
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Shape_getUserData
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
uint64 result = pShape->GetUserData();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: mustBeStatic
Expand All @@ -159,3 +232,26 @@ JNIEXPORT jboolean JNICALL Java_com_github_stephengold_joltjni_Shape_mustBeStati
bool result = pShape->MustBeStatic();
return result;
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: setUserData
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_Shape_setUserData
(JNIEnv *, jclass, jlong shapeVa, jlong value) {
Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
pShape->SetUserData(value);
}

/*
* Class: com_github_stephengold_joltjni_Shape
* Method: toRefC
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_Shape_toRefC
(JNIEnv *, jclass, jlong shapeVa) {
const Shape * const pShape = reinterpret_cast<Shape *> (shapeVa);
ShapeRefC * const pResult = new ShapeRefC(pShape);
return reinterpret_cast<jlong> (pResult);
}

0 comments on commit 78fff74

Please sign in to comment.