How to use physics engine joints in CocosCreator

How to use physics engine joints in CocosCreator

CocosCreator version 2.4.2

mousejoint mouse joint

Cocos document definition:

The mouse joint is used to make a point on a rigid body track a specified position in the world coordinate system. Mouse joints can be assigned a maximum radius to impose a soft constraint.

The mouse joint will automatically register mouse events using the mouse region node and move the selected body on touch move events.

Note: Generally, mouse joints are only used in testing environments.

illustrate:

Add a mouse joint to any cc.Node, set mouseRegion to Canvas, and set elasticity and damping to 1

Mouse Region: Mouse registration node

Frequency: Elasticity, which indicates the strength of pulling an object. The larger the value, the greater the elasticity. It's like a rubber band tied to a ball. The more you pull the rubber band, the faster the ball moves.

DampingRatio: Damping, which indicates the resistance an object encounters after being pulled. It's like 1 is pulling a ball with a rubber band in the air, and 10 is pulling a ball with a rubber band in water. The greater the damping, the slower the ball moves.

distanceJoint distance joint

Cocos document definition:

Distance joints constrain the two bodies connected by the joint to a fixed length. You can think of it as a massless, solid stick.

illustrate:

The two rigid bodies can only rotate around their respective nodes, and the distance between the two nodes remains unchanged.

ConnectedBody: The rigid body at the other end of the joint.

Anchor: The rigid body anchor point where the joint is located

ConnectedAnchor: The rigid body anchor point at the other end of the joint

CollideConnected: Whether the rigid bodies at both ends of the joint collide

Distance: distance, 200 pixels

Frequency: Elasticity. If it is set to 10, there will be elastic force between the two rigid bodies, just like there is a spring connecting the two objects. If it is set to 0, it means that the distance of 200 pixels will always remain unchanged, just like the roots of a tree.

DampingRatio: Damping

The joint needs to connect two rigid bodies. The two dots and dotted line in the figure represent the anchor points of the rigid bodies and the joint connection lines.

distance is 200, and the two rigid bodies always maintain a distance of 200. And they can only rotate on their respective nodes.

application:

The distance is fixed, the nodes at both ends can rotate, and the joints can be set to be elastic

1. One end is fixed, it can be made into a large pendulum

2. One end is fixed, set up elastic, elastic large pendulum

motorjoint

Cocos document definition:

Motor joints are used to control the relative motion between two rigid bodies. A typical example is to control the motion of a dynamic rigid body relative to the ground.

illustrate:

The motor joint constrains two rigid bodies so that their relative positions and angles remain consistent. When the position and angle of BodyA changes, BodyB will move toward the position and angle of BodyA using the forces of MaxForce and MaxTorque.

Connected Body: The rigid body at the other end of the joint

CollideConnected: Whether the rigid bodies at both ends of the joint collide

LinearOffset: The position offset of the other end rigid body relative to the starting rigid body. That is the target position of BodyB.

AngularOffset: The angular offset of the other rigid body relative to the starting rigid body. This is the target angle of Body B.

MaxForce: The maximum force that can be applied to the rigidbody.

Max Torque: The maximum torque force that can be applied to the rigid body.

CorrectionFactor: Position correction factor, range [0, 1]. The default value is 0.3. Easing factor for BodyB to move towards BodyA. The larger the value, the faster the easing.

The linear offset is 200, which means that the positions of the two rigid bodies are offset by 200. Once they deviate from this limit, the two rigid bodies will move to a position 200 apart with a max force of 300.

angularoffset is 0, and the angle offset is 0. Once it deviates from this limit, the two rigid bodies will use the force of maxtorque 100 to keep the angles of the two rigid bodies offset to 0.

application

Use force and torque to keep the positions and angles of the two rigid bodies consistent

1. One end is fixed, it can be made into a tumbler or a boxing sandbag placed on the ground

prismaticJoint displacement joint

Cocos document definition:

The translation joint specifies that the rigid body can only be moved in one direction. You can enable joint limits to set the range of movement of the rigid body, or enable motors to use joint motors to drive the movement of the rigid body.

illustrate:

It cannot rotate and can only move on restricted coordinate axes.

LocalAxisA: Specifies the direction in which the rigid body can move. Here X=1, which means it can only move left and right on the X axis.

ReferenceAngle: relative angle. The relative angle between the rigid bodies at both ends of the joint.

EnableLimit: Whether to enable the distance limit of the joint. When enabled, the joint rigid body can only move within the restricted range of LowerLimit and UpperLimit. This means that you can only move between 200-400 pixels on the x-axis.

EnableMotor: Whether to enable the motor. When turned on, the rigid body will move toward the specified coordinate axis with the force of maxmotorforce until the speed reaches motorspeed.

You can see that the rigid body can only move on the x-axis, and the movement range is limited to 200-400 pixels. This can be used in some horizontal games, where terrain or mechanisms can only move left, right or in a specified direction.

application

Limit movement in a specified direction, cannot rotate, optional motor, optional limit range

1. Turn on the motor and limit the x-axis to move left and right. Limit the moving range to two end points. It can be made into a sliding rail vehicle that moves back and forth.

2. Drawers

revoluteJoint

Cocos document definition:

A revolute joint constrains two rigid bodies to rotate around a point. You can limit the maximum and minimum angles of rotation by turning on joint limits.

You can apply a torque force by turning on the motor to drive the relative velocity of the two rigid bodies at this point.

illustrate:

Similar to other joints, they limit some angle and force parameters.

The middle one is the rigid body at the other end of the joint, and its type is static. The rigidbody bound to the joint is rotated with a force of 1000 at a distance of (13,202) from the rigidbody at the other end of the joint.

We use revolute joints to make a car. The red color corresponds to the body of the car and the white color corresponds to the wheels.

