Rigidbody2d



I have been trying to change the gravity scale of my Rigidbody2d when I click on it. By default it is by 0 and when I click on it, it should change to 1, so that it falls down. I have a square with a boxcollider2d and a Rigidbody2d and a square with just a box collider. The Rigidbody2D class essentially provides the same functionality in 2D that the Rigidbody class provides in 3D. Adding a Rigidbody2D component to a sprite puts it under the control of the physics engine. By itself, this means that the sprite will be affected by gravity and can be. To do so, we need to understand what rigidbodies are. Rigidbodies are components that allow a GameObject to react to real-time physics. This includes reactions to forces and gravity, mass, drag and momentum. You can attach a Rigidbody to your GameObject by simply clicking on Add Component and typing in Rigidbody2D. C# (CSharp) UnityEngine Rigidbody2D.AddRelativeForce - 8 examples found. These are the top rated real world C# (CSharp) examples of UnityEngine.Rigidbody2D.AddRelativeForce extracted from open source projects. You can rate examples to help us improve the quality of examples. When controlling the RigidBody2D in kinematic mode, one needs to alter the position of the node, in order to have the integrateforces called. There, one also needs to update the Physics2DDirectBodyState transform, to not get into problems later on when switching back.

Hi,

I want to switch the behaviour of a Rigidbody2d node from kinematic to rigid and vice versa. Currently I am trying out 3.1 beta2.

What confuses me however, is how to properly move the rigidbody, when in kinematic mode. What i find is, that i should be altering the position directly, which is what is stated one should not do, reading from the documentation.

After debugging the behavior of the body. I found that It looks like, once in kinematic mode, the body's state is forced set to sleeping (though cansleep is false), and the _integrateforces function is not called. Applying forces or impulse won't move the body, so altering the position(setposition/setglobal_position) is all that's left. And once I do that, integrate is called, the body moves and is interacting with other bodies in the scene.

According to the latest doc, it should behave like the KinematicBody2d, but in the rigidbody class, we don't have access to the 'move_*' functions , which is the recommended way to use that node type.

Asked already on on the QA site, but I thought I'd add it here as well.
https://godotengine.org/qa/39960/how-to-properly-move-rigidbody2d-in-kinematic-mode

This tutorial was written prior to Godot Recipes. Its format will eventually be updated to match the rest of the docs on this site.

In this tutorial, I’ll explain when (and when not) to use rigid bodies, how theywork, and demonstrate a few handy tricks to bend them to your will. Theexamples will use RigidBody2D, but the lessons apply equally to 3D.

Introduction

RigidBody2Dis the physics body in Godot that provides simulated physics. This means that youdon’t control a RigidBody2D directly. Instead you apply forces to it (gravity, impulses,etc.) and Godot’s built-in physics engine calculates the resulting movement, includingcollisions, bouncing, rotating, etc.

You can modify a RigidBody2D’s behavior via properties such as “Mass”, “Friction”,or “Bounce”, which can be set in the Inspector:

The body’s behavior is also affected by the world, via the Project Settings -> Physicsproperties, or by entering an Area2D that is overriding the global physics properties.

Using RigidBody2D

One of the benefits of using a rigid body is that a lot of behavior can be gotten “forfree” without writing any code. For example, let’s look at making a rudimentary“Angry Birds”-style game with falling blocks. You only need to create RigidBody2Dsfor the blocks and projectile, and set their properties. Stacking, falling, and bouncingwill automatically be handled by the physics engine.

Stacking blocks

Rigidbody2d gravity

Start by making a RigidBody2D for the block and adding Sprite andCollisionShape2Dchildren:

Add a texture to the Sprite and a rectangular collision shape. IMPORTANT: Do notchange the Scale of the collision shape! In general this is a bad idea, and willresult in unexpected collision behavior. Always use the shape’s inner size handles andnot the outer Node2D-derived scale handles.

NOTE: For the textures in this example, I’m using the Physics Asset pack from Kenney.nl. It contains awide variety of blocks in different shapes and materials.

Press “Play” and you’ll see the block fall slowly downward. This is due to thedefault global gravity. You can find this setting in “Project Settings” underPhysics -> 2d. You can also try changing the Block’s Gravity Scale property in theInspector. I’m using a value of 3.

Now create a Main scene (I usually use a Node) for the root).Add a few StaticBody2Dnodes with rectangular collision shapes to serve as your “ground” and walls.

Instance a Block, and then duplicate it (Ctrl-D on Windows and Cmd-D on MacOS)so you can make a nice stack. Something like this:

Projectile

Create another scene with the same node setup as your Block, but name this one “Ball”. Use one ofthe round textures and a circular collision shape. Instance this in your Mainscene and place it somewhere to the side of the stack of blocks.

Rigidbody2d Godot

To cause a rigid body to move, it must have some velocity. You can give the bodyan initial velocity using the Linear -> Velocity property. Try setting thisto (500, 0).

You can also tinker with the ball’s Friction and Bounce properties. Both ofthese properties can range from zero to one. I like a bounce of around 0.5.

