Rapier logo

Forces

Apply forces and impulses to rigid bodies to drive physics interactions, such as making objects jump or move programmatically.

Usage

To use methods (like applying forces or impulses) you first need to access the element using template ref. Then access to the instance

Basic example, making the cube jump with one click:

<script setup lang="ts">
import { TresCanvas } from '@tresjs/core'
import { Physics, RigidBody } from '@tresjs/rapier'
import { shallowRef } from 'vue'
const rigidCubeRef = shallowRef(null)
const jumpCube = () => {
  if (rigidCubeRef.value) {
    rigidCubeRef.value.instance.applyImpulse({ x: 0, y: 15, z: 0 }, true)
  }
}
</script>

<template>
  <TresCanvas window-size>
    <TresPerspectiveCamera :position="[11, 11, 11]" :look-at="[0, 0, 0]" />
    <Suspense>
      <Physics debug>
        <RigidBody ref="rigidCubeRef">
          <TresMesh :position="[0, 5, 0]" @click="jumpCube">
            <TresBoxGeometry />
            <TresMeshNormalMaterial />
          </TresMesh>
        </RigidBody>
      </Physics>
    </Suspense>
  </TresCanvas>
</template>
For more info check Forces and Impulses