Skip to content

Commit

Permalink
add the CollisionGroup class
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Sep 26, 2024
1 parent bbd7e6d commit 8a9391a
Show file tree
Hide file tree
Showing 2 changed files with 265 additions and 0 deletions.
137 changes: 137 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/CollisionGroup.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
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;

/**
* Restrict which bodies can collide.
*
* @author Stephen Gold [email protected]
*/
public class CollisionGroup extends JoltPhysicsObject {
// *************************************************************************
// fields

/**
* protect the {@code GroupFilter} from garbage collection
*/
private GroupFilter filter;
// *************************************************************************
// constructors

/**
* Instantiate a group with the specified filter and IDs.
*
* @param filter the collision-group filter
* @param groupId the main group ID
* @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));
}
// *************************************************************************
// new methods exposed

/**
* Access the group filter. The group is unaffected.
*
* @return filter the pre-existing filter (not null)
*/
public GroupFilter getGroupFilter() {
return filter;
}

/**
* Return the main group ID. The group is unaffected.
*
* @return the ID value
*/
public int getGroupId() {
long groupVa = va();
int result = getGroupId(groupVa);

return result;
}

/**
* Return the sub-group ID. The group is unaffected.
*
* @return the ID value
*/
public int getSubGroupId() {
long groupVa = va();
int result = getSubGroupId(groupVa);

return result;
}

/**
* Replace the group filter.
*
* @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);
}

/**
* Alter the main group ID.
*
* @param id the desired ID
*/
public void setGroupId(int id) {
long groupVa = va();
setGroupId(groupVa, id);
}

/**
* Alter the sub-group ID.
*
* @param id the desired ID
*/
public void setSubGroupID(int id) {
long groupVa = va();
setSubGroupId(groupVa, id);
}
// *************************************************************************
// native private methods

native private static long createGroup(
long filterVa, int groupId, int subGroupId);

native private static void free(long groupVa);

native private static int getGroupId(long groupVa);

native private static int getSubGroupId(long groupVa);

native private static void setGroupFilter(long groupVa, long filterVa);

native private static void setGroupId(long groupVa, int id);

native private static void setSubGroupId(long groupVa, int id);
}
128 changes: 128 additions & 0 deletions src/main/native/glue/co/CollisionGroup.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
/*
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/CollisionGroup.h"
#include "auto/com_github_stephengold_joltjni_CollisionGroup.h"
#include "glue/glue.h"

using namespace JPH;

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: createGroup
* Signature: (JII)J
*/
JNIEXPORT jlong JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_createGroup
(JNIEnv *, jclass, jlong filterVa, jint groupId, jint subGroupId) {
const GroupFilter * const pFilter
= reinterpret_cast<GroupFilter *> (filterVa);
const CollisionGroup::GroupID gid = (CollisionGroup::GroupID) groupId;
const CollisionGroup::SubGroupID sgid
= (CollisionGroup::SubGroupID) subGroupId;
CollisionGroup * const pResult = new CollisionGroup(pFilter, gid, sgid);
TRACE_NEW("CollisionGroup", pResult)
return reinterpret_cast<jlong> (pResult);
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: free
* Signature: (J)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_free
(JNIEnv *, jclass, jlong groupVa) {
CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
TRACE_DELETE("CollisionGroup", pGroup)
delete pGroup;
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: getGroupId
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_getGroupId
(JNIEnv *, jclass, jlong groupVa) {
const CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const CollisionGroup::GroupID result = pGroup->GetGroupID();
return result;
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: getSubGroupId
* Signature: (J)I
*/
JNIEXPORT jint JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_getSubGroupId
(JNIEnv *, jclass, jlong groupVa) {
const CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const CollisionGroup::SubGroupID result = pGroup->GetSubGroupID();
return result;
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: setGroupFilter
* Signature: (JJ)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_setGroupFilter
(JNIEnv *, jclass, jlong groupVa, jlong filterVa) {
CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const GroupFilter * const pFilter
= reinterpret_cast<GroupFilter *> (filterVa);
pGroup->SetGroupFilter(pFilter);
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: setGroupId
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_setGroupId
(JNIEnv *, jclass, jlong groupVa, jint groupId) {
CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const CollisionGroup::GroupID gid = (CollisionGroup::GroupID) groupId;
pGroup->SetGroupID(gid);
}

/*
* Class: com_github_stephengold_joltjni_CollisionGroup
* Method: setSubGroupId
* Signature: (JI)V
*/
JNIEXPORT void JNICALL Java_com_github_stephengold_joltjni_CollisionGroup_setSubGroupId
(JNIEnv *, jclass, jlong groupVa, jint subGroupId) {
CollisionGroup * const pGroup
= reinterpret_cast<CollisionGroup *> (groupVa);
const CollisionGroup::SubGroupID sgid
= (CollisionGroup::SubGroupID) subGroupId;
pGroup->SetSubGroupID(sgid);
}

0 comments on commit 8a9391a

Please sign in to comment.