Skip to content

Commit

Permalink
CollisionGroup: refactor to avoid caching the GroupFilter
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Oct 14, 2024
1 parent 8623f31 commit 0b870e4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
28 changes: 18 additions & 10 deletions src/main/java/com/github/stephengold/joltjni/CollisionGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ of this software and associated documentation files (the "Software"), to deal
*/
public class CollisionGroup extends JoltPhysicsObject {
// *************************************************************************
// fields
// constructors

/**
* protect the {@code GroupFilter} from garbage collection
* Instantiate a group with the specified native object assigned but not
* owned.
*
* @param groupVa the virtual address of the native object to assign (not
* zero)
*/
private GroupFilter filter;
// *************************************************************************
// constructors
CollisionGroup(long groupVa) {
setVirtualAddress(groupVa, null);
}

/**
* Instantiate a group with the specified filter and IDs.
Expand All @@ -45,7 +49,6 @@ public class CollisionGroup extends JoltPhysicsObject {
* @param subGroupId the ID of the subgroup to which the body belongs
*/
public CollisionGroup(GroupFilter filter, int groupId, int subGroupId) {
this.filter = filter;
long filterVa = filter.va();
long groupVa = createGroup(filterVa, groupId, subGroupId);
setVirtualAddress(groupVa, () -> free(groupVa));
Expand All @@ -54,12 +57,16 @@ public CollisionGroup(GroupFilter filter, int groupId, int subGroupId) {
// new methods exposed

/**
* Access the group filter. The group is unaffected.
* Access the group filter.
*
* @return filter the pre-existing filter (not null)
* @return a new JVM object with the pre-existing native object assigned
*/
public GroupFilter getGroupFilter() {
return filter;
long groupVa = va();
long filterVa = getGroupFilter(groupVa);
GroupFilter result = new GroupFilter(filterVa);

return result;
}

/**
Expand Down Expand Up @@ -92,7 +99,6 @@ public int getSubGroupId() {
* @param filter the desired filter (not null, alias created)
*/
public void setGroupFilter(GroupFilter filter) {
this.filter = filter;
long groupVa = va();
long filterVa = filter.va();
setGroupFilter(groupVa, filterVa);
Expand Down Expand Up @@ -125,6 +131,8 @@ native private static long createGroup(

native private static void free(long groupVa);

native private static long getGroupFilter(long groupVa);

native private static int getGroupId(long groupVa);

native private static int getSubGroupId(long groupVa);
Expand Down
13 changes: 13 additions & 0 deletions src/main/native/glue/co/CollisionGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_free
delete pGroup;
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: getGroupFilter
* Signature: (J)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_getGroupFilter
(JNIEnv *, jclass, jlong groupVa) {
CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const GroupFilter * const pResult = pGroup->GetGroupFilter();
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: getGroupId
Expand Down

0 comments on commit 0b870e4

Please sign in to comment.