Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix avatar physics capsule size mismatch #3624

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion avatars/avatars.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,9 +639,12 @@ class Avatar {
rightToe: _getOffset(modelBones.Right_toe),
});

// default avatar body height/width ratio
const heightFactor = 1.6;
const widthFactor = 0.6;
// height is defined as eyes to root
this.height = getHeight(object);
this.width = 0.36; // TODO : calculate this instead of hard coding it
this.width = widthFactor/heightFactor * this.height;
this.shoulderWidth = modelBones.Left_arm.getWorldPosition(new THREE.Vector3()).distanceTo(modelBones.Right_arm.getWorldPosition(new THREE.Vector3()));
this.leftArmLength = this.shoulderTransforms.leftArm.armLength;
this.rightArmLength = this.shoulderTransforms.rightArm.armLength;
Expand Down
21 changes: 13 additions & 8 deletions character-physics.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const groundStickOffset = 0.03;

const physicsScene = physicsManager.getScene();

const CHARACTER_CONTROLLER_HEIGHT_FACTOR = 1.6;
class CharacterPhysics {
constructor(character) {
this.character = character;
Expand All @@ -55,15 +54,20 @@ class CharacterPhysics {
this.characterWidth = characterWidth;
this.characterHeight = characterHeight;

const radius = (this.characterWidth / CHARACTER_CONTROLLER_HEIGHT_FACTOR) * this.characterHeight;
const height = this.characterHeight - radius * 2;
const radius = this.characterWidth / 2;
let height = this.characterHeight - radius * 2;

const contactOffset = (0.1 / CHARACTER_CONTROLLER_HEIGHT_FACTOR) * this.characterHeight;
const stepOffset = (0.5 / CHARACTER_CONTROLLER_HEIGHT_FACTOR) * this.characterHeight;
// avatar.height is the height of eye from the floor
// convert to full height by adding some more
const heightOffset = radius * 1.5;
this.yOffset = -heightOffset / 2; // add this after physx cct position output
height = height + heightOffset;

const contactOffset = 0.1 * this.characterHeight;
avaer marked this conversation as resolved.
Show resolved Hide resolved
const stepOffset = 0.5 * this.characterHeight;

const position = this.character.position
.clone()
.add(new THREE.Vector3(0, -this.characterHeight / 2, 0));
.clone();

if (this.characterController) {
physicsScene.destroyCharacterController(this.characterController);
Expand All @@ -79,7 +83,6 @@ class CharacterPhysics {
}
setPosition(p) {
localVector.copy(p);
localVector.y -= this.characterHeight * 0.5;
physicsScene.setCharacterControllerPosition(
this.characterController,
localVector
Expand Down Expand Up @@ -157,6 +160,7 @@ class CharacterPhysics {
timeDiffS,
this.characterController.position
);
this.characterController.position.y += this.yOffset;
// const collided = flags !== 0;
let grounded = !!(flags & 0x1);

Expand All @@ -175,6 +179,7 @@ class CharacterPhysics {
0,
localVector4
);
localVector4.y += this.yOffset;
const newGrounded = !!(flags & 0x1);
if (newGrounded) {
grounded = true;
Expand Down
6 changes: 1 addition & 5 deletions physics-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -630,14 +630,10 @@ class PhysicsScene extends EventTarget {
position,
physicsId
)

radius = radius + contactOffset;
height = height + radius * 2;

const halfHeight = height / 2;
const physicsObject = new THREE.Object3D()
const physicsMesh = new THREE.Mesh(
new CapsuleGeometry(radius, radius, halfHeight * 2),
new CapsuleGeometry(radius, radius, height + contactOffset * 2),
redMaterial
)
physicsMesh.visible = false
Expand Down