Skip to content

Commit

Permalink
Updated the avatar radius and camera rotation (#10118)
Browse files Browse the repository at this point in the history
* updated the avatar radius to be the sum of the eyeOffset and the camera Near values
updated the camera rotation to use the world rotation

* removed the camera component near value as a const that is exported.
spawn avatar fetches camera's near value from camera component

* made the avatar radius part of the collider update and connected to the Avatar Controller Component

* removed dev log

* made adjustment requested in PR

---------

Co-authored-by: Josh Field <joshfield999@gmail.com>
  • Loading branch information
MbfloydIR and HexaField committed May 2, 2024
1 parent 40f7694 commit 970fc86
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { TargetCameraRotationComponent } from '@etherealengine/spatial/src/camer
import { PhysicsState } from '@etherealengine/spatial/src/physics/state/PhysicsState'
import { XRControlsState } from '@etherealengine/spatial/src/xr/XRState'
import { useEffect } from 'react'
import { CameraComponent } from '../../../../spatial/src/camera/components/CameraComponent'
import { setAvatarColliderTransform } from '../functions/spawnAvatarReceptor'
import { AvatarComponent } from './AvatarComponent'

export const AvatarControllerComponent = defineComponent({
Expand Down Expand Up @@ -100,19 +102,17 @@ export const AvatarControllerComponent = defineComponent({
const avatarComponent = useComponent(entity, AvatarComponent)
const avatarControllerComponent = useComponent(entity, AvatarControllerComponent)
const isCameraAttachedToAvatar = useHookstate(getMutableState(XRControlsState).isCameraAttachedToAvatar)
const camera = useComponent(Engine.instance.cameraEntity, CameraComponent)

useEffect(() => {
/** @todo fix this */
// getState(PhysicsState).physicsWorld.removeCollider(avatarControllerComponent.bodyCollider.value, false)
// const collider = createAvatarCollider(entity)
// avatarControllerComponent.bodyCollider.set(collider)
setAvatarColliderTransform(entity)

const cameraEntity = avatarControllerComponent.cameraEntity.value
if (cameraEntity && entityExists(cameraEntity) && hasComponent(cameraEntity, FollowCameraComponent)) {
const cameraComponent = getComponent(cameraEntity, FollowCameraComponent)
cameraComponent.offset.set(0, avatarComponent.eyeHeight.value, 0)
}
}, [avatarComponent.avatarHeight])
}, [avatarComponent.avatarHeight, camera.near])

useEffect(() => {
if (isCameraAttachedToAvatar.value) {
Expand Down
30 changes: 22 additions & 8 deletions packages/engine/src/avatar/functions/spawnAvatarReceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
UUIDComponent,
createEntity,
getComponent,
getOptionalComponent,
setComponent
} from '@etherealengine/ecs'
import { getState } from '@etherealengine/hyperflux'
Expand Down Expand Up @@ -62,7 +63,8 @@ import { AvatarAnimationComponent, AvatarRigComponent } from '../components/Avat
import { AvatarComponent } from '../components/AvatarComponent'
import { AvatarColliderComponent, AvatarControllerComponent } from '../components/AvatarControllerComponent'

export const avatarRadius = 0.125
import { CameraComponent } from '../../../../spatial/src/camera/components/CameraComponent'
import { eyeOffset } from '../systems/AvatarTransparencySystem'

export const spawnAvatarReceptor = (entityUUID: EntityUUID) => {
const entity = UUIDComponent.getEntityByUUID(entityUUID)
Expand Down Expand Up @@ -124,21 +126,33 @@ export const createAvatarCollider = (entity: Entity) => {
const colliderEntity = createEntity()
setComponent(entity, AvatarColliderComponent, { colliderEntity })

const avatarComponent = getComponent(entity, AvatarComponent)
const halfHeight = avatarComponent.avatarHeight * 0.5

setAvatarColliderTransform(colliderEntity)
setComponent(colliderEntity, EntityTreeComponent, { parentEntity: entity })
setComponent(colliderEntity, TransformComponent, {
position: new Vector3(0, halfHeight + 0.25, 0),
scale: new Vector3(avatarRadius, halfHeight - avatarRadius - 0.25, avatarRadius)
})
setComponent(colliderEntity, ColliderComponent, {
shape: Shapes.Capsule,
collisionLayer: CollisionGroups.Avatars,
collisionMask: AvatarCollisionMask
})
}

const avatarCapsuleOffset = 0.25
export const setAvatarColliderTransform = (entity: Entity) => {
const avatarCollider = getOptionalComponent(entity, AvatarColliderComponent)
if (!avatarCollider) {
return
}
const colliderEntity = avatarCollider.colliderEntity
const camera = getComponent(Engine.instance.cameraEntity, CameraComponent)
const avatarRadius = eyeOffset + camera.near
const avatarComponent = getComponent(entity, AvatarComponent)
const halfHeight = avatarComponent.avatarHeight * 0.5

setComponent(colliderEntity, TransformComponent, {
position: new Vector3(0, halfHeight + avatarCapsuleOffset, 0),
scale: new Vector3(avatarRadius, halfHeight - avatarRadius - avatarCapsuleOffset, avatarRadius)
})
}

export const createAvatarController = (entity: Entity) => {
const transform = getComponent(entity, TransformComponent)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import { AvatarComponent } from '../components/AvatarComponent'
import { AvatarHeadDecapComponent } from '../components/AvatarIKComponents'
import { TransparencyDitheringComponent } from '../components/TransparencyDitheringComponent'

const eyeOffset = 0.25
export const eyeOffset = 0.25
const execute = () => {
const selfAvatarEntity = AvatarComponent.getSelfAvatarEntity()
const deltaSeconds = getState(ECSState).deltaSeconds
Expand Down
2 changes: 1 addition & 1 deletion packages/spatial/src/camera/systems/CameraSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ const computeCameraFollow = (cameraEntity: Entity, referenceEntity: Entity) => {

targetPosition
.copy(followCamera.offset)
.applyQuaternion(targetTransform.rotation)
.applyQuaternion(TransformComponent.getWorldRotation(referenceEntity, targetTransform.rotation))
.add(TransformComponent.getWorldPosition(referenceEntity, new Vector3()))

// Run only if not in first person mode
Expand Down

0 comments on commit 970fc86

Please sign in to comment.