Skip to content

Commit

Permalink
samples: invoke getPtr() so Ref subclasses needn't implement so much
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Nov 7, 2024
1 parent 5d06555 commit 7879fb8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 73 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ else if (sSceneName.equals( "ObstacleCourse") )
{
CharacterVirtualSettings settings=new CharacterVirtualSettings();
settings.setShape ( mStandingShape);
settings.setInnerBodyShape ( mInnerStandingShape);
settings.setInnerBodyShape ( mInnerStandingShape.getPtr());
settings.setSupportingVolume (new Plane(Vec3.sAxisY(), -cCharacterRadiusStanding)); // Accept contacts that touch the lower sphere of the capsule
mAnimatedCharacterVirtualWithInnerBody = new CharacterVirtual(settings, cCharacterVirtualWithInnerBodyPosition, Quat.sIdentity(), 0, mPhysicsSystem).toRef();
mAnimatedCharacterVirtualWithInnerBody.getPtr().setCharacterVsCharacterCollision(mCharacterVsCharacterCollision);
Expand Down Expand Up @@ -689,7 +689,7 @@ else if (pos.yy() > cReversingVerticallyMovingPosition.yy() + 5.0f)
if (character.getGroundState() == EGroundState.OnGround)
velocity = Vec3.sZero();
else
velocity = Op.add(Op.multiply(character.getLinearVelocity() , mAnimatedCharacter.getUp()) , Op.multiply(mPhysicsSystem.getGravity() , inParams.mDeltaTime));
velocity = Op.add(Op.multiply(character.getLinearVelocity() , mAnimatedCharacter.getPtr().getUp()) , Op.multiply(mPhysicsSystem.getGravity() , inParams.mDeltaTime));
Op.plusEquals(velocity , Op.multiply((float)Math.sin(mTime) , cCharacterVelocity));
character.setLinearVelocity(velocity);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,23 @@ void ProcessInput(const ProcessInputParams &inParams)
public void PrePhysicsUpdate(PreUpdateParams inParams)
{
// Calculate up vector based on position on planet surface
Vec3 old_up = mCharacter.getUp();
Vec3 up =new Vec3(mCharacter.getPosition()).normalized();
Vec3 old_up = mCharacter.getPtr().getUp();
Vec3 up =new Vec3(mCharacter.getPtr().getPosition()).normalized();
mCharacter.getPtr().setUp(up);

// Rotate capsule so it points up relative to the planet surface
mCharacter.getPtr().setRotation(Op.multiply(Quat.sFromTo(old_up, up) , mCharacter.getRotation()).normalized());
mCharacter.getPtr().setRotation(Op.multiply(Quat.sFromTo(old_up, up) , mCharacter.getPtr().getRotation()).normalized());

// Draw character pre update (the sim is also drawn pre update)
if (Jolt.implementsDebugRendering()) {
mCharacter.getShape().draw(mDebugRenderer, mCharacter.getCenterOfMassTransform(), Vec3.sReplicate(1.0f), Color.sGreen, false, true);
mCharacter.getPtr().getShape().draw(mDebugRenderer, mCharacter.getPtr().getCenterOfMassTransform(), Vec3.sReplicate(1.0f), Color.sGreen, false, true);
}

// Determine new character velocity
Vec3 current_vertical_velocity = Op.multiply(mCharacter.getLinearVelocity().dot(up) , up);
Vec3 ground_velocity = mCharacter.getGroundVelocity();
Vec3 current_vertical_velocity = Op.multiply(mCharacter.getPtr().getLinearVelocity().dot(up) , up);
Vec3 ground_velocity = mCharacter.getPtr().getGroundVelocity();
Vec3 new_velocity;
if (mCharacter.getGroundState() == EGroundState.OnGround // If on ground
if (mCharacter.getPtr().getGroundState() == EGroundState.OnGround // If on ground
&& Op.subtract(current_vertical_velocity , ground_velocity).dot(up) < 0.1f) // And not moving away from ground
{
// Assume velocity of ground when on ground
Expand Down Expand Up @@ -173,23 +173,23 @@ RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch)
{
// Pivot is center of character + distance behind based on the heading and pitch of the camera.
Vec3 fwd = new Vec3(Math.cos(inCameraPitch) * Math.cos(inCameraHeading), Math.sin(inCameraPitch), Math.cos(inCameraPitch) * Math.sin(inCameraHeading));
RVec3 cam_pos = Op.subtract(mCharacter.getPosition() , Op.multiply(5.0f , Op.rotate(mCharacter.getRotation() , fwd)));
return RMat44.sRotationTranslation(mCharacter.getRotation(), cam_pos);
RVec3 cam_pos = Op.subtract(mCharacter.getPtr().getPosition() , Op.multiply(5.0f , Op.rotate(mCharacter.getPtr().getRotation() , fwd)));
return RMat44.sRotationTranslation(mCharacter.getPtr().getRotation(), cam_pos);
}

void SaveState(StateRecorder inStream)
{
mCharacter.saveState(inStream);
mCharacter.getPtr().saveState(inStream);

// Save character up, it's not stored by default but we use it in this case update the rotation of the character
inStream.write(mCharacter.getUp());
inStream.write(mCharacter.getPtr().getUp());
}

void RestoreState(StateRecorder inStream)
{
mCharacter.getPtr().restoreState(inStream);

Vec3 up = mCharacter.getUp();
Vec3 up = mCharacter.getPtr().getUp();
inStream.readVec3(up);
mCharacter.getPtr().setUp(up);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void PrePhysicsUpdate(PreUpdateParams inParams)

// Update the character so it stays relative to the space ship
RMat44 new_space_ship_transform = mBodyInterface.getCenterOfMassTransform(mSpaceShip);
mCharacter.getPtr().setPosition(Op.multiply(Op.multiply(new_space_ship_transform , mSpaceShipPrevTransform.inversed()) , mCharacter.getPosition()));
mCharacter.getPtr().setPosition(Op.multiply(Op.multiply(new_space_ship_transform , mSpaceShipPrevTransform.inversed()) , mCharacter.getPtr().getPosition()));

// Update the character rotation and its up vector to match the new up vector of the ship
mCharacter.getPtr().setUp(new_space_ship_transform.getAxisY());
Expand All @@ -118,22 +118,22 @@ public void PrePhysicsUpdate(PreUpdateParams inParams)
// Draw character pre update (the sim is also drawn pre update)
// Note that we have first updated the position so that it matches the new position of the ship
if(Jolt.implementsDebugRendering()){
mCharacter.getShape().draw(mDebugRenderer, mCharacter.getCenterOfMassTransform(), Vec3.sReplicate(1.0f), Color.sGreen, false, true);
mCharacter.getPtr().getShape().draw(mDebugRenderer, mCharacter.getPtr().getCenterOfMassTransform(), Vec3.sReplicate(1.0f), Color.sGreen, false, true);
} // JPH_DEBUG_RENDERER

// Determine new character velocity
Vec3 current_vertical_velocity =Op.multiply( mCharacter.getLinearVelocity().dot(mSpaceShipPrevTransform.getAxisY()) , mCharacter.getUp());
Vec3 ground_velocity = mCharacter.getGroundVelocity();
Vec3 current_vertical_velocity =Op.multiply( mCharacter.getPtr().getLinearVelocity().dot(mSpaceShipPrevTransform.getAxisY()) , mCharacter.getPtr().getUp());
Vec3 ground_velocity = mCharacter.getPtr().getGroundVelocity();
Vec3 new_velocity;
if (mCharacter.getGroundState() == EGroundState.OnGround // If on ground
if (mCharacter.getPtr().getGroundState() == EGroundState.OnGround // If on ground
&& (current_vertical_velocity.getY() - ground_velocity.getY()) < 0.1f) // And not moving away from ground
{
// Assume velocity of ground when on ground
new_velocity = ground_velocity;

// Jump
if (mJump)
Op.plusEquals(new_velocity , Op.multiply(cJumpSpeed , mCharacter.getUp()));
Op.plusEquals(new_velocity , Op.multiply(cJumpSpeed , mCharacter.getPtr().getUp()));
}
else
new_velocity = current_vertical_velocity;
Expand Down Expand Up @@ -186,12 +186,12 @@ RMat44 GetCameraPivot(float inCameraHeading, float inCameraPitch)
{
// Pivot is center of character + distance behind based on the heading and pitch of the camera
Vec3 fwd =new Vec3(Math.cos(inCameraPitch) * Math.cos(inCameraHeading), Math.sin(inCameraPitch), Math.cos(inCameraPitch) * Math.sin(inCameraHeading));
return RMat44.sTranslation(Op.subtract(Op.add(mCharacter.getPosition() ,new Vec3(0, cCharacterHeightStanding + cCharacterRadiusStanding, 0)) , Op.multiply(5.0f , fwd)));
return RMat44.sTranslation(Op.subtract(Op.add(mCharacter.getPtr().getPosition() ,new Vec3(0, cCharacterHeightStanding + cCharacterRadiusStanding, 0)) , Op.multiply(5.0f , fwd)));
}

void SaveState(StateRecorder inStream)
{
mCharacter.saveState(inStream);
mCharacter.getPtr().saveState(inStream);

inStream.write(mTime);
inStream.write(mSpaceShipPrevTransform);
Expand Down
24 changes: 12 additions & 12 deletions src/test/java/testjoltjni/app/samples/character/CharacterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ of this software and associated documentation files (the "Software"), to deal
public class CharacterTest extends CharacterBaseTest{
static final float cCollisionTolerance = 0.05f;
CharacterRef mCharacter;
RVec3 GetCharacterPosition(){return mCharacter.getPosition();}
RVec3 GetCharacterPosition(){return mCharacter.getPtr().getPosition();}

public void Initialize()
{
Expand All @@ -58,7 +58,7 @@ void PrePhysicsUpdate(PreUpdateParams inParams)
super.PrePhysicsUpdate(inParams);

// Draw state of character
DrawCharacterState(mCharacter, mCharacter.getWorldTransform(), mCharacter.getLinearVelocity());
DrawCharacterState(mCharacter.getPtr(), mCharacter.getPtr().getWorldTransform(), mCharacter.getPtr().getLinearVelocity());
}

public void PostPhysicsUpdate(float inDeltaTime)
Expand All @@ -71,9 +71,9 @@ void SaveState(StateRecorder inStream)
{
super.SaveState(inStream);

mCharacter.saveState(inStream);
mCharacter.getPtr().saveState(inStream);

boolean is_standing = mCharacter.getShape() == mStandingShape;
boolean is_standing = mCharacter.getPtr().getShape() == mStandingShape.getPtr();
inStream.write(is_standing);
}

Expand All @@ -83,20 +83,20 @@ void RestoreState(StateRecorder inStream)

mCharacter.getPtr().restoreState(inStream);

boolean is_standing = mCharacter.getShape() == mStandingShape; // Initialize variable for validation mode
boolean is_standing = mCharacter.getPtr().getShape() == mStandingShape.getPtr(); // Initialize variable for validation mode
is_standing = inStream.readBoolean(is_standing);
mCharacter.getPtr().setShape(is_standing? mStandingShape : mCrouchingShape, Float.MAX_VALUE);
mCharacter.getPtr().setShape(is_standing? mStandingShape.getPtr() : mCrouchingShape.getPtr(), Float.MAX_VALUE);
}

void HandleInput(Vec3Arg inMovementDirection, boolean inJump, boolean inSwitchStance, float inDeltaTime)
{
// Cancel movement in opposite direction of normal when touching something we can't walk up
Vec3 movement_direction =new Vec3(inMovementDirection);
EGroundState ground_state = mCharacter.getGroundState();
EGroundState ground_state = mCharacter.getPtr().getGroundState();
if (ground_state == EGroundState.OnSteepGround
|| ground_state == EGroundState.NotSupported)
{
Vec3 normal = mCharacter.getGroundNormal();
Vec3 normal = mCharacter.getPtr().getGroundNormal();
normal.setY(0.0f);
float dot = normal.dot(movement_direction);
if (dot < 0.0f)
Expand All @@ -105,14 +105,14 @@ void HandleInput(Vec3Arg inMovementDirection, boolean inJump, boolean inSwitchSt

// Stance switch
if (inSwitchStance)
mCharacter.getPtr().setShape(mCharacter.getShape() == mStandingShape? mCrouchingShape : mStandingShape, 1.5f * mPhysicsSystem.getPhysicsSettings().getPenetrationSlop());
mCharacter.getPtr().setShape(mCharacter.getPtr().getShape() == mStandingShape.getPtr()? mCrouchingShape.getPtr() : mStandingShape.getPtr(), 1.5f * mPhysicsSystem.getPhysicsSettings().getPenetrationSlop());

if (sControlMovementDuringJump || mCharacter.isSupported())
if (sControlMovementDuringJump || mCharacter.getPtr().isSupported())
{
// Update velocity
Vec3 current_velocity =new Vec3(mCharacter.getLinearVelocity());
Vec3 current_velocity =new Vec3(mCharacter.getPtr().getLinearVelocity());
Vec3 desired_velocity =new Vec3(Op.multiply(sCharacterSpeed , movement_direction));
if (!desired_velocity.isNearZero() || current_velocity.getY() < 0.0f || !mCharacter.isSupported())
if (!desired_velocity.isNearZero() || current_velocity.getY() < 0.0f || !mCharacter.getPtr().isSupported())
desired_velocity.setY(current_velocity.getY());
Vec3 new_velocity =new Vec3(Op.add(Op.multiply(0.75f , current_velocity) , Op.multiply(0.25f , desired_velocity)));

Expand Down
Loading

0 comments on commit 7879fb8

Please sign in to comment.