IMPORTANT:NEVER scale a physics body! If you try, a warning will appear,and when the scene runs, the physics engine will automatically set the scale backto (1, 1).

Forces

Reset the linear velocity to (0, 0). Now what if you want to be able to tossthe ball? You should never set a rigid body’s velocity or position manually -remember, these are simulating “real-world” style physics. In the real world,objects can’t instantly jump from place to place or from a standstill to a highspeed. If you try and do so, the physics engine will resist it, and unexpectedmovement can occur. Instead, we must apply forces which create an acceleration in a certaindirection (also known as Newton’s Second Law). Godot physics objects work inthe same way.

To add force to a rigid body, you have two functions to choose from:

  • add_force()
Rigidbody2d

Adds a continuous force to the body. Imagine a rocket’s thrust, steadilypushing it faster and faster. Note that this adds to any already existingforces. The force continues to be applied until removed.

  • apply_impulse()

Adds an instantaneous “kick” to the body. Imagine hitting a baseball with a bat.

We’ll use apply_impulse() to kick the ball when we click, drag, and releasethe mouse button.

Open “Project Settings” and in the “Input Map” tab, add a new action called “click”.Connect it to the left mouse button.

Next, add a script to the Ball, and add the following code:

This script toggles dragging on when the mouse button is pressed and recordsthe location of the click. When the button is released, we find the vector fromthe click point to the release point and use that to apply the impulse (multiplyingby 5 to scale it up). apply_impulse() also takes an offset as its firstparameter. This lets you “hit” the body off center, if you wish. For instance,try setting it to Vector2(25, 0) and you’ll add some spin to the ball whenit’s launched.

Controlling Rigid Bodies

Rigidbody2d Movement

There are cases where you need more direct control of a rigid body. For example,imagine you’re trying to make a version of the classic game “Asteroids”. Theplayer’s spaceship needs to rotate using the left/right arrow keys, and to moveforward when the up arrow is pressed.

Here’s the image I’m using for my ship:

I recommend you also go to OpenGameArt and search fora nice space background (but this is totally optional).

Create a new scene for the ship as we did above with the following node structure:

  • RigidBody2D
    • Sprite
    • CollisionShape2D

Note: In Godot 3.0, 0 degrees points to the right (along the x axis).This means you need to add a Rotation of 90 to the Sprite so it willmatch the body’s direction.

By default, the physics settings provide some damping, which reduces a body’svelocity and spin. In space, there’s no friction, so there shouldn’t be anydamping at all. However, for the “Asteroids” feel, we want the ship to stop rotatingwhen we let go of the keys, so set the ship’s Angular -> Damp to 5.

Let’s walk through what this script is doing. The two variables, engine_thrustand spin_thrust control how fast the ship can accelerate and turn. In theInspector, set them to 500 and 25000 respectively (the units of torque makefor large numbers). thrust will represent the ship’s engine state: (0, 0)when coasting, or a vector with the length of engine_thrust when powered on.rotation_dir will represent what direction the ship is turning. The screensizevariable will capture the size of the screen, which we’ll be using later.

Next, the input() function captures the keystates and sets the ship’s thruston or off, and the rotation direction (rotation_dir) positive or negative. Thisfunction is called every frame in _process().

Finally, physics-related functions should be called in _physics_process(). Herewe use set_applied_force() to apply the thrust in whatever direction theship is facing. Then we use set_applied_torque() to cause the ship to rotate.

Play the scene - you should be able to fly around freely.

The Position Problem

Another feature of “Asteroids” is that the screen “wraps around”. If the playergoes off one side, it teleports to the other side. But we already talked aboveabout how you can’t change a rigid body’s position without breaking the physicsengine. This presents a huge problem when working with rigid bodies.

A common mistake is to try adding something like this to _physics_process():

This fails spectacularly, trapping the player on the edge of the screen (withoccasional glitches). So why doesn’t this work? The docs say _physics_process()is for physics-related stuff, right?

Not exactly. _physics_process() is synced to the physics timestep, but thatdoesn’t make it OK to use for just anything. Hope is not lost, however, the answeris in the docs.

To quote the RigidBody2D docs:

You should not change a RigidBody2D’s position or linear_velocity every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.

Rigidbody2d Api

And the description for _integrate_forces:

Allows you to read and safely modify the simulation state for the object. Use this instead of _physics_process if you need to directly change the body’s position or other physics properties.

So there’s our answer. Instead of using _physics_process() we need to use _integrate_forces(),which gives us access to the Physics2DDirectBodyState. I highly recommend you take a look at thelinked document, there is a lot of really useful data provided in the physics state object.For our purposes, the key piece of information is the body’s Transform2D.(Explaining transforms is beyond the scope of this document - see Matrices and transformsfor more information.)

The body’s position is contained in the transform’s origin. Change _physics_process()to _integrate_forces() and add the following code:

We grab the current transform, change it as necessary, and set it back as the newtransform. The physics engine stays happy, and everything works as expected:

Conclusion

When used properly, rigid bodies are a powerful tool in your Godot toolkit. Manyusers get in trouble, however, when they use them for the wrong purposes, orfail to understand exactly how they work.

Comments

Rigidbody2d.addforce