<commit_before>// For conditions of distribution and use, see copyright notice in LICENSE

#pragma once

#include "CoreDefines.h"
#include "IComponent.h"
#include "AssetReference.h"
#include "AssetFwd.h"
#include "Geometry/AABB.h"
#include "PhysicsModuleApi.h"
#include "PhysicsModuleFwd.h"

#include <LinearMath/btMotionState.h>

class EC_Placeable;
class EC_Terrain;

/// Physics rigid body entity-component
/** <table class="header">
    <tr>
    <td>
    <h2>RigidBody</h2>
    Physics rigid body entity-component

    Registered by Physics::PhysicsModule.

    <b>Attributes</b>:
    <ul>
    <li>float: mass
    <div>@copydoc mass</div>
    <li>enum: shapeType
    <div>@copydoc shapeType</div>
    <li>float3: size
    <div>@copydoc size</div>
    <li>AssetReference: collisionMeshRef
    <div>@copydoc @copydoc collisionMeshRef</div>
    <li>float: friction
    <div>@copydoc friction</div>
    <li>float: restitution
    <div>@copydoc restitution</div>
    <li>float: linearDamping
    <div>@copydoc linearDamping</div>
    <li>float: angularDamping
    <div>@copydoc angularDamping</div>
    <li>float3: linearFactor
    <div>@copydoc linearFactor</div>
    <li>float3: angularFactor
    <div>@copydoc angularFactor</div>
    <li>bool: kinematic
    <div>@copydoc kinematic</div>
    <li>bool: phantom
    <div>@copydoc phantom</div>
    <li>bool: drawDebug
    <div>@copydoc drawDebug</div>
    <li>float3: linearVelocity
    <div>@copydoc linearVelocity</div>
    <li>float3: angularVelocity
    <div>@copydoc angularVelocity</div>
    <li>int: collisionLayer
    <div>@copydoc collisionLayer</div>
    <li>int: collisionMask
    <div>@copydoc collisionMask</div>
    </ul>

    <b>Exposes the following scriptable functions:</b>
    <ul>
    <li> "SetShapeFromVisibleMesh": @copydoc SetShapeFromVisibleMesh
    <li> "SetLinearVelocity": @copydoc SetLinearVelocity
    <li> "SetAngularVelocity": @copydoc SetAngularVelocity
    <li> "ApplyForce": @copydoc ApplyForce
    <li> "ApplyTorque": @copydoc ApplyTorque
    <li> "ApplyImpulse": @copydoc ApplyImpulse
    <li> "ApplyTorqueImpulse": @copydoc ApplyTorqueImpulse
    <li> "Activate": @copydoc Activate
    <li> "IsActive": @copydoc IsActive
    <li> "KeepActive": @copydoc KeepActive
    <li> "ResetForces": @copydoc ResetForces
    <li> "SetRotation": @copydoc SetRotation
    <li> "Rotate": @copydoc Rotate
    <li> "GetLinearVelocity": @copydoc GetLinearVelocity
    <li> "GetAngularVelocity": @copydoc GetAngularVelocity
    <li> "HasAuthority": @copydoc HasAuthority
    <li> "ShapeAABB": @copydoc ShapeAABB
    </ul>

    <b>Reacts on the following actions:</b>
    <ul>
    <li>None.
    </ul>
    </td>
    </tr>

    Does not emit any actions.

    <b>Depends on the component @ref EC_Placeable "Placeable", and optionally on @ref EC_Mesh "Mesh" 
    and @ref EC_Terrain "Terrain" to copy the collision shape from them</b>.
    </table> */
class PHYSICS_MODULE_API EC_RigidBody : public IComponent, public btMotionState
{
    Q_OBJECT
    COMPONENT_NAME("EC_RigidBody", 23)
    Q_ENUMS(ShapeType)

    friend class Physics::PhysicsWorld;

public:
    /// Do not directly allocate new components using operator new, but use the factory-based SceneAPI::CreateComponent functions instead.
    explicit EC_RigidBody(Scene* scene);
    virtual ~EC_RigidBody();

