Then we set applied_torque to cause the ship to rotate. Here we set applied_force to apply the thrust in whatever direction the ship is facing. This function is called every frame in _process().įinally, physics-related functions should be called in _physics_process(). Next, the input() function captures the keystates and sets the ship’s thrust on or off, and the rotation direction ( rotation_dir) positive or negative.
The screensize variable will capture the size of the screen, which we’ll be using later.
rotation_dir will represent what direction the ship is turning. thrust will represent the ship’s engine state: (0, 0) when coasting, or a vector with the length of engine_thrust when powered on. The two variables, engine_thrust and spin_thrust control how fast the ship can accelerate and turn. Let’s walk through what this script is doing. is_action_pressed( "ui_left"):Īpplied_torque = rotation_dir * spin_thrust To do that, we set the ship’s Angular/Damp to 5 and Linear/Damp to 1 in the Inspector.Įxport var engine_thrust = 500 export var spin_thrust = 15000 var thrust = Vector2() However, for the “Asteroids” feel, we want the ship to coast to a stop and to stop rotating when we let go of the keys. In space there’s no friction, so there shouldn’t be and damping at all. The player’s spaceship needs to rotate using the left/right arrow keys, and to move forward when the up arrow is pressed.īy default, there is some damping (set in Project Settings), which reduces a body’s velocity and spin. For example, imagine you’re trying to make a version of the classic game “Asteroids”. There are cases where you need more direct control of a rigid body. You can also adjust contacts_reported to set the number of collisions that will be reported. If you want to get collision information from a rigid body, you must set contact_monitor to true, which enables signals such as body_entered. Imagine hitting a baseball with a bat.īy default, while a RigidBody2D collides and interacts with other bodies in the space, it does not report those collisions.
To add force to a rigid body, you have two functions to choose from:Īdds a continuous force to the body. It is more accurate but slower, so use with caution. Continuous collision detection tries to predict collisions by moving the body continuously along its path, rather than moving and then checking for any collisions. If your fast-moving body is tunneling through objects, you might need to enable this. Using this value you can get or set the total torque applied to the body. Using this value you can get or set the total force applied to the body. Total gravity is the sum of the “Default Gravity” value from Project Settings and any additional gravity that may applied by Area2D nodes. This value multiplies the gravity applied to the body.
All movement and collision response must be done manually. NOTE: This does not mean that it gains KinematicBody2D helper functions such as move_and_slide(). Kinematic - In this mode, the body behaves like a KinematicBody2D, meaning it can only move via code.Character - In this mode, the body acts the same as in rigid mode, but does not rotate.Static - In this mode, the body does not move, similar to a StaticBody2D.The body behaves like a solid physical object, colliding and responding to forces. Here are the main properties you’ll need to understand:Ī rigid body can be in one of four modes, which affect its behavior: Many users get in trouble, however, when they use them for the wrong purposes, or fail to understand exactly how they work. When used properly, rigid bodies are a powerful tool in your Godot toolkit. The body’s behavior is also affected by the world, via the Project Settings -> Physics properties, or by entering an Area2D that is overriding the global physics properties. The physics engine controls these values. Setting a RigidBody2D's physical properties, such as position or linear_velocity directly will not work correctly.