The red one is the node bound to revoluteJoint. The node is bound to two revoluteJoint joints. The Connected Bodies of the joints are two white box rigid bodies, which are equivalent to wheels.

Each wheel has a force of maxmotortorque 500 and a maximum speed of motorspeed 200.

application

Two rigid bodies rotate around a point, with optional rotation angle and motor

1. One end is fixed, and the other end is turned on by the motor to rotate, electric fan, wheel

2. Turn off the motor and only set the rotation range to form the joints of the human body

3. Rope

Why don't ropes use rope joints? Because rope joints cannot limit the rotation range, position range, or rotation nodes.

ropeJoint

Cocos document definition:

The rope joint only specifies the maximum distance between the two rigidbodies and has no other effects. Note: This may give some unexpected effects if you try to dynamically modify the length of a joint.

illustrate:

maxlength: the maximum length of the rope

There's nothing special about it. They have no spring, no motor. It's just like a rope tying two rigid bodies, limited to the maxlength 200.

application

Limit the maximum length

1. Yo-Yo

2. Dog walking, dog leash

weldJoint welding joint (welding joint, bonding joint)

Cocos document definition:

A weld joint is equivalent to gluing two rigid bodies together. Welding joints may distort something because the constraint solver is only approximating the values.

illustrate:

Like two rigid bodies welded together by a steel bar. Compared with the distance joint, the two rigid bodies of the distance joint can rotate at their respective nodes, while the two rigid bodies of the weld joint cannot rotate at their respective nodes.

If you set the Frequency elastic force, for example, Frequency = 0.5, the joint rigid body can rotate at its own node, but the rigid body at the other end cannot rotate.

application

Two rigid bodies are welded (pasted) together, and elasticity can be set

1. Rigid bodies can be welded together

2. It can also be understood that various rigid bodies are pasted together

3. A game

4. After setting the elasticity, the rigid body where the joint is located can rotate. For example, a soft rectangle can bend.

The ring composed of welded joints can be bent after setting elasticity.

wheeljoint wheel joint

Cocos document definition:

Wheel joints provide two degrees of freedom: rotation and translation along a specified position in a direction. You can use motors to drive the rotation of a rigidbody by turning on joint motors. The wheel joint is specially designed for motor vehicle types.

illustrate:

Wheel joints, composed of Revolute and Prismatic, are used to simulate motor vehicle wheels.

LocalAxis: Specifies the direction in which the rigid body can move

MaxMotorT...: The maximum torque force that can be applied to the rigid body

Motor Speed: Expected motor speed

EnableMotor: Enable the motor

Frequency: elastic coefficient

DampingRatio: Damping

Compared with the RevoluteJoint joint, it also has a motor, but also has increased elasticity and limited movement direction.

Frequency The elastic coefficient is like the shock absorber of a car tire, which allows it to shake when the wheel moves.

Local Axis A limits the direction, which can limit the direction of the shock-absorbing elastic force to only up and down.

In the figure below, the red rigid body is bound to two wheelJoint joints, and the ConnectedBody is set to two circle rigid bodies.

The motor has a torque of 200 and a maximum speed of 1000, which is the driving force and maximum speed of the wheels.

The elastic coefficient is 5, the damping is 0.1, and the limited direction is the Y-axis, which is equivalent to the shock absorption system of this car.

Set the Anchor points to the Connected Body, which is the center point of the wheel (when the wheel joint is selected, there will be a blue mark, this blue is the Anchor, just drag the blue mark to the center of the circular rigid body)

Select two wheels and set the friction coefficient of the circular rigid body to 0.8 and the elastic coefficient to 0.2.

If the friction coefficient is 0, it is equivalent to the wheels constantly slipping on the ground and unable to move.

The above is the details of how to use the physical engine joints in CocosCreator. For more information about CocosCreator physical engine joints, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • Unity3D realizes camera lens movement and limits the angle
  • Detailed explanation of how to use several timers in CocosCreator
  • CocosCreator learning modular script
  • How to use JSZip compression in CocosCreator
  • CocosCreator Getting Started Tutorial: Making Your First Game with TS
  • Interpretation of CocosCreator source code: engine startup and main loop
  • CocosCreator general framework design resource management
  • How to make a List in CocosCreator
  • How to use http and WebSocket in CocosCreator
  • Analysis of CocosCreator's new resource management system
  • How to use cc.follow for camera tracking in CocosCreator

<<:  Tomcat uses Log4j to output catalina.out log

>>:  Brief analysis of MySQL union and union all

Recommend

What is the file mysql-bin.000001 in mysql? Can it be deleted?

After installing MySQL using ports, I found that ...

SMS verification code login function based on antd pro (process analysis)

Table of contents summary Overall process front e...

Advantages of INSERT INTO SET in MySQL

Insert data into mysql database. Previously commo...

How to install openssh from source code in centos 7

Environment: CentOS 7.1.1503 Minimum Installation...

Installation tutorial of the latest stable version of MySQL 5.7.17 under Linux

Install the latest stable version of MySQL on Lin...

MySQL exposes Riddle vulnerability that can cause username and password leakage

The Riddle vulnerability targeting MySQL versions...

Method to detect whether ip and port are connectable

Windows cmd telnet format: telnet ip port case: t...

Nginx cache files and dynamic files automatic balancing configuration script

nginx Nginx (engine x) is a high-performance HTTP...

Detailed tutorial on using the tomcat8-maven-plugin plugin in Maven

I searched a lot of articles online but didn'...

Summary of various methods of MySQL data recovery

Table of contents 1. Introduction 2. Direct recov...

Implementing a table scrolling carousel effect through CSS animation

An application of CSS animation, with the same co...

Several ways to clear arrays in Vue (summary)

Table of contents 1. Introduction 2. Several ways...