    enum ShapeType
    {
        Shape_Box = 0, ///< Box
        Shape_Sphere, ///< Sphere
        Shape_Cylinder, ///< Cylinder
        Shape_Capsule, ///< Capsule
        Shape_TriMesh, ///< Triangle mesh
        Shape_HeightField, ///< Heightfield
        Shape_ConvexHull ///< Convex hull
    };

    /// Mass of the body. Set to 0 to have a static (immovable) object
    Q_PROPERTY(float mass READ getmass WRITE setmass);
    DEFINE_QPROPERTY_ATTRIBUTE(float, mass);

    /// Shape type, see ShapeType.
    Q_PROPERTY(int shapeType READ getshapeType WRITE setshapeType)
    DEFINE_QPROPERTY_ATTRIBUTE(int, shapeType);

    /// Size (scaling) of the shape.
    /** Sphere only uses x-axis, and capsule uses only x & z axes. Shape is further scaled by Placeable scale.*/
    Q_PROPERTY(float3 size READ getsize WRITE setsize)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, size);

    /// Collision mesh asset reference, only effective if shapeType is Shape_TriMesh.
    Q_PROPERTY(AssetReference collisionMeshRef READ getcollisionMeshRef WRITE setcollisionMeshRef);
    DEFINE_QPROPERTY_ATTRIBUTE(AssetReference, collisionMeshRef);

    /// Friction coefficient between 0.0 - 1.0.
    Q_PROPERTY(float friction READ getfriction WRITE setfriction);
    DEFINE_QPROPERTY_ATTRIBUTE(float, friction);

    /// Rolling friction coefficient between 0.0 - 1.0.
    Q_PROPERTY(float rollingFriction READ getrollingFriction WRITE setrollingFriction);
    DEFINE_QPROPERTY_ATTRIBUTE(float, rollingFriction);

    /// Restitution coefficient between 0.0 - 1.0.
    Q_PROPERTY(float restitution READ getrestitution WRITE setrestitution);
    DEFINE_QPROPERTY_ATTRIBUTE(float, restitution);

    /// Linear damping coefficient of the object (makes it lose velocity even when no force acts on it).
    Q_PROPERTY(float linearDamping READ getlinearDamping WRITE setlinearDamping);
    DEFINE_QPROPERTY_ATTRIBUTE(float, linearDamping);

    /// Angular damping coefficient of the object (makes it lose angular velocity even when no force acts on it)
    Q_PROPERTY(float angularDamping READ getangularDamping WRITE setangularDamping);
    DEFINE_QPROPERTY_ATTRIBUTE(float, angularDamping);

    /// Linear factor. Specifies the axes on which forces can act on the object, making it move.
    Q_PROPERTY(float3 linearFactor READ getlinearFactor WRITE setlinearFactor)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, linearFactor);

    /// Angular factor. Defines in which dimensions the object can rotate
    Q_PROPERTY(float3 angularFactor READ getangularFactor WRITE setangularFactor)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, angularFactor);

    /// Kinematic flag. If true, forces don't affect the object, but it may push other objects around.
    Q_PROPERTY(bool kinematic READ getkinematic WRITE setkinematic)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, kinematic)

    /// Phantom flag. If true, contact response is disabled, ie. there is no collision interaction between this object and others
    Q_PROPERTY(bool phantom READ getphantom WRITE setphantom)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, phantom)

    /// DrawDebug flag. If true, collision shape will be visualized when physics debug drawing is enabled.
    Q_PROPERTY(bool drawDebug READ getdrawDebug WRITE setdrawDebug)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, drawDebug)

    /// Linear velocity
    Q_PROPERTY(float3 linearVelocity READ getlinearVelocity WRITE setlinearVelocity)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, linearVelocity)

    /// Specifies the axes on which torques can act on the object, making it rotate.
    /** Set to 0,0,0 to make for example an avatar capsule that does not tip over by itself. */
    Q_PROPERTY(float3 angularVelocity READ getangularVelocity WRITE setangularVelocity)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, angularVelocity)

    /// The collision layer bitmask of this rigidbody. Several bits can be set. 0 is default (all bits set)
    Q_PROPERTY(int collisionLayer READ getcollisionLayer WRITE setcollisionLayer)
    DEFINE_QPROPERTY_ATTRIBUTE(int, collisionLayer)

    /// Tells with which collision layers this rigidbody collides with (a bitmask). 0 is default (all bits set)
    Q_PROPERTY(int collisionMask READ getcollisionMask WRITE setcollisionMask)
    DEFINE_QPROPERTY_ATTRIBUTE(int, collisionMask)

    /// btMotionState override. Called when Bullet wants us to tell the body's initial transform
    virtual void getWorldTransform(btTransform &worldTrans) const;

    /// btMotionState override. Called when Bullet wants to tell us the body's current transform
    virtual void setWorldTransform(const btTransform &worldTrans);

    void SetClientExtrapolating(bool isClientExtrapolating);

    btRigidBody* GetRigidBody() const { return body_; }

    /// Constructs axis-aligned bounding box from bullet collision shape
    /** @param outMin The minimum corner of the box
        @param outMax The maximum corner of the box */
    void GetAabbox(float3 &outAabbMin, float3 &outAabbMax);

