Skip to content

Commit

Permalink
Merge pull request #39 from LeXXik/ccd
Browse files Browse the repository at this point in the history
Allow to change motion quality
  • Loading branch information
yohami authored May 20, 2024
2 parents 0fd914a + 89aa3db commit 8c36fe6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 7 deletions.
29 changes: 28 additions & 1 deletion src/physics/jolt/back/operators/modifier.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import {
CMD_ADD_FORCE, CMD_ADD_IMPULSE, CMD_ADD_TORQUE, CMD_APPLY_BUOYANCY_IMPULSE, CMD_CHANGE_GRAVITY,
CMD_CHAR_SET_LIN_VEL, CMD_CHAR_SET_SHAPE, CMD_MOVE_BODY, CMD_MOVE_KINEMATIC, CMD_PAIR_BODY,
CMD_REPORT_SET_SHAPE, CMD_RESET_VELOCITIES, CMD_SET_ANG_VEL, CMD_SET_DOF, CMD_SET_DRIVER_INPUT,
CMD_SET_GRAVITY_FACTOR, CMD_SET_LIN_VEL, CMD_SET_MOTION_TYPE, CMD_SET_OBJ_LAYER,
CMD_SET_GRAVITY_FACTOR, CMD_SET_LIN_VEL, CMD_SET_MOTION_QUALITY, CMD_SET_MOTION_TYPE, CMD_SET_OBJ_LAYER,
CMD_SET_USER_DATA, CMD_TOGGLE_GROUP_PAIR, CMD_USE_MOTION_STATE, COMPONENT_SYSTEM_CHAR,
MOTION_QUALITY_DISCRETE,
MOTION_TYPE_DYNAMIC, MOTION_TYPE_KINEMATIC
} from '../../constants.mjs';

Expand Down Expand Up @@ -143,6 +144,10 @@ class Modifier {
case CMD_SET_DOF:
ok = this._setDOF(cb);
break;

case CMD_SET_MOTION_QUALITY:
ok = this._setMotionQuality(cb);
break;
}

return ok;
Expand Down Expand Up @@ -640,6 +645,28 @@ class Modifier {
return true;
}

_setMotionQuality(cb) {
const backend = this._backend;
const Jolt = backend.Jolt;
const index = cb.read(BUFFER_READ_UINT32);
const body = backend.tracker.getBodyByPCID(index);
const quality = cb.read(BUFFER_READ_UINT8);

const jQuality = quality === MOTION_QUALITY_DISCRETE ?
Jolt.EMotionQuality_Discrete : Jolt.EMotionQuality_LinearCast;

try {
backend.bodyInterface.SetMotionQuality(body.GetID(), jQuality);
} catch (e) {
if ($_DEBUG) {
Debug.error(e);
}
return false;
}

return true;
}

_getBody(cb) {
const index = cb.read(BUFFER_READ_UINT32);
return this._backend.tracker.getBodyByPCID(index);
Expand Down
2 changes: 2 additions & 0 deletions src/physics/jolt/constants.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ export const CMD_SET_DOF = 36;
export const CMD_COLLIDE_POINT = 37;
export const CMD_COLLIDE_SHAPE_IDX = 38;

export const CMD_SET_MOTION_QUALITY = 39;

// Constraints 500+

export const CMD_JNT_SET_ENABLED = 500;
Expand Down
33 changes: 27 additions & 6 deletions src/physics/jolt/front/body/component.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import {
BUFFER_WRITE_UINT8, BUFFER_WRITE_VEC32, CMD_ADD_FORCE, CMD_ADD_IMPULSE,
CMD_APPLY_BUOYANCY_IMPULSE, CMD_DESTROY_BODY, CMD_MOVE_BODY, CMD_MOVE_KINEMATIC,
CMD_RESET_VELOCITIES, CMD_SET_ANG_VEL, CMD_SET_DOF, CMD_SET_GRAVITY_FACTOR, CMD_SET_LIN_VEL,
CMD_SET_MOTION_TYPE, CMD_SET_OBJ_LAYER, CMD_USE_MOTION_STATE, MOTION_QUALITY_DISCRETE,
MOTION_TYPE_DYNAMIC, MOTION_TYPE_KINEMATIC, MOTION_TYPE_STATIC, OBJ_LAYER_NON_MOVING,
OMP_CALCULATE_MASS_AND_INERTIA, OMP_MASS_AND_INERTIA_PROVIDED, OPERATOR_CLEANER,
OPERATOR_MODIFIER, SHAPE_CONVEX_HULL, SHAPE_HEIGHTFIELD, SHAPE_MESH
CMD_SET_MOTION_QUALITY, CMD_SET_MOTION_TYPE, CMD_SET_OBJ_LAYER, CMD_USE_MOTION_STATE,
MOTION_QUALITY_DISCRETE, MOTION_TYPE_DYNAMIC, MOTION_TYPE_KINEMATIC, MOTION_TYPE_STATIC,
OBJ_LAYER_NON_MOVING, OMP_CALCULATE_MASS_AND_INERTIA, OMP_MASS_AND_INERTIA_PROVIDED,
OPERATOR_CLEANER, OPERATOR_MODIFIER, SHAPE_CONVEX_HULL, SHAPE_HEIGHTFIELD, SHAPE_MESH
} from '../../constants.mjs';

const vec3 = new Vec3();
Expand Down Expand Up @@ -348,17 +348,38 @@ class BodyComponent extends ShapeComponent {
}

/**
* Motion quality, or how well it detects collisions when it has a high velocity.
* Following enum aliases available:
* Changes the body motion quality. Following constants available:
* ```
* MOTION_QUALITY_DISCRETE
* ```
* ```
* MOTION_QUALITY_LINEAR_CAST
* ```
*
* Use linear cast (CCD) for fast moving objects, in other cases prefer discrete one since it
* is cheaper.
*
* @param {number} quality - Quality constant.
*/
set motionQuality(quality) {
if (this._motionQuality === quality) {
return;
}

if ($_DEBUG) {
Debug.checkUint(quality, `Invalid motion quality: ${quality}`);
}

this._motionQuality = quality;
this.system.addCommand(
OPERATOR_MODIFIER, CMD_SET_MOTION_QUALITY, this._index,
quality, BUFFER_WRITE_UINT8, false
);
}

/**
* Motion quality, or how well it detects collisions when it has a high velocity.
*
* @returns {number} Enum number, representing the collision detection algorithm for this body.
* @defaultValue MOTION_QUALITY_DISCRETE
*/
Expand Down

0 comments on commit 8c36fe6

Please sign in to comment.