Search Unity

Is there any way I can apply a force vector at a certain location on a physics mesh?

Discussion in 'Physics for ECS' started by Kmsxkuse, Jan 23, 2020.

  1. Kmsxkuse

    Kmsxkuse

    Joined:
    Feb 15, 2019
    Posts:
    306
    I was following this wonderful tutorial linked below that used the old rigidbody based physics and I reached the point where buoyant force is applied at the center of every calculated subtriangle of the boat/block model's mesh.

    However, it does not seem like an equivalent method or even ability to do so is found in DOTS Physics. At least in my quick scan of the functions a PhysicsBody and PhysicsMass typed variable has to offer.

    Tutorial: https://www.habrador.com/tutorials/unity-boat-tutorial/3-buoyancy/

    In particular this section:
    Code (CSharp):
    1. // Calculate the buoyancy force
    2. Vector3 buoyancyForce = BuoyancyForce(rhoWater, triangleData);
    3.  
    4. // Add the force to the boat
    5. boatRB.AddForceAtPosition(buoyancyForce, triangleData.center);
    Current direct conversion from Mono to DOTS (I keep writing ECS) process (nonfunctional): https://github.com/Kmsxkuse/ProjectBoxes/blob/master/Assets/Scripts/Buoyancy.cs
     
  2. Nothke

    Nothke

    Joined:
    Dec 2, 2012
    Posts:
    112
    There are extensions to do that in Unity.Physics.Extensions. Note that you will need the PhysicsVelocity, PhysicsMass, Translation and Rotation components to perform it.

    Code (CSharp):
    1. using Unity.Physics.Extensions;
    2. ...
    3. physicsVelocity.ApplyImpulse(physicsMass, translation, rotation, impulse, point);
    Note that "impulse" is force in time, so you'll need to multiply the force with deltaTime.

    There is an alternative extension using a PhysicsWorld and a rigidbody index.