signals:
    /// A physics collision has happened between this rigid body and another entity
    /** If there are several contact points, the signal will be sent multiple times for each contact.
        @param otherEntity The second entity
        @param position World position of collision
        @param normal World normal of collision
        @param distance Contact distance
        @param impulse Impulse applied to the objects to separate them
        @param newCollision True if same collision did not happen on the previous frame.
        If collision has multiple contact points, newCollision can only be true for the first of them. */
    void PhysicsCollision(Entity* otherEntity, const float3& position, const float3& normal, float distance, float impulse, bool newCollision);
<commit_msg>Define rollingFriction last in EC_RigidBody header, so that attribute indices stay unchanged.<commit_after>// For conditions of distribution and use, see copyright notice in LICENSE

#pragma once

#include "CoreDefines.h"
#include "IComponent.h"
#include "AssetReference.h"
#include "AssetFwd.h"
#include "Geometry/AABB.h"
#include "PhysicsModuleApi.h"
#include "PhysicsModuleFwd.h"

#include <LinearMath/btMotionState.h>

class EC_Placeable;
class EC_Terrain;

/// Physics rigid body entity-component
/** <table class="header">
    <tr>
    <td>
    <h2>RigidBody</h2>
    Physics rigid body entity-component

    Registered by Physics::PhysicsModule.

    <b>Attributes</b>:
    <ul>
    <li>float: mass
    <div>@copydoc mass</div>
    <li>enum: shapeType
    <div>@copydoc shapeType</div>
    <li>float3: size
    <div>@copydoc size</div>
    <li>AssetReference: collisionMeshRef
    <div>@copydoc @copydoc collisionMeshRef</div>
    <li>float: friction
    <div>@copydoc friction</div>
    <li>float: restitution
    <div>@copydoc restitution</div>
    <li>float: linearDamping
    <div>@copydoc linearDamping</div>
    <li>float: angularDamping
    <div>@copydoc angularDamping</div>
    <li>float3: linearFactor
    <div>@copydoc linearFactor</div>
    <li>float3: angularFactor
    <div>@copydoc angularFactor</div>
    <li>bool: kinematic
    <div>@copydoc kinematic</div>
    <li>bool: phantom
    <div>@copydoc phantom</div>
    <li>bool: drawDebug
    <div>@copydoc drawDebug</div>
    <li>float3: linearVelocity
    <div>@copydoc linearVelocity</div>
    <li>float3: angularVelocity
    <div>@copydoc angularVelocity</div>
    <li>int: collisionLayer
    <div>@copydoc collisionLayer</div>
    <li>int: collisionMask
    <div>@copydoc collisionMask</div>
    <li>float: rollingFriction
    <div>@copydoc rollingFriction</div>
    </ul>

    <b>Exposes the following scriptable functions:</b>
    <ul>
    <li> "SetShapeFromVisibleMesh": @copydoc SetShapeFromVisibleMesh
    <li> "SetLinearVelocity": @copydoc SetLinearVelocity
    <li> "SetAngularVelocity": @copydoc SetAngularVelocity
    <li> "ApplyForce": @copydoc ApplyForce
    <li> "ApplyTorque": @copydoc ApplyTorque
    <li> "ApplyImpulse": @copydoc ApplyImpulse
    <li> "ApplyTorqueImpulse": @copydoc ApplyTorqueImpulse
    <li> "Activate": @copydoc Activate
    <li> "IsActive": @copydoc IsActive
    <li> "KeepActive": @copydoc KeepActive
    <li> "ResetForces": @copydoc ResetForces
    <li> "SetRotation": @copydoc SetRotation
    <li> "Rotate": @copydoc Rotate
    <li> "GetLinearVelocity": @copydoc GetLinearVelocity
    <li> "GetAngularVelocity": @copydoc GetAngularVelocity
    <li> "HasAuthority": @copydoc HasAuthority
    <li> "ShapeAABB": @copydoc ShapeAABB
    </ul>

    <b>Reacts on the following actions:</b>
    <ul>
    <li>None.
    </ul>
    </td>
    </tr>

    Does not emit any actions.

    <b>Depends on the component @ref EC_Placeable "Placeable", and optionally on @ref EC_Mesh "Mesh" 
    and @ref EC_Terrain "Terrain" to copy the collision shape from them</b>.
    </table> */
