Skip to content

Commit

Permalink
add the AllHitRayCastBodyCollector class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 23, 2024
1 parent 39e3971 commit 0837e58
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
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;

/**
* Collect all results from a broad-phase ray cast. (native type: {@code
* AllHitCollisionCollector<RayCastBodyCollector>})
*
* @author Stephen Gold [email protected]
*/
public class AllHitRayCastBodyCollector extends RayCastBodyCollector {
// *************************************************************************
// constructors

/**
* Instantiate a default collector.
*/
public AllHitRayCastBodyCollector() {
long collectorVa = createDefault();
setVirtualAddress(collectorVa, true);
}
// *************************************************************************
// new methods exposed

/**
* Access the hit with the specified index.
*
* @param index (&ge;0, &lt;numHits)
* @return a new JVM object with the pre-existing native object assigned
*/
public BroadPhaseCastResult get(int index) {
long collectorVa = va();
long hitVa = getHit(collectorVa, index);
BroadPhaseCastResult result = new BroadPhaseCastResult(this, hitVa);

return result;
}

/**
* Access all the hits as an array.
*
* @return a new array of new JVM objects with pre-existing native objects
* assigned
*/
public BroadPhaseCastResult[] getHits() {
long collectorVa = va();
int numHits = countHits(collectorVa);
BroadPhaseCastResult[] result = new BroadPhaseCastResult[numHits];
for (int i = 0; i < numHits; ++i) {
long hitVa = getHit(collectorVa, i);
result[i] = new BroadPhaseCastResult(this, hitVa);
}

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

native private static int countHits(long collectorVa);

native private static long createDefault();

native private static long getHit(long collectorVa, int hitIndex);
}
72 changes: 72 additions & 0 deletions src/main/native/glue/a/AllHitRayCastBodyCollector.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
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/BroadPhase/BroadPhaseQuery.h>
#include <Jolt/Physics/Collision/CastResult.h>
#include <Jolt/Physics/Collision/CollisionCollectorImpl.h>
#include "auto/com_github_stephengold_joltjni_AllHitRayCastBodyCollector.h"
#include "glue/glue.h"

using namespace JPH;

/*
* Class: com_github_stephengold_joltjni_AllHitRayCastBodyCollector
* Method: countHits
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_AllHitRayCastBodyCollector_countHits
(JNIEnv *, jclass, jlong collectorVa) {
const AllHitCollisionCollector<RayCastBodyCollector> * const pCollector
= reinterpret_cast<AllHitCollisionCollector<RayCastBodyCollector> *> (collectorVa);
const Array<BroadPhaseCastResult>::size_type result = pCollector->mHits.size();
return result;
}

/*
* Class: com_github_stephengold_joltjni_AllHitRayCastBodyCollector
* Method: createDefault
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_AllHitRayCastBodyCollector_createDefault
(JNIEnv *, jclass) {
AllHitCollisionCollector<RayCastBodyCollector> * const pCollector
= new AllHitCollisionCollector<RayCastBodyCollector>();
TRACE_NEW("AllHitCollisionCollector<RayCastBodyCollector>", pCollector)
return reinterpret_cast<jlong> (pCollector);
}

/*
* Class: com_github_stephengold_joltjni_AllHitRayCastBodyCollector
* Method: getHit
* Signature: (JI)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_AllHitRayCastBodyCollector_getHit
(JNIEnv *, jclass, jlong collectorVa, jint index) {
const AllHitCollisionCollector<RayCastBodyCollector> * const pCollector
= reinterpret_cast<AllHitCollisionCollector<RayCastBodyCollector> *> (collectorVa);
const BroadPhaseCastResult * const pResult = &pCollector->mHits.at(index);
return reinterpret_cast<jlong> (pResult);
}

0 comments on commit 0837e58

Please sign in to comment.