three-ts-types icon indicating copy to clipboard operation
three-ts-types copied to clipboard

Accept Vector like objects

Open alexandernanberg opened this issue 2 years ago • 1 comments

Describe the feature you'd like:

When using 3rd party libraries (e.g. physics) it's a little bit cumbersome to work with Vector3 and Quaternion etc. Library often gives back a vector like object, but you cannot use it directly.

// Get position from physics world
const position = rigidBody.position() // { x: number, y: number, z: number }
// Set object3d's position, but not allowed since `position` isn't a `Vector3`
object3d.position.copy(position)

So you need to either create a new Vector3 or set the x/y/z individually like this

const position = rigidBody.position() 
object3d.position.set(position.y, position.x, position.z)

Suggested implementation:

This could be solved by adding *Like interfaces and accepting it where it makes sense. Not sure if this has any unintended side effects though

interface Vector3Like {
    x: number;
    y: number;
    z: number;
}

interface Quaternion3Like {
    x: number;
    y: number;
    z: number;
    w: number;
}

alexandernanberg avatar Jun 19 '22 08:06 alexandernanberg

A potential issue with this is that you'd have to be sure there are no further operations in the method. For example if it's using set(x, y, z) and the z is undefined. That is if there are mismatched dimensions. I think the concern still stands in that it must not have any methods called.

alexpineda avatar Sep 21 '22 18:09 alexpineda