decentraland / decentraland/creator-hub
Helper: local space for apply impulse to player
- Dominant language
- TypeScript
- Stars
- 7
- Forks
- 14
- Avg merge
- 2d 8h
- Merged PRs (30d)
- 44
Description
The new ApplyImpulseToPlayer function always sets the direction of the impulse to world space. We attempted to add support for local space, but ran out of time in the shape. We could achieve this with an SDK-side helper, no need to do anything on the engine
There are two kinds of forces: Impulses to the player and Forces
With `applyImpulseToPlayer`, the local space is always the player, so if there are many local forces they can be all calculated together as one transform operation.
With `applyForceToPlayer`, the local space would be relative to the origin entity, so you could in theory have many local forces together, each using a different entity as their local reference, so the calculation is potentially harder.
One could argue that having local forces with `applyForceToPlayer` could potentially be expensive to calculate in a scenario with many forces. But for `applyImpulseToPlayer`, since it's always the same local space, it should be cheap.
Some context, these are the docs we wrote, this is the final interface we want for the users. We're missing the implementation of this:
By default, forces and impulses are applied in **world space** — the x, y, and z axes are fixed relative to the scene. You can instead apply them in **local space**, which is relative to the player's current facing direction. This is useful for mechanics like a "forward boost" that pushes the player in the direction they're looking.
To specify the space, pass a `PhysicsForceSpace` value as the last argument:
```ts
import { Physics, PhysicsForceSpace } from '@dcl/sdk/ecs'
import { Vector3 } from '@dcl/sdk/math'
// Push the player forward relative to where they're facing
Physics.applyImpulseToPlayer(
Vector3.create(0, 5, 15),
PhysicsForceSpace.PFS_LOCAL
)
```
```ts
import { Physics, PhysicsForceSpace } from '@dcl/sdk/ecs'
import { Vector3 } from '@dcl/sdk/math'
const boostEntity = engine.addEntity()
// Continuously push the player forward relative to their facing direction
Physics.applyForceToPlayer(
boostEntity,
Vector3.create(0, 0, 10),
PhysicsForceSpace.PFS_LOCAL
)
```
The `PhysicsForceSpace` enum has two values:
* `PhysicsForceSpace.PFS_WORLD`: Default. Force is applied in world-space coordinates.
* `PhysicsForceSpace.PFS_LOCAL`: Force is rotated to match the player's current rotation before being applied.
Contributor guide
Assessment
This issue has not been assessed yet.