"Unity_api Analysis" tenth Chapter Rigidbody class

Source: Internet
Author: User
Tags instance method time interval

The function of the Rigidbody class is to simulate the physical characteristics of the Gameobject object in the real world, including gravity, resistance, mass, speed, and so on. The assignment code for Rigidbody object properties is usually placed in the Onfixedupdate method in the script.

Rigidbody Class Instance Properties

Collisiondetectionmode Properties: Collision Detection Mode

Public Collisiondetectionmode collisiondetectionmode{get; set;}

This property is used to set the collision detection mode of a rigid body, with three types of collision detection modes, that is, 3 values for enumeration type Collisiondetectionmode.

Discrete: Static discrete detection mode, system default settings. In this mode, the collision of two objects in a frame can only be detected if they overlap, which may occur through the phenomenon.

Continuous: Static continuous detection mode, generally used in high-speed moving rigid body target collision body, to prevent the crossing, the detection intensity is higher than the discrete.

Continuousdynamic: The strongest continuous dynamic monitoring mode, generally used in two high-speed moving objects, to prevent cross-crossing, its calculation consumes the most, the general situation is used with caution.

No matter what kind of detection mode, it is possible to pass through, in order to prevent the penetration phenomenon, in addition to set its collision detection mode crooked, but also appropriate to increase the thickness of two object collider, generally not less than 0.1, while reducing the amount of time to reduce the collision of two objects relative speed.

Drag properties: Rigid body resistance

public float drag{get; set;}

This property is used to add a resistance to a rigid body, the larger the drag value, the faster the body slows down, and when drag>0, the rigid body moves at a constant speed after it increases to a certain velocity.

Inertiatensor Properties: Inertia tensor

This property is used to set the inertia tensor of a rigid body, and at the same distance as that of the center of gravity, just realize the inclination to the large side of the tensor value.

Mass properties: Rigid body mass

public float mass{get; set;}

This property is used to set or return the mass of a rigid body, the general value of the best simulation near 0.1, the maximum not more than 10, otherwise prone to simulation instability.

Velocity properties: Rigid body speed

Public Vector3 velocity{get; set;}

This property is used to set or return the speed value of a rigid body.

In the script, whether to give a rigid body a Vector3 type of speed variable V, or to get the speed of the current rigid body v,v direction is relative to the world coordinate system.

Velocity is measured in meters per second. Meters are the default unit of length in unity.

Rigidbody class instance method

Addexplosionforce method to simulate explosive force

public void Addexplosionforce (float explosionforce, Vector3 explosionposition, float explosionraduce);

public void Addexplosionforce (float explosionforce, Vector3 explosionposition, float explosionraduce, float Upwardsmodifier);

public void Addexplosionforce (float explosionforce, Vector3 explosionposition, float explosionraduce, float Upwardsmodifier, Forcemode mode);

The parameter explosionforce is the magnitude of the force increased by the explosive point, the parameter explosionposition is the point coordinate of the explosion (relative to the world coordinate system), the parameter Explosionradius is the effective radius of explosion force, The parameter upwardsmodifier is the offset of the point in the y-axis direction of the explosion force, and the parameter mode is the action pattern of the explosive power, and the default is Forcemode.force.

Addforceatpositon method: Increasing the point force of a rigid body

public void Addforceatpostion (Vector3 force, Vector3 position);

public void Addforceatposition (Vector3 force, Vector3 position, Forcemode mode);

The parameter force is the torque vector, the parameter position is the action point coordinate value, the parameter mode Weili action mode.

This method is used to add force forces to the position point of the parameter, whose reference coordinate system is the world coordinate system, and the mode is mode, and the default value is Forcemode.force.

Addtorque method: Adding torque to a rigid body

public void Addtorque (Vector3 torque);

public void Addtorque (Vector3 torque, forcemode mode);

public void Addtorque (float x, float y, float z);

public void Addtorque (float x, float y, float z, forcemode mode);

The parameter torque is the torque vector, and the parameter mode Weili the action mode.

This method is used to add a torque torque to the rigid body, and the force mode of the torque is determined by mode.

