Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to dynamically update the mass of a RigidBody ? #711

Open
jojocello opened this issue Oct 9, 2024 · 0 comments
Open

How to dynamically update the mass of a RigidBody ? #711

jojocello opened this issue Oct 9, 2024 · 0 comments

Comments

@jojocello
Copy link

I need to dynamically change the mass of a cube (dynamic RigidBody) gliding on a tilted floor (fixed RigidBody). I explicitely create the cube with a mass and use setAdditionalMass() method to update the mass through a leva control.

The issue is that the mass variations don't affect the animation of the cube physics, although the RigidBody reference seems to be correctly updated with my new mass values.

Here's my full code snippet:

import { Box } from "@react-three/drei";
import { useFrame } from "@react-three/fiber";
import { Physics, RapierRigidBody, RigidBody } from "@react-three/rapier";
import { useControls } from "leva";
import React, { useEffect, useRef } from "react";

const Floor = () => {
  const floorRef = useRef<RapierRigidBody>(null);

  const { friction } = useControls({
    friction: { value: 0, min: 0, max: 1, step: 0.01 },
  });

  useFrame(() => {
    // if (floorRef.current) {
    //   console.log("Current floor friction:", floorRef.current.friction());
    // }
  });

  return (
    <RigidBody
      ref={floorRef}
      type="fixed"
      colliders="cuboid"
      name="floor"
      friction={friction}
    >
      <Box
        position={[0, -5, 0]}
        scale={[50, 2, 250]}
        rotation={[Math.PI / 20, 0, 0]} // Tilt the floor
        receiveShadow
      >
        <meshStandardMaterial color={"lightgray"} />
      </Box>
    </RigidBody>
  );
};

const Cube = () => {
  const cubeRef = useRef<RapierRigidBody>(null);

  const { mass } = useControls({
    mass: { value: 30, min: 30, max: 100000, step: 1 },
  });

  useEffect(() => {
    if (cubeRef.current) {
      // Set the new mass when it changes
      cubeRef.current.setAdditionalMass(mass, true);
    }
  }, [mass]);

  useFrame(() => {
    if (cubeRef.current) {
      console.log("Current cube mass:", cubeRef.current.mass());
    }
  });

  const size = 3;

  return (
    <RigidBody
      ref={cubeRef}
      colliders="cuboid"
      mass={mass} // Initial mass
      position={[0, 40, -100]}
      friction={0}
      // ccd={true}
    >
      <Box scale={[size, size, size]} castShadow receiveShadow name="cube">
        <meshStandardMaterial color={"lightgreen"} />
      </Box>
    </RigidBody>
  );
};

export const CubeMassGliding: React.FC = () => {
  return (
    <Physics gravity={[0, -9.81, 0]} debug>
      <group>
        <Cube />
        <Floor />
      </group>
      <axesHelper />
    </Physics>
  );
};

The expectation is for seeing an acceleration of the cube gliding while increasing its mass, and a deceleration while decreasing it.

I am looking for insights into why this might be happening and how to resolve it, because I couldn't find any demo about mass handling in the github repo. Is there a specific configuration that I am missing, or could this be a potential bug in the library ? Any help or guidance would be greatly appreciated.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant