Debian Codegraph @

doc/classes/RigidBody2D.xml

raw symbols SCIP
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <class name="RigidBody2D" inherits="PhysicsBody2D" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../class.xsd">
3 <brief_description>
4 A 2D physics body that is moved by a physics simulation.
5 </brief_description>
6 <description>
7 [RigidBody2D] implements full 2D physics. It cannot be controlled directly, instead, you must apply forces to it (gravity, impulses, etc.), and the physics simulation will calculate the resulting movement, rotation, react to collisions, and affect other physics bodies in its path.
8 The body's behavior can be adjusted via [member lock_rotation], [member freeze], and [member freeze_mode]. By changing various properties of the object, such as [member mass], you can control how the physics simulation acts on it.
9 A rigid body will always maintain its shape and size, even when forces are applied to it. It is useful for objects that can be interacted with in an environment, such as a tree that can be knocked over or a stack of crates that can be pushed around.
10 If you need to directly affect the body, prefer [method _integrate_forces] as it allows you to directly access the physics state.
11 If you need to override the default physics behavior, you can write a custom force integration function. See [member custom_integrator].
12 [b]Note:[/b] Changing the 2D transform or [member linear_velocity] of a [RigidBody2D] very often may lead to some unpredictable behaviors. This also happens when a [RigidBody2D] is the descendant of a constantly moving node, like another [RigidBody2D], as that will cause its global transform to be set whenever its ancestor moves.
13 </description>
14 <tutorials>
15 <link title="Physics introduction">$DOCS_URL/tutorials/physics/physics_introduction.html</link>
16 <link title="Troubleshooting physics issues">$DOCS_URL/tutorials/physics/troubleshooting_physics_issues.html</link>
17 <link title="2D Physics Platformer Demo">https://godotengine.org/asset-library/asset/2725</link>
18 <link title="Instancing Demo">https://godotengine.org/asset-library/asset/2716</link>
19 </tutorials>
20 <methods>
21 <method name="_integrate_forces" qualifiers="virtual">
22 <return type="void" />
23 <param index="0" name="state" type="PhysicsDirectBodyState2D" />
24 <description>
25 Called during physics processing, allowing you to read and safely modify the simulation state for the object. By default, it is called before the standard force integration, but the [member custom_integrator] property allows you to disable the standard force integration and do fully custom force integration for a body.
26 </description>
27 </method>
28 <method name="add_constant_central_force">
29 <return type="void" />
30 <param index="0" name="force" type="Vector2" />
31 <description>
32 Adds a constant directional force without affecting rotation that keeps being applied over time until cleared with [code]constant_force = Vector2(0, 0)[/code].
33 This is equivalent to using [method add_constant_force] at the body's center of mass.
34 </description>
35 </method>
36 <method name="add_constant_force">
37 <return type="void" />
38 <param index="0" name="force" type="Vector2" />
39 <param index="1" name="position" type="Vector2" default="Vector2(0, 0)" />
40 <description>
41 Adds a constant positioned force to the body that keeps being applied over time until cleared with [code]constant_force = Vector2(0, 0)[/code].
42 [param position] is the offset from the body origin in global coordinates.
43 </description>
44 </method>
45 <method name="add_constant_torque">
46 <return type="void" />
47 <param index="0" name="torque" type="float" />
48 <description>
49 Adds a constant rotational force without affecting position that keeps being applied over time until cleared with [code]constant_torque = 0[/code].
50 </description>
51 </method>
52 <method name="apply_central_force">
53 <return type="void" />
54 <param index="0" name="force" type="Vector2" />
55 <description>
56 Applies a directional force without affecting rotation. A force is time dependent and meant to be applied every physics update.
57 This is equivalent to using [method apply_force] at the body's center of mass.
58 </description>
59 </method>
60 <method name="apply_central_impulse">
61 <return type="void" />
62 <param index="0" name="impulse" type="Vector2" default="Vector2(0, 0)" />
63 <description>
64 Applies a directional impulse without affecting rotation.
65 An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).
66 This is equivalent to using [method apply_impulse] at the body's center of mass.
67 </description>
68 </method>
69 <method name="apply_force">
70 <return type="void" />
71 <param index="0" name="force" type="Vector2" />
72 <param index="1" name="position" type="Vector2" default="Vector2(0, 0)" />
73 <description>
74 Applies a positioned force to the body. A force is time dependent and meant to be applied every physics update.
75 [param position] is the offset from the body origin in global coordinates.
76 </description>
77 </method>
78 <method name="apply_impulse">
79 <return type="void" />
80 <param index="0" name="impulse" type="Vector2" />
81 <param index="1" name="position" type="Vector2" default="Vector2(0, 0)" />
82 <description>
83 Applies a positioned impulse to the body.
84 An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).
85 [param position] is the offset from the body origin in global coordinates.
86 </description>
87 </method>
88 <method name="apply_torque">
89 <return type="void" />
90 <param index="0" name="torque" type="float" />
91 <description>
92 Applies a rotational force without affecting position. A force is time dependent and meant to be applied every physics update.
93 [b]Note:[/b] [member inertia] is required for this to work. To have [member inertia], an active [CollisionShape2D] must be a child of the node, or you can manually set [member inertia].
94 </description>
95 </method>
96 <method name="apply_torque_impulse">
97 <return type="void" />
98 <param index="0" name="torque" type="float" />
99 <description>
100 Applies a rotational impulse to the body without affecting the position.
101 An impulse is time-independent! Applying an impulse every frame would result in a framerate-dependent force. For this reason, it should only be used when simulating one-time impacts (use the "_force" functions otherwise).
102 [b]Note:[/b] [member inertia] is required for this to work. To have [member inertia], an active [CollisionShape2D] must be a child of the node, or you can manually set [member inertia].
103 </description>
104 </method>
105 <method name="get_colliding_bodies" qualifiers="const">
106 <return type="Node2D[]" />
107 <description>
108 Returns a list of the bodies colliding with this one. Requires [member contact_monitor] to be set to [code]true[/code] and [member max_contacts_reported] to be set high enough to detect all the collisions.
109 [b]Note:[/b] The result of this test is not immediate after moving objects. For performance, list of collisions is updated once per frame and before the physics step. Consider using signals instead.
110 </description>
111 </method>
112 <method name="get_contact_count" qualifiers="const">
113 <return type="int" />
114 <description>
115 Returns the number of contacts this body has with other bodies. By default, this returns 0 unless bodies are configured to monitor contacts (see [member contact_monitor]).
116 [b]Note:[/b] To retrieve the colliding bodies, use [method get_colliding_bodies].
117 </description>
118 </method>
119 <method name="set_axis_velocity">
120 <return type="void" />
121 <param index="0" name="axis_velocity" type="Vector2" />
122 <description>
123 Sets the body's velocity on the given axis. The velocity in the given vector axis will be set as the given vector length. This is useful for jumping behavior.
124 </description>
125 </method>
126 </methods>
127 <members>
128 <member name="angular_damp" type="float" setter="set_angular_damp" getter="get_angular_damp" default="0.0">
129 Damps the body's rotation. By default, the body will use the [member ProjectSettings.physics/2d/default_angular_damp] setting or any value override set by an [Area2D] the body is in. Depending on [member angular_damp_mode], you can set [member angular_damp] to be added to or to replace the body's damping value.
130 See [member ProjectSettings.physics/2d/default_angular_damp] for more details about damping.
131 </member>
132 <member name="angular_damp_mode" type="int" setter="set_angular_damp_mode" getter="get_angular_damp_mode" enum="RigidBody2D.DampMode" default="0">
133 Defines how [member angular_damp] is applied.
134 </member>
135 <member name="angular_velocity" type="float" setter="set_angular_velocity" getter="get_angular_velocity" default="0.0">
136 The body's rotational velocity in [i]radians[/i] per second.
137 </member>
138 <member name="can_sleep" type="bool" setter="set_can_sleep" getter="is_able_to_sleep" default="true">
139 If [code]true[/code], the body can enter sleep mode when there is no movement. See [member sleeping].
140 </member>
141 <member name="center_of_mass" type="Vector2" setter="set_center_of_mass" getter="get_center_of_mass" default="Vector2(0, 0)">
142 The body's custom center of mass, relative to the body's origin position, when [member center_of_mass_mode] is set to [constant CENTER_OF_MASS_MODE_CUSTOM]. This is the balanced point of the body, where applied forces only cause linear acceleration. Applying forces outside of the center of mass causes angular acceleration.
143 When [member center_of_mass_mode] is set to [constant CENTER_OF_MASS_MODE_AUTO] (default value), the center of mass is automatically determined, but this does not update the value of [member center_of_mass].
144 </member>
145 <member name="center_of_mass_mode" type="int" setter="set_center_of_mass_mode" getter="get_center_of_mass_mode" enum="RigidBody2D.CenterOfMassMode" default="0">
146 Defines the way the body's center of mass is set.
147 </member>
148 <member name="constant_force" type="Vector2" setter="set_constant_force" getter="get_constant_force" default="Vector2(0, 0)">
149 The body's total constant positional forces applied during each physics update.
150 See [method add_constant_force] and [method add_constant_central_force].
151 </member>
152 <member name="constant_torque" type="float" setter="set_constant_torque" getter="get_constant_torque" default="0.0">
153 The body's total constant rotational forces applied during each physics update.
154 See [method add_constant_torque].
155 </member>
156 <member name="contact_monitor" type="bool" setter="set_contact_monitor" getter="is_contact_monitor_enabled" default="false">
157 If [code]true[/code], the RigidBody2D will emit signals when it collides with another body.
158 [b]Note:[/b] By default the maximum contacts reported is set to 0, meaning nothing will be recorded, see [member max_contacts_reported].
159 </member>
160 <member name="continuous_cd" type="int" setter="set_continuous_collision_detection_mode" getter="get_continuous_collision_detection_mode" enum="RigidBody2D.CCDMode" default="0">
161 Continuous collision detection mode.
162 Continuous collision detection tries to predict where a moving body will collide instead of moving it and correcting its movement after collision. Continuous collision detection is slower, but more precise and misses fewer collisions with small, fast-moving objects. Raycasting and shapecasting methods are available.
163 </member>
164 <member name="custom_integrator" type="bool" setter="set_use_custom_integrator" getter="is_using_custom_integrator" default="false">
165 If [code]true[/code], the standard force integration (like gravity or damping) will be disabled for this body. Other than collision response, the body will only move as determined by the [method _integrate_forces] method, if that virtual method is overridden.
166 Setting this property will call the method [method PhysicsServer2D.body_set_omit_force_integration] internally.
167 </member>
168 <member name="freeze" type="bool" setter="set_freeze_enabled" getter="is_freeze_enabled" default="false">
169 If [code]true[/code], the body is frozen. Gravity and forces are not applied anymore.
170 See [member freeze_mode] to set the body's behavior when frozen.
171 [b]Note:[/b] For a body that is always frozen, use [StaticBody2D] or [AnimatableBody2D] instead.
172 </member>
173 <member name="freeze_mode" type="int" setter="set_freeze_mode" getter="get_freeze_mode" enum="RigidBody2D.FreezeMode" default="0">
174 The body's freeze mode. Determines the body's behavior when [member freeze] is [code]true[/code].
175 [b]Note:[/b] For a body that is always frozen, use [StaticBody2D] or [AnimatableBody2D] instead.
176 </member>
177 <member name="gravity_scale" type="float" setter="set_gravity_scale" getter="get_gravity_scale" default="1.0">
178 Multiplies the gravity applied to the body. The body's gravity is calculated from the [member ProjectSettings.physics/2d/default_gravity] project setting and/or any additional gravity vector applied by [Area2D]s.
179 </member>
180 <member name="inertia" type="float" setter="set_inertia" getter="get_inertia" default="0.0">
181 The body's moment of inertia. This is like mass, but for rotation: it determines how much torque it takes to rotate the body. The moment of inertia is usually computed automatically from the mass and the shapes, but this property allows you to set a custom value.
182 If set to [code]0[/code], inertia is automatically computed (default value).
183 [b]Note:[/b] This value does not change when inertia is automatically computed. Use [PhysicsServer2D] to get the computed inertia.
184 [codeblocks]
185 [gdscript]
186 @onready var ball = $Ball
187
188 func get_ball_inertia():
189 return 1.0 / PhysicsServer2D.body_get_direct_state(ball.get_rid()).inverse_inertia
190 [/gdscript]
191 [csharp]
192 private RigidBody2D _ball;
193
194 public override void _Ready()
195 {
196 _ball = GetNode&lt;RigidBody2D&gt;("Ball");
197 }
198
199 private float GetBallInertia()
200 {
201 return 1.0f / PhysicsServer2D.BodyGetDirectState(_ball.GetRid()).InverseInertia;
202 }
203 [/csharp]
204 [/codeblocks]
205 </member>
206 <member name="linear_damp" type="float" setter="set_linear_damp" getter="get_linear_damp" default="0.0">
207 Damps the body's movement. By default, the body will use the [member ProjectSettings.physics/2d/default_linear_damp] setting or any value override set by an [Area2D] the body is in. Depending on [member linear_damp_mode], you can set [member linear_damp] to be added to or to replace the body's damping value.
208 See [member ProjectSettings.physics/2d/default_linear_damp] for more details about damping.
209 </member>
210 <member name="linear_damp_mode" type="int" setter="set_linear_damp_mode" getter="get_linear_damp_mode" enum="RigidBody2D.DampMode" default="0">
211 Defines how [member linear_damp] is applied.
212 </member>
213 <member name="linear_velocity" type="Vector2" setter="set_linear_velocity" getter="get_linear_velocity" default="Vector2(0, 0)">
214 The body's linear velocity in pixels per second. Can be used sporadically, but [b]don't set this every frame[/b], because physics may run in another thread and runs at a different granularity. Use [method _integrate_forces] as your process loop for precise control of the body state.
215 </member>
216 <member name="lock_rotation" type="bool" setter="set_lock_rotation_enabled" getter="is_lock_rotation_enabled" default="false">
217 If [code]true[/code], the body cannot rotate. Gravity and forces only apply linear movement.
218 </member>
219 <member name="mass" type="float" setter="set_mass" getter="get_mass" default="1.0">
220 The body's mass.
221 </member>
222 <member name="max_contacts_reported" type="int" setter="set_max_contacts_reported" getter="get_max_contacts_reported" default="0">
223 The maximum number of contacts that will be recorded. Requires a value greater than 0 and [member contact_monitor] to be set to [code]true[/code] to start to register contacts. Use [method get_contact_count] to retrieve the count or [method get_colliding_bodies] to retrieve bodies that have been collided with.
224 [b]Note:[/b] The number of contacts is different from the number of collisions. Collisions between parallel edges will result in two contacts (one at each end), and collisions between parallel faces will result in four contacts (one at each corner).
225 </member>
226 <member name="physics_material_override" type="PhysicsMaterial" setter="set_physics_material_override" getter="get_physics_material_override">
227 The physics material override for the body.
228 If a material is assigned to this property, it will be used instead of any other physics material, such as an inherited one.
229 </member>
230 <member name="sleeping" type="bool" setter="set_sleeping" getter="is_sleeping" default="false">
231 If [code]true[/code], the body will not move and will not calculate forces until woken up by another body through, for example, a collision, or by using the [method apply_impulse] or [method apply_force] methods.
232 </member>
233 </members>
234 <signals>
235 <signal name="body_entered">
236 <param index="0" name="body" type="Node" />
237 <description>
238 Emitted when a collision with another [PhysicsBody2D] or [TileMap] occurs. Requires [member contact_monitor] to be set to [code]true[/code] and [member max_contacts_reported] to be set high enough to detect all the collisions. [TileMap]s are detected if the [TileSet] has Collision [Shape2D]s.
239 [param body] the [Node], if it exists in the tree, of the other [PhysicsBody2D] or [TileMap].
240 </description>
241 </signal>
242 <signal name="body_exited">
243 <param index="0" name="body" type="Node" />
244 <description>
245 Emitted when the collision with another [PhysicsBody2D] or [TileMap] ends. Requires [member contact_monitor] to be set to [code]true[/code] and [member max_contacts_reported] to be set high enough to detect all the collisions. [TileMap]s are detected if the [TileSet] has Collision [Shape2D]s.
246 [param body] the [Node], if it exists in the tree, of the other [PhysicsBody2D] or [TileMap].
247 </description>
248 </signal>
249 <signal name="body_shape_entered">
250 <param index="0" name="body_rid" type="RID" />
251 <param index="1" name="body" type="Node" />
252 <param index="2" name="body_shape_index" type="int" />
253 <param index="3" name="local_shape_index" type="int" />
254 <description>
255 Emitted when one of this RigidBody2D's [Shape2D]s collides with another [PhysicsBody2D] or [TileMap]'s [Shape2D]s. Requires [member contact_monitor] to be set to [code]true[/code] and [member max_contacts_reported] to be set high enough to detect all the collisions. [TileMap]s are detected if the [TileSet] has Collision [Shape2D]s.
256 [param body_rid] the [RID] of the other [PhysicsBody2D] or [TileSet]'s [CollisionObject2D] used by the [PhysicsServer2D].
257 [param body] the [Node], if it exists in the tree, of the other [PhysicsBody2D] or [TileMap].
258 [param body_shape_index] the index of the [Shape2D] of the other [PhysicsBody2D] or [TileMap] used by the [PhysicsServer2D]. Get the [CollisionShape2D] node with [code]body.shape_owner_get_owner(body.shape_find_owner(body_shape_index))[/code].
259 [param local_shape_index] the index of the [Shape2D] of this RigidBody2D used by the [PhysicsServer2D]. Get the [CollisionShape2D] node with [code]self.shape_owner_get_owner(self.shape_find_owner(local_shape_index))[/code].
260 </description>
261 </signal>
262 <signal name="body_shape_exited">
263 <param index="0" name="body_rid" type="RID" />
264 <param index="1" name="body" type="Node" />
265 <param index="2" name="body_shape_index" type="int" />
266 <param index="3" name="local_shape_index" type="int" />
267 <description>
268 Emitted when the collision between one of this RigidBody2D's [Shape2D]s and another [PhysicsBody2D] or [TileMap]'s [Shape2D]s ends. Requires [member contact_monitor] to be set to [code]true[/code] and [member max_contacts_reported] to be set high enough to detect all the collisions. [TileMap]s are detected if the [TileSet] has Collision [Shape2D]s.
269 [param body_rid] the [RID] of the other [PhysicsBody2D] or [TileSet]'s [CollisionObject2D] used by the [PhysicsServer2D].
270 [param body] the [Node], if it exists in the tree, of the other [PhysicsBody2D] or [TileMap].
271 [param body_shape_index] the index of the [Shape2D] of the other [PhysicsBody2D] or [TileMap] used by the [PhysicsServer2D]. Get the [CollisionShape2D] node with [code]body.shape_owner_get_owner(body.shape_find_owner(body_shape_index))[/code].
272 [param local_shape_index] the index of the [Shape2D] of this RigidBody2D used by the [PhysicsServer2D]. Get the [CollisionShape2D] node with [code]self.shape_owner_get_owner(self.shape_find_owner(local_shape_index))[/code].
273 </description>
274 </signal>
275 <signal name="sleeping_state_changed">
276 <description>
277 Emitted when the physics engine changes the body's sleeping state.
278 [b]Note:[/b] Changing the value [member sleeping] will not trigger this signal. It is only emitted if the sleeping state is changed by the physics engine or [code]emit_signal("sleeping_state_changed")[/code] is used.
279 </description>
280 </signal>
281 </signals>
282 <constants>
283 <constant name="FREEZE_MODE_STATIC" value="0" enum="FreezeMode">
284 Static body freeze mode (default). The body is not affected by gravity and forces. It can be only moved by user code and doesn't collide with other bodies along its path.
285 </constant>
286 <constant name="FREEZE_MODE_KINEMATIC" value="1" enum="FreezeMode">
287 Kinematic body freeze mode. Similar to [constant FREEZE_MODE_STATIC], but collides with other bodies along its path when moved. Useful for a frozen body that needs to be animated.
288 </constant>
289 <constant name="CENTER_OF_MASS_MODE_AUTO" value="0" enum="CenterOfMassMode">
290 In this mode, the body's center of mass is calculated automatically based on its shapes. This assumes that the shapes' origins are also their center of mass.
291 </constant>
292 <constant name="CENTER_OF_MASS_MODE_CUSTOM" value="1" enum="CenterOfMassMode">
293 In this mode, the body's center of mass is set through [member center_of_mass]. Defaults to the body's origin position.
294 </constant>
295 <constant name="DAMP_MODE_COMBINE" value="0" enum="DampMode">
296 In this mode, the body's damping value is added to any value set in areas or the default value.
297 </constant>
298 <constant name="DAMP_MODE_REPLACE" value="1" enum="DampMode">
299 In this mode, the body's damping value replaces any value set in areas or the default value.
300 </constant>
301 <constant name="CCD_MODE_DISABLED" value="0" enum="CCDMode">
302 Continuous collision detection disabled. This is the fastest way to detect body collisions, but can miss small, fast-moving objects.
303 </constant>
304 <constant name="CCD_MODE_CAST_RAY" value="1" enum="CCDMode">
305 Continuous collision detection enabled using raycasting. This is faster than shapecasting but less precise.
306 </constant>
307 <constant name="CCD_MODE_CAST_SHAPE" value="2" enum="CCDMode">
308 Continuous collision detection enabled using shapecasting. This is the slowest CCD method and the most precise.
309 </constant>
310 </constants>
311 </class>