This repository has been archived by the owner on Jun 16, 2023. It is now read-only.
Converting Positions Between Local Coordinate Spaces. #55
Reality-Dev
started this conversation in
Show and tell
Replies: 3 comments
-
Here are some updates, including the ability to use /// Converts a position from the local space of the Object on which you called this method to the local space of a reference Object.
/// - Parameters:
/// - position: The position given in the local space of the Object.
/// - referenceObject: The Object that defines a frame of reference. Set this to nil to indicate world space.
/// - Returns: The position specified relative to referenceObject.
func convertPosition(_ position: simd_float3, to referenceObject: Object?) -> simd_float3 {
let worldSpacePosition = (self.worldMatrix * simd_float4x4(translation: position)).translation
if let referenceObject = referenceObject {
return (referenceObject.worldMatrix.inverse * simd_float4x4(translation: worldSpacePosition)).translation
} else {
return worldSpacePosition
}
}
/// Converts a position from the local space of a reference Object to the local space of the Object on which you called this method.
/// - Parameters:
/// - position: The position specified relative to referenceObject.
/// - referenceObject: The Object that defines a frame of reference. Set this to nil to indicate world space.
/// - Returns: The position given in the local space of the Object.
func convertPosition(_ position: SIMD3<Float>, from referenceObject: Object?) -> SIMD3<Float> {
var worldSpacePosition = position
if let referenceObject = referenceObject {
worldSpacePosition = (referenceObject.worldMatrix * simd_float4x4(translation: position)).translation
}
return (self.worldMatrix.inverse * simd_float4x4(translation: worldSpacePosition)).translation
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here are some updates for converting direction vectors. /// Converts a direction vector from the local space of the Object on which you called this method to the local space of a reference Object.
/// - Parameters:
/// - direction: The direction vector given in the local space of the Object.
/// - referenceObject: The Object that defines a frame of reference. Set this to nil to indicate world space.
/// - Returns: The direction vector specified relative to referenceObject.
func convert(
direction: SIMD3<Float>,
to referenceObject: Object?
) -> SIMD3<Float> {
let worldSpaceDirection = worldOrientation.act(direction)
if let referenceObject = referenceObject {
return referenceObject.worldMatrix.inverse.orientation.act(worldSpaceDirection)
} else {
return worldSpaceDirection
}
}
/// Converts a direction vector from the local space of a reference Object to the local space of the Object on which you called this method.
/// - Parameters:
/// - direction: The direction vector specified relative to referenceObject.
/// - referenceObject: The Object that defines a frame of reference. Set this to nil to indicate world space.
/// - Returns: The direction vector given in the local space of the Object.
func convert(
direction: SIMD3<Float>,
from referenceObject: Object?
) -> SIMD3<Float> {
var worldSpaceDirection = direction
if let referenceObject = referenceObject {
worldSpaceDirection = referenceObject.worldOrientation.act(direction)
}
return self.worldMatrix.inverse.orientation.act(worldSpaceDirection)
} |
Beta Was this translation helpful? Give feedback.
0 replies
-
Thanks @Reality-Dev... these functions have been integrated into Satin's Object class. There were some changes I made, so please let me know if they work for you. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Coming from RealityKit I am used to using methods on the TransformComponent like these, where transform values can be retrieved and set relative to any other Entity in the scene graph:
_ position: SIMD3<Float>,
relativeTo referenceEntity: Entity?
)
_ orientation: simd_quatf,
relativeTo referenceEntity: Entity?
)
_ scale: SIMD3<Float>,
relativeTo referenceEntity: Entity?
)
And methods like these that can convert from one Entity's local coordinate space to another Entity's local coordinate space:
position: SIMD3<Float>,
to referenceEntity: Entity?
) -> SIMD3<Float>
position: SIMD3<Float>,
from referenceEntity: Entity?
) -> SIMD3<Float>
Out of necessity, and as a start, I have created a couple of these for Satin:
Beta Was this translation helpful? Give feedback.
All reactions