Skip to content

Commit

Permalink
add the ConstBodyId interface
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jul 13, 2024
1 parent 2fae67d commit 3ba9403
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/github/stephengold/joltjni/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,10 +243,10 @@ public float getFriction() {
*
* @return the value
*/
public BodyId getId() {
public ConstBodyId getId() {
long bodyVa = va();
long bodyIdVa = getId(bodyVa);
BodyId result = new BodyId(bodyIdVa);
ConstBodyId result = new BodyId(bodyIdVa);

return result;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/github/stephengold/joltjni/BodyId.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ of this software and associated documentation files (the "Software"), to deal
*
* @author Stephen Gold [email protected]
*/
public class BodyId extends JoltPhysicsObject {
public class BodyId extends JoltPhysicsObject implements ConstBodyId {
// *************************************************************************
// constructors

Expand All @@ -39,14 +39,15 @@ public class BodyId extends JoltPhysicsObject {
super(idVa);
}
// *************************************************************************
// new methods exposed
// ConstBodyId methods

/**
* Return the body's index in the body array. The current instance is
* unaffected.
*
* @return the index (≥0)
*/
@Override
public int getIndex() {
long idVa = va();
int result = getIndex(idVa);
Expand All @@ -59,6 +60,7 @@ public int getIndex() {
*
* @return the sequence number (≥0)
*/
@Override
public int getSequenceNumber() {
long idVa = va();
int result = getSequenceNumber(idVa);
Expand All @@ -71,6 +73,7 @@ public int getSequenceNumber() {
*
* @return true if invalid, false if valid
*/
@Override
public boolean isInvalid() {
long idVa = va();
boolean result = isInvalid(idVa);
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/github/stephengold/joltjni/BodyInterface.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public class BodyInterface extends NonCopyable {
*
* @param bodyId the ID of the body to activate (not null)
*/
public void activateBody(BodyId bodyId) {
public void activateBody(ConstBodyId bodyId) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
activateBody(bodyInterfaceVa, bodyIdVa);
Expand All @@ -54,7 +54,7 @@ public void activateBody(BodyId bodyId) {
* @param bodyId the ID of the body to add (not null)
* @param activation whether to activate the body (not null)
*/
public void addBody(BodyId bodyId, EActivation activation) {
public void addBody(ConstBodyId bodyId, EActivation activation) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
int activationOrdinal = activation.ordinal();
Expand All @@ -68,10 +68,10 @@ public void addBody(BodyId bodyId, EActivation activation) {
* @param activationMode whether to activate the body (not null)
* @return the ID of the created body, or an invalid ID when out of bodies
*/
public BodyId createAndAddBody(
public ConstBodyId createAndAddBody(
BodyCreationSettings settings, EActivation activationMode) {
Body body = createBody(settings);
BodyId result = body.getId();
ConstBodyId result = body.getId();
addBody(result, activationMode);

return result;
Expand Down Expand Up @@ -100,7 +100,7 @@ public Body createBody(BodyCreationSettings settings) {
*
* @param bodyId the ID of the body to destroy (not null)
*/
public void destroyBody(BodyId bodyId) {
public void destroyBody(ConstBodyId bodyId) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
destroyBody(bodyInterfaceVa, bodyIdVa);
Expand All @@ -112,7 +112,7 @@ public void destroyBody(BodyId bodyId) {
* @param bodyId the ID of the body to locate (not null)
* @return a new vector
*/
public RVec3 getCenterOfMassPosition(BodyId bodyId) {
public RVec3 getCenterOfMassPosition(ConstBodyId bodyId) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
double xx = getCenterOfMassPositionX(bodyInterfaceVa, bodyIdVa);
Expand All @@ -129,7 +129,7 @@ public RVec3 getCenterOfMassPosition(BodyId bodyId) {
* @param bodyId the ID of the body (not null)
* @return a new vector
*/
public Vec3 getLinearVelocity(BodyId bodyId) {
public Vec3 getLinearVelocity(ConstBodyId bodyId) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
float x = getLinearVelocityX(bodyInterfaceVa, bodyIdVa);
Expand All @@ -145,7 +145,7 @@ public Vec3 getLinearVelocity(BodyId bodyId) {
*
* @param bodyId the ID of the body to remove (not null)
*/
public void removeBody(BodyId bodyId) {
public void removeBody(ConstBodyId bodyId) {
long bodyIdVa = bodyId.va();
removeBody(va(), bodyIdVa);
}
Expand All @@ -156,7 +156,7 @@ public void removeBody(BodyId bodyId) {
* @param bodyId the ID of the body to test (not null)
* @return true if active, otherwise false
*/
public boolean isActive(BodyId bodyId) {
public boolean isActive(ConstBodyId bodyId) {
long bodyIdVa = bodyId.va();
boolean result = isActive(va(), bodyIdVa);
return result;
Expand All @@ -168,7 +168,7 @@ public boolean isActive(BodyId bodyId) {
* @param bodyId the ID of the body to test (not null)
* @param omega the desired rates (not null, unaffected)
*/
public void setAngularVelocity(BodyId bodyId, Vec3Arg omega) {
public void setAngularVelocity(ConstBodyId bodyId, Vec3Arg omega) {
long bodyInterfaceVa = va();
long bodyIdVa = bodyId.va();
setAngularVelocity(bodyInterfaceVa, bodyIdVa,
Expand All @@ -181,7 +181,7 @@ public void setAngularVelocity(BodyId bodyId, Vec3Arg omega) {
* @param bodyId the ID of the body to test (not null)
* @param velocity the desired velocity (not null, unaffected)
*/
public void setLinearVelocity(BodyId bodyId, Vec3Arg velocity) {
public void setLinearVelocity(ConstBodyId bodyId, Vec3Arg velocity) {
long bodyIdVa = bodyId.va();
setLinearVelocity(va(), bodyIdVa,
velocity.getX(), velocity.getY(), velocity.getZ());
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/github/stephengold/joltjni/ConstBodyId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
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;

/**
* An immutable {@code BodyId}.
*
* @author Stephen Gold [email protected]
*/
public interface ConstBodyId extends ConstJoltPhysicsObject {
// *************************************************************************
// new methods exposed

/**
* Return the body's index in the body array. The current instance is
* unaffected.
*
* @return the index (≥0)
*/
int getIndex();

/**
* Return the body's sequence number. The current instance is unaffected.
*
* @return the sequence number (≥0)
*/
int getSequenceNumber();

/**
* Test whether the ID is valid. The current instance is unaffected.
*
* @return true if invalid, false if valid
*/
boolean isInvalid();
}
2 changes: 1 addition & 1 deletion src/test/java/testjoltjni/app/HelloWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public static void main(String[] argv)
// Now create a dynamic body to bounce on the floor
// Note that this uses the shorthand version of creating and adding a body to the world
BodyCreationSettings sphere_settings = new BodyCreationSettings(new SphereShape(0.5f), new RVec3(0f, 2f, 0f), new Quat(), EMotionType.Dynamic, OBJ_LAYER_MOVING);
BodyId sphere_id = body_interface.createAndAddBody(sphere_settings, EActivation.Activate);
ConstBodyId sphere_id = body_interface.createAndAddBody(sphere_settings, EActivation.Activate);

// Now you can interact with the dynamic body, in this case we're going to give it a velocity.
// (note that if we had used CreateBody then we could have set the velocity straight on the body before adding it to the physics system)
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/testjoltjni/junit/Test001.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ of this software and associated documentation files (the "Software"), to deal

import com.github.stephengold.joltjni.Body;
import com.github.stephengold.joltjni.BodyCreationSettings;
import com.github.stephengold.joltjni.BodyId;
import com.github.stephengold.joltjni.BodyInterface;
import com.github.stephengold.joltjni.BoxShapeSettings;
import com.github.stephengold.joltjni.ConstBodyId;
import com.github.stephengold.joltjni.EActivation;
import com.github.stephengold.joltjni.EMotionType;
import com.github.stephengold.joltjni.EPhysicsUpdateError;
Expand Down Expand Up @@ -105,7 +105,7 @@ public void test001() {
= new BodyCreationSettings(floorShapeSettings, floorLocation,
orientation, EMotionType.Static, objLayerNonMoving);
Body floor = bodyInterface.createBody(floorBodySettings);
BodyId floorId = floor.getId();
ConstBodyId floorId = floor.getId();
bodyInterface.addBody(floorId, EActivation.DontActivate);

Shape ballShape = new SphereShape(0.5f);
Expand All @@ -116,7 +116,7 @@ public void test001() {
BodyCreationSettings ballSettings = new BodyCreationSettings(
ballShape, ballLocation, orientation,
EMotionType.Dynamic, objLayerMoving);
BodyId ballId = bodyInterface.createAndAddBody(
ConstBodyId ballId = bodyInterface.createAndAddBody(
ballSettings, EActivation.Activate);
Vec3 ballVelocity = new Vec3(0f, -5f, 0f);
bodyInterface.setLinearVelocity(ballId, ballVelocity);
Expand Down

0 comments on commit 3ba9403

Please sign in to comment.