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 10, 2024

    Inventory System v1.16 available

    A new version of the Inventory System is available. This version requires Godot Engine 4.3 and includes many refinements. New Drag and Drop system The Drag-and-Drop component has received a major overhaul. The previous system was quite complex and the separation of the classes that implemented the click-and-drag and click-and-release approaches had so much overlap …

  • November 27, 2024

    Toggling Visibility of Nested CanvasLayers

    I had a setup with nested CanvasLayer nodes. Toggling the visibility of the root CanvasLayer doesn’t hide any nested CanvasLayer nodes. My solution was to listen to the visibility_changed signal, find any CanvasLayer child nodes, and apply the same visibility to them.

  • 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 29, 2024

    Inventory System v1.0 available

    Version 1.0 of the Inventory System is now available. It includes a few new additions since the closed beta: Lots of fixes found their way into this release as well:

    © 2025 GoGoGodot.io. All rights reserved.