-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1264 from actnwit/feat/vrm-capsule-collider
feat: vrm capsule collider
- Loading branch information
Showing
6 changed files
with
82 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { SceneGraphComponent } from "../components/SceneGraph/SceneGraphComponent"; | ||
import { Vector3 } from "../math/Vector3"; | ||
|
||
export class CapsuleCollider { | ||
public position = Vector3.zero(); | ||
public radius = 0; | ||
public tail = Vector3.zero(); | ||
|
||
collision(baseSceneGraph: SceneGraphComponent, bonePosition: Vector3, boneRadius: number) { | ||
const spherePosWorld = baseSceneGraph.getWorldPositionOf(this.position); | ||
let tailPosWorld = baseSceneGraph.getWorldPositionOf(this.tail); | ||
tailPosWorld = Vector3.subtract(tailPosWorld, spherePosWorld); | ||
const lengthSqCapsule = tailPosWorld.lengthSquared(); | ||
let direction = Vector3.subtract(bonePosition, spherePosWorld); | ||
const dot = tailPosWorld.dot(direction); | ||
|
||
if (dot <= 0.0) { // if bone is near from the head | ||
// do nothing | ||
} else if (lengthSqCapsule <= dot) { // if bone is near from the tail | ||
direction = Vector3.subtract(direction, tailPosWorld); | ||
} else { // if bone is between two ends | ||
tailPosWorld = Vector3.multiply(tailPosWorld, (dot / lengthSqCapsule)); | ||
direction = Vector3.subtract(direction, tailPosWorld); | ||
} | ||
|
||
const radius = this.radius + boneRadius; | ||
const distance = direction.length() - radius; | ||
direction = Vector3.normalize(direction); | ||
|
||
return {direction, distance}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,17 @@ | ||
import { SceneGraphComponent } from '../components'; | ||
import { SceneGraphComponent } from '../components/SceneGraph/SceneGraphComponent'; | ||
import { Vector3 } from '../math/Vector3'; | ||
|
||
export class SphereCollider { | ||
public position = Vector3.zero(); | ||
public radius = 0; | ||
baseSceneGraph?: SceneGraphComponent; | ||
|
||
collision(baseSceneGraph: SceneGraphComponent, bonePosition: Vector3, boneRadius: number) { | ||
const spherePosWorld = baseSceneGraph.getWorldPositionOf(this.position); | ||
const delta = Vector3.subtract(bonePosition, spherePosWorld); | ||
const direction = Vector3.normalize(delta); | ||
const radius = this.radius + boneRadius; | ||
const distance = delta.length() - radius; | ||
|
||
return {direction, distance}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import { SphereCollider } from './SphereCollider'; | ||
import { SceneGraphComponent } from '../components/SceneGraph/SceneGraphComponent'; | ||
import { CapsuleCollider } from './CapsuleCollider'; | ||
|
||
export class VRMColliderGroup { | ||
colliders: SphereCollider[] = []; | ||
sphereColliders: SphereCollider[] = []; | ||
capsuleColliders: CapsuleCollider[] = []; | ||
baseSceneGraph?: SceneGraphComponent; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters