How can I make my characters detect a Line2d... I think?

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

So, a TRON Light Cycle style of game… Remember TRON? Sark! I need my Light Cycles to detect the “light walls” that the Light Cycles generate as they speed around the Game Grid in order for them to queue_free() correctly… :smiley:Light Cycle crashing into "light wall"

So far, the closest I’ve gotten to the effect I’m seeking is by generating a Line2D via an array of points based on the cycle’s positions throughout the _process() function. The cycle should crash into the Line2d/“light wall” if any of those positions/Line2D points are found in the array twice.

The problem I’m running into now, is that the positions/Line2D points of Player 1’s Light Cycle are almost always a fraction off from Player 2’s Light Cycle. So, Player 1 can crash into its own wall, but not into Player 2’s wall, and vice versa… or at least, it rarely occurs.

Since the Vectors are floats, I tried converting the array’s points to int, or rounding the floats up or down, etc… This allows the cycles to detect their opponents walls more frequently, but still, not every time. However, this leads to other problems (the Light Cycle will frequently round to the same position within one or two steps of its move _and _slide() sequence… or sometimes it will skip over its own previous positions.

I would think this should be easy to do, but I’m such a noob, it’s taking me forever to get this mechanic implemented correctly. So, is there some reasonable way I can do this? Am I going about it completely wrong? Is there a collision or signal I can use? I tried the CollisionPolygon2D node, but that drew a whole shape, not just the line…

Sorry if this is unclear. Please let me know if I can clarify or if I should post some code or pics of what i’ve got so far…

:bust_in_silhouette: Reply From: supper_raptor

I think this method will work.
subdivide your level into cells and for each individual cell can have an int value.
0 means nothing , 1 means player 1, 2 means player 2.

var cell_count = vector2(400,400) 	#increase it for higher precision
var world_size = Vector2(1280,720) 	#set size of your map in pixels

onready var ratio = world_size / cell_count

var matrix = []

func _ready():
	#create 2d array and set it to 0
	for x in range(cell_count.x):
	    for y in range(cell_count.y):

func _process(_delta):
	var cpos = player1.position / ratio

	if matrix[int(cpos.x)][int(cpos.y)] == 1:
	elif matrix[int(cpos.x)][int(cpos.y)] == 2:
		#killed by player 2
		matrix[int(cpos.x)][int(cpos.y)] = 1

	#player 2
	cpos = player2.position / ratio

	if matrix[int(cpos.x)][int(cpos.y)] == 1:
		#killed by player 1
	elif matrix[int(cpos.x)][int(cpos.y)] == 2:
		matrix[int(cpos.x)][int(cpos.y)] = 2

I haven’t tested but this should work

Thanks, It should work. If it doesn’t, at least you’ve given me some ideas. Good idea!

CassanovaWong | 2020-05-31 08:01

So, I had to give up on using Line2D for this project and I had my character just drop tiny static bodies every frame, leaving a trail that looks like a line but acts as a wall…

CassanovaWong | 2022-03-04 23:08