Closestpointonbounds method: The shortest distance from the explosion point to the rigid body

Public Vector3 closestpointonbounds (Vector3 position);

Getpointvelocity method: Rigid point speed

Public Vector3 getpointvelocity (Vector3 worldpoint);

Where the parameter worldpoint is the point coordinate of the world coordinate system.

This method is used to obtain the velocity of the worldpoint point in the local coordinate system of the rigid body in the world coordinate system, and the velocity calculation is affected by the angular velocity of the rigid body.

Getrelativepointvelocity method: Rigid point relative velocity

Public Vector3 getrelativepointvelocity (Vector3 relativepoint);

The parameters are point coordinates in the body's own coordinate system.

This method is used to obtain the speed of a point in the body's own coordinate system, and the velocity is affected by the angular velocity of the rigid body.

Moveposition method: Rigid body position movement

public void Moveposition (Vector3 position);

Where the parameters are the coordinates of the position to which the rigid body component is to be moved.

This method is used to move the position of a rigid body, usually when a rigid body loses its kinetic simulation, that is, when Iskinematic is true.

Similar methods are moverotation.

Sleep method: Rigid body hibernation

This method causes the rigid body to enter hibernation and sleeps at least one frame.

Wakeup method: Waking a rigid body

public void WakeUp ();

This method is used to wake a rigid body from hibernation. To wake a rigid body from hibernation, in addition to calling the wakeup method, there are 4 situations that occur.

Other rigid bodies collide with dormant rigid bodies.

Other rigid bodies that use joint connections move.

The properties of the rigid body have changed.

An external force is applied to the rigid body in hibernation.

Sweeptest method: Probe Collider.

public bool Sweeptest (Vector3 direction, out Raycasthit hitinfo);

public bool Sweeptest (Vector3 direction, out Raycasthit hitinfo, float distance);

The parameter direction is the detection direction, the parameter distance is the effective detection distance, the default is infinity.

This method is used to detect if there is a collider object in the direction direction of the rigid body, and the effective detection distance of the object is not greater than distance.

Sweeptestall Method: Detecting Collider

Public raycasthit[] Sweeptestall (Vector3 dorection);

Public raycasthit[] Sweeptestall (Vector3 dorection, float distance);

This method is used to detect whether a collider is contained within the distance distance of the direction direction of the rigid body and returns the raycasthit of all detected objects.

Annotations:

The Usegravity property is used to determine whether a rigid body accepts induction of gravitational acceleration.

The Iskinematic property is used to determine whether a rigid body accepts dynamic simulations, which include not only gravity sensing, but also physical simulations of mass, velocity, resistance, and so on.

In the Rigidbody class, there is a close relationship between mass, density, and Scale3 APIs.

If the density of a rigid body is not set using the Rigidbody, Setdensity (density:float) method in the script, the mass mass value of the rigid body is the inspector size in the mass panel, At this point the mass is independent of the scale size in transform.

If the density of a rigid body is set using the Rigidbody,setdensity (Density:float) method in the script, the mass of the rigid body is mass = density * scale.x * scale.y * Scale.z, regardless of the size of the mass setting in the Inspector panel.

If both the density density and the quality mass are set in the script, the quality value of the rigid body century depends on the order in which the code executes in the script, which is valid.

When two objects collide, follow the momentum constant force, i.e. M1 * v1 + m2 * v2 = (m1 + m2) * v.

Forcemode is an enumeration type and is used to control how the force functions, with 4 enumeration members.

Forcemode.force: By default, the momentum is calculated using the mass calculation of the rigid body, measured in intervals per frame.

Forcemode.acceleration: In this mode of action, the actual mass of the rigid body is ignored and the default value of 1.0f is used, and the time interval is calculated at the system frame frequency interval.

Forcemode.impulse: This method uses instantaneous force action, that is, the value of T defaults to 1, no longer use the system frame frequency interval.

Forcemode.velocitychange: This mode of action ignores the actual mass of the rigid body, using the default mass of 1.0, but also ignores the actual frame frequency interval of the system, using the default interval of 1.0.

"Unity_api Analysis" tenth Chapter Rigidbody class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.