Physics

Updated: 12/08/2022

Create Physics Settings

Physics settings are created in the Editor and compiled in the .physics3.json file.
See “How to Set Up Physics” for more information on how to set up physics.
See “Exporting Data for Embedded Use” for more information on outputting physics calculation settings files.

Class for Physics Operation

The physics calculations and their application to the model are performed by the CubismPhysics class.
By passing the data in the .physics3.json file to the CubismPhysics::Create function in Native (C++) or the CubismPhysics.create function in Web (TypeScript) and Java,
an instance of the CubismPhysics class is created that reflects the physics operation settings.

// C++
csmString path = "example.physics3.json";
buffer = CreateBuffer(path.GetRawString(), &size);

CubismPhysics* _physics = CubismPhysics::Create(buffer, size);

DeleteBuffer(buffer, path.GetRawString());
// TypeScript
let path: string = "example.physics3.json";

fetch(path).then(
    (response) => 
    {
        return response.arrayBuffer();
    }
).then(
    (arrayBuffer) =>
    {
        let buffer: ArrayBuffer = arrayBuffer;
        let size: number = buffer.byteLength;

        _physics: CubismPhysics = CubismPhysics.create(buffer, size);
        deleteBuffer(buffer, path);
    }
);
// Java
String path = "example.physics3.json";
buffer = createBuffer(path);
CubismPhysics _physics = CubismPhysics.create(buffer);

At this time, Gravity and Wind described in the .physics3.json file are
overwritten by the variable _options of the CubismPhysics::Options structure in Native (C++) or the variable _options of the CubismPhysics.ts Option class in Web (TypeScript) and Java.
The variable _options can be accessed with the CubismPhysics::GetOptions function and CubismPhysics::SetOptions function in Native (C++),
or the CubismPhysics.getOptions function and CubismPhysics.setOptions function in Web (TypeScript) and Java.

// C++
/**
 * @brief Option
 * 
 * Physics options.
 */
struct Options
{
    CubismVector2 Gravity;          ///< Direction of gravity
    CubismVector2 Wind;             ///< Direction of wind
};

Options     _options;               ///< Option
// TypeScript
/**
 * Physics options
 */
export class Options
{
    constructor()
    {
        this.gravity = new CubismVector2(0, 0);
        this.wind = new CubismVector2(0, 0);
    }

    gravity: CubismVector2; // Direction of gravity.
    wind: CubismVector2;    // Direction of wind.
}

_options: Options;  // Option
//Java
/**
* Physics options.
*/
public static class Options {
    public final CubismVector2 gravity; // Direction of gravity.
    public final CubismVector2 wind; // Direction of wind.
  
    public Options() {
        gravity = new CubismVector2();
        wind = new CubismVector2();
    }

    /**
     * Copy constructor
     */
    public Options(Options options) {
        this.gravity = new CubismVector2(options.gravity);
        this.wind = new CubismVector2(options.wind);
    }
}

private Options _options; // Option

The values of Gravity and Wind to be overridden by the variable _options are as shown in the code below.

// C++
// set default options.
_options.Gravity.Y = -1.0f;
_options.Gravity.X = 0;
_options.Wind.X = 0;
_options.Wind.Y = 0;
// TypeScript
// set default options
this._options = new Options();
this._options.gravity.y = -1.0;
this._options.gravity.x = 0;
this._options.wind.x = 0;
this._options.wind.y = 0;
// Java
// set default options.
_options.gravity.x = 0.0f;
_options.gravity.y = -1.0f;
_options.wind.x = 0.0f;
_options.wind.y = 0.0f;

Apply to Models

The results of physics calculations can be applied to model parameters with the CubismPhysics::Evaluate function in Native (C++) or the CubismPhysics.evaluate function in Web (TypeScript) and Java.
In these situations, physics calculations are performed at the same time as the results of physics calculations are applied to model parameters.

// C++
// Physics settings
if (_physics ! = NULL)
{
    _physics->Evaluate(_model, deltaTimeSeconds);
}
// TypeScript
// Physics settings
if(_physics ! = null)
{
    _physics.evaluate(_model, deltaTimeSeconds);
}
// Java
// Physics settings
if (_physics ! = null) {
    _physics.evaluate(_model, deltaTimeSeconds);
}

Discard

The CubismPhysics class must also be destroyed at the time the model is released.

// C++
CubismPhysics::Delete(_physics);
// TypeScript
CubismPhysics.delete(_physics);

Java does not need to be disposed because garbage collection is responsible for deallocation.

Please let us know what you think about this article.