Skip to content

Commit

Permalink
add the ScaledShape class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 28, 2024
1 parent bfb0e6e commit 39cf428
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/ScaledShape.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright (c) 2024 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;

/**
* Apply scaling to an existing {@code Shape}.
*
* @author Stephen Gold [email protected]
*/
public class ScaledShape extends DecoratedShape {
// *************************************************************************
// constructors

/**
* Instantiate a shape based on the specified shape and scale factors.
*
* @param baseShape the unscaled base shape (not null)
* @param scaleFactors the desired scale factors (not null)
*/
public ScaledShape(Shape baseShape, Vec3 scaleFactors) {
long baseShapeVa = baseShape.va();
float scaleX = scaleFactors.getX();
float scaleY = scaleFactors.getY();
float scaleZ = scaleFactors.getZ();
long scaledShapeVa
= createScaledShape(baseShapeVa, scaleX, scaleY, scaleZ);
setVirtualAddress(scaledShapeVa, true);
}
// *************************************************************************
// new methods exposed

/**
* Copy the shape's scale factors.
*
* @return a new vector
*/
public Vec3 getScale() {
long scaledVa = va();
float scaleX = getScaleX(scaledVa);
float scaleY = getScaleY(scaledVa);
float scaleZ = getScaleZ(scaledVa);
Vec3 result = new Vec3(scaleX, scaleY, scaleZ);

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

native private static long createScaledShape(
long baseShapeVa, float scaleX, float scaleY, float scaleZ);

native private static float getScaleX(long scaledVa);

native private static float getScaleY(long scaledVa);

native private static float getScaleZ(long scaledVa);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
Copyright (c) 2024 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/Collision/Shape/ScaledShape.h>
#include "auto/com_github_stephengold_joltjni_ScaledShape.h"

using namespace JPH;

/*
* Class: com_github_stephengold_joltjni_ScaledShape
* Method: createScaledShape
* Signature: (JFFF)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_ScaledShape_createScaledShape
(JNIEnv *, jclass, jlong baseShapeVa, jfloat x, jfloat y, jfloat z) {
const Shape * const pBase = reinterpret_cast<Shape *> (baseShapeVa);
Vec3 factors(x, y, z);
ScaledShape * const pResult = new ScaledShape(pBase, factors);
return reinterpret_cast<jlong> (pResult);
}

inline static const Vec3& getScale(jlong scaledVa) {
const ScaledShape * const pScaled
= reinterpret_cast<ScaledShape *> (scaledVa);
const Vec3& result = pScaled->GetScale();
return result;
}

/*
* Class: com_github_stephengold_joltjni_ScaledShape
* Method: getScaleX
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleX
(JNIEnv *, jclass, jlong scaledVa) {
const Vec3& factors = getScale(scaledVa);
float result = factors.GetX();
return result;
}

/*
* Class: com_github_stephengold_joltjni_ScaledShape
* Method: getScaleY
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleY
(JNIEnv *, jclass, jlong scaledVa) {
const Vec3& factors = getScale(scaledVa);
float result = factors.GetY();
return result;
}
/*
* Class: com_github_stephengold_joltjni_ScaledShape
* Method: getScaleZ
* Signature: (J)F
*/
JNIEXPORT jfloat JNICALL Java_com_github_stephengold_joltjni_ScaledShape_getScaleZ
(JNIEnv *, jclass, jlong scaledVa) {
const Vec3& factors = getScale(scaledVa);
float result = factors.GetZ();
return result;
}

0 comments on commit 39cf428

Please sign in to comment.