collision between bodies while following path

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By shinolikesbugs
:warning: Old Version Published before Godot 3 was released.

I have a large number of units which follow a path like a tower defense and this all works well i am using set_pos() to have them follow the path; however i would also like them to not overlap eachother (i was thinking add an impulse when there is a collision but it does not work the way i did it) like they do currently they are rigidbody2D i am hoping there is an easy way to handle this. if i need to use a different body that works too just would like to know

thanks

Edit: just add the code im using to follow path

func update_path():
	path = nav.get_simple_path(get_pos(), goal, false)
	if path.size() == 0:
		queue_free()

func _fixed_process(delta):
	if path.size() > 1:
		var d = get_pos().distance_to(path[0])
		if d > 2:
			set_pos(get_pos().linear_interpolate(path[0], (speed * delta)/d))
		else:
			path.remove(0)
	else:
		queue_free()
:bust_in_silhouette: Reply From: kidscancode

If you want it to look good, you need to implement some kind of steering behavior to your units. The unit will “want” to follow the path, but also “want” to avoid nearby units. The classic use of this is “flocking” behavior, which is used to simulate all sorts of crowd-like movements.

The code required is remarkably simple. You have a “steering force” pushing the unit towards the path, and an other, smaller force pushing away from each nearby unit. Sum up the forces, and you have a resulting movement vector for the unit.

This book is the bible for implementing such things - I highly recommend checking it out:
http://natureofcode.com/book/chapter-6-autonomous-agents/

ok, ill look into it and ill post code here when i figure it out, should i be using rigidbody2D? the only functionality i want is if the units come into range of certain building to change the path to another. I feel like rigid is probably not needed, also love your youtube videos they are pretty great. you probably noticed the code snipet is from your tutorial :smiley:

shinolikesbugs | 2017-08-28 18:35

What type of body really depends on the rest of your setup, but i general, you usually don’t need a RigidBody2D. KinematicBody2D will be sufficient, you have a movement vector (ie velocity) and you pass that to move()

kidscancode | 2017-08-28 18:40