bullets in Godot 3: Is there an Area2D that I can apply impulses to like a RigidBody2D?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By markopolo

I think it might have been possible in 2.X with the trigger property, but that’s gone. It’s still possible by having a rigidbody with a disabled collision shape and adding the area 2d as a child, but that seems wasteful when I’ve got thousands of bullets on screen at once. Am I missing something obvious?

:bust_in_silhouette: Reply From: zdimaria

Do your bullets really need physics? When I’ve done bullets, I’ve made them Area2D’s and used the Area2D’s signal on_body_entered to detect the hits.

There’s not an easier way to have movement “just work” - I’d have to write the movement code, and that seems wasteful when there’s a perfectly good physics engine avaiable :slight_smile:

markopolo | 2018-04-05 17:33

Its actually super easy, and uses way less resources to move them (you mentioned you’ll have thousands on screen).

If you’re interested, this is how you could set it up:

Make a Bullet.tscn – an Area2D with a Sprite2D and CollisionShape2D as it’s children.

Add a script to Bullet.tscn:

var SPEED = 400

func _ready():
    connect('body_entered', self, '_on_body_entered')

func _process(delta):
   var motion = Vector2(cos(self.rotation), sin(self.rotation)) * SPEED
   position += motion * delta

func _on_body_entered(body):
   print('bullet just hit: ', body.name)

And then in the scene that you want to shoot the bullet, make an instance of the bullet and set its rotation to the direction you want it to go in.

Thats it! if you don’t understand anything about the script i’d be more than happy to walk you through it.

zdimaria | 2018-04-05 17:47

“uses way less resources to move them”

I’m not sure I believe you that user scripts are more efficient than the physics engine :slight_smile: I definitely would be interested to know how that shakes out performance-wise, especially when multithreaded physics are enabled.

One use case that the engine handles nicely is bullet deflection - let’s say I want to have bullets “bend” their paths when in some area (think Auditorium but with bullets). It’s pretty easy to say body.apply_impulse when it’s in the area - I can even customize the effect by changing the bullet mass to have some bullets be more resistant than others. I’m not really inclined to figure out how to do that outside the physics engine :\

markopolo | 2018-04-05 18:34