class PHYSICS_MODULE_API EC_RigidBody : public IComponent, public btMotionState
{
    Q_OBJECT
    COMPONENT_NAME("EC_RigidBody", 23)
    Q_ENUMS(ShapeType)

    friend class Physics::PhysicsWorld;

public:
    /// Do not directly allocate new components using operator new, but use the factory-based SceneAPI::CreateComponent functions instead.
    explicit EC_RigidBody(Scene* scene);
    virtual ~EC_RigidBody();

    enum ShapeType
    {
        Shape_Box = 0, ///< Box
        Shape_Sphere, ///< Sphere
        Shape_Cylinder, ///< Cylinder
        Shape_Capsule, ///< Capsule
        Shape_TriMesh, ///< Triangle mesh
        Shape_HeightField, ///< Heightfield
        Shape_ConvexHull ///< Convex hull
    };

    /// Mass of the body. Set to 0 to have a static (immovable) object
    Q_PROPERTY(float mass READ getmass WRITE setmass);
    DEFINE_QPROPERTY_ATTRIBUTE(float, mass);

    /// Shape type, see ShapeType.
    Q_PROPERTY(int shapeType READ getshapeType WRITE setshapeType)
    DEFINE_QPROPERTY_ATTRIBUTE(int, shapeType);

    /// Size (scaling) of the shape.
    /** Sphere only uses x-axis, and capsule uses only x & z axes. Shape is further scaled by Placeable scale.*/
    Q_PROPERTY(float3 size READ getsize WRITE setsize)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, size);

    /// Collision mesh asset reference, only effective if shapeType is Shape_TriMesh.
    Q_PROPERTY(AssetReference collisionMeshRef READ getcollisionMeshRef WRITE setcollisionMeshRef);
    DEFINE_QPROPERTY_ATTRIBUTE(AssetReference, collisionMeshRef);

    /// Friction coefficient between 0.0 - 1.0.
    Q_PROPERTY(float friction READ getfriction WRITE setfriction);
    DEFINE_QPROPERTY_ATTRIBUTE(float, friction);

    /// Restitution coefficient between 0.0 - 1.0.
    Q_PROPERTY(float restitution READ getrestitution WRITE setrestitution);
    DEFINE_QPROPERTY_ATTRIBUTE(float, restitution);

    /// Linear damping coefficient of the object (makes it lose velocity even when no force acts on it).
    Q_PROPERTY(float linearDamping READ getlinearDamping WRITE setlinearDamping);
    DEFINE_QPROPERTY_ATTRIBUTE(float, linearDamping);

    /// Angular damping coefficient of the object (makes it lose angular velocity even when no force acts on it)
    Q_PROPERTY(float angularDamping READ getangularDamping WRITE setangularDamping);
    DEFINE_QPROPERTY_ATTRIBUTE(float, angularDamping);

    /// Linear factor. Specifies the axes on which forces can act on the object, making it move.
    Q_PROPERTY(float3 linearFactor READ getlinearFactor WRITE setlinearFactor)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, linearFactor);

    /// Angular factor. Defines in which dimensions the object can rotate
    Q_PROPERTY(float3 angularFactor READ getangularFactor WRITE setangularFactor)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, angularFactor);

    /// Kinematic flag. If true, forces don't affect the object, but it may push other objects around.
    Q_PROPERTY(bool kinematic READ getkinematic WRITE setkinematic)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, kinematic)

    /// Phantom flag. If true, contact response is disabled, ie. there is no collision interaction between this object and others
    Q_PROPERTY(bool phantom READ getphantom WRITE setphantom)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, phantom)

    /// DrawDebug flag. If true, collision shape will be visualized when physics debug drawing is enabled.
    Q_PROPERTY(bool drawDebug READ getdrawDebug WRITE setdrawDebug)
    DEFINE_QPROPERTY_ATTRIBUTE(bool, drawDebug)

    /// Linear velocity
    Q_PROPERTY(float3 linearVelocity READ getlinearVelocity WRITE setlinearVelocity)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, linearVelocity)

    /// Specifies the axes on which torques can act on the object, making it rotate.
    /** Set to 0,0,0 to make for example an avatar capsule that does not tip over by itself. */
    Q_PROPERTY(float3 angularVelocity READ getangularVelocity WRITE setangularVelocity)
    DEFINE_QPROPERTY_ATTRIBUTE(float3, angularVelocity)

    /// The collision layer bitmask of this rigidbody. Several bits can be set. 0 is default (all bits set)
    Q_PROPERTY(int collisionLayer READ getcollisionLayer WRITE setcollisionLayer)
    DEFINE_QPROPERTY_ATTRIBUTE(int, collisionLayer)

    /// Tells with which collision layers this rigidbody collides with (a bitmask). 0 is default (all bits set)
    Q_PROPERTY(int collisionMask READ getcollisionMask WRITE setcollisionMask)
    DEFINE_QPROPERTY_ATTRIBUTE(int, collisionMask)

    /// Rolling friction coefficient between 0.0 - 1.0.
    Q_PROPERTY(float rollingFriction READ getrollingFriction WRITE setrollingFriction);
    DEFINE_QPROPERTY_ATTRIBUTE(float, rollingFriction);

    /// btMotionState override. Called when Bullet wants us to tell the body's initial transform
    virtual void getWorldTransform(btTransform &worldTrans) const;

    /// btMotionState override. Called when Bullet wants to tell us the body's current transform
    virtual void setWorldTransform(const btTransform &worldTrans);

    void SetClientExtrapolating(bool isClientExtrapolating);

    btRigidBody* GetRigidBody() const { return body_; }

    /// Constructs axis-aligned bounding box from bullet collision shape
    /** @param outMin The minimum corner of the box
        @param outMax The maximum corner of the box */
    void GetAabbox(float3 &outAabbMin, float3 &outAabbMax);

signals:
    /// A physics collision has happened between this rigid body and another entity
    /** If there are several contact points, the signal will be sent multiple times for each contact.
        @param otherEntity The second entity
        @param position World position of collision
        @param normal World normal of collision
        @param distance Contact distance
        @param impulse Impulse applied to the objects to separate them
        @param newCollision True if same collision did not happen on the previous frame.
        If collision has multiple contact points, newCollision can only be true for the first of them. */
    void PhysicsCollision(Entity* otherEntity, const float3& position, const float3& normal, float distance, float impulse, bool newCollision);

{'fraction_non_alphanumeric': 0.0839736254155087, 'fraction_numerical': 0.0045774072257642635, 'mean_word_length': 3.9017094017094016, 'pattern_counts': {'":': 34, '<': 204, '<?xml version=': 0, '>': 190, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3503421', 'n_tokens_mistral': 5553, 'n_tokens_neox': 5341, 'n_words': 1731}