Go, Go, Godot!
  • 0

Projectiles going through collision objects

October 27, 2022

Ever had the problem where you’re firing a bullet or some kind of projectile at high speeds, and it just goes right through the collision object, instead of hitting it?

Here’s a weapon that fires a bullet at random velocities, to demonstrate the issue:

The bullets impact the character in various places, rather than at the first point of contact. I haven’t found a way to enable any kind of continuous contact option, outside of playing with the safe_margin setting. I ended up adding a raycast:

The ray cast looks somewhat like this:

func _physics_process(delta):
	ttl -= delta
	match state:
		STATES.FIRED:
			var collision: KinematicCollision2D = move_and_collide(velocity * delta)
			if collision: _on_collision(collision.get_collider(), collision.get_position())
			
			var rq = PhysicsRayQueryParameters2D.create(_last_pos, position, collision_mask)
			rq.collide_with_areas = true
			var res: Dictionary = get_world_2d().direct_space_state.intersect_ray(rq)
			if res.has("collider"):
				_on_collision(res.collider, res.position)
				position = res.position
			elif ttl <= 0:
				state = STATES.EXPIRED
		# ...

It’s definitely a hack, though. The move_and_collide should be replaced by the ray query. Using both instructions together could result in an even weirder situation; move_and_collide could skip a collision object (the issue we’re trying to fix with the ray query), but then still collide with a different collision object. Then the ray query redoes the same movement and collides with the first object that was missed by move_and_collide . Depending on the game, that could mean something like randomly shooting through shields or walls under specific circumstances.

The current approach is essentially a two-pass solution, where the first pass is sloppy, and the second pass works as intended, but doesn’t move the projectile.

Posted in Godot.
Share
PreviousMaking videos for the web with Godot 4’s Movie Writer
NextAudio Manager to handle the loading of sound effects in bulk

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Posts

  • October 9, 2023

    Design Patterns for Building Friendships

    In this 2018 GDC session, Spry Fox‘s Daniel Cook explains how to keep human beings from being treated as interchangeable, disposable, or abusable when designing multiplayer games. If you’re developing, or thinking about developing a multiplayer game, this is a great talk to better understand the challenges of designing multiplayer interactions that result in more …

  • January 27, 2023

    Dynamically finding and loading resources from the Filesystem for Android and Web Exports

    A little while ago, I created a type of AudioManager to make it easier to work with sound files in bulk: Rather than assigning audio streams by hand, I was doing it programmatically; reading the contents of a directory and using load() to get the resources. It worked great and saved a lot of time …

  • February 11, 2024

    Inventory System v1.7 available

    A couple of corrections and a new feature: Bug fixes:

  • December 2, 2024

    Inventory System v1.18.1 available (and v2 progress update)

    I’m wrapping up development on version 1 of the Inventory System, which is currently at v1.18.1. All core functionality is in place, and it provides many quality-of-life features. The guide covers and walks through most of the code base, and the demo projects show off a lot of use cases. This first version has been …

    © 2025 GoGoGodot.io. All rights reserved.