how to do the math for an enemy kill circle

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

I am doing a top down tank RTS project , I have the enemy jeeps able to scout out player locations and am now trying to figure out using that information to set up enemy attacks.

what I am trying to do is to get the AI to do things that I would do so if I was going to attack an enemy position I would move tanks to a kill circle say around a stationary gun turret.

I have things so that a stationary gun turret can be identified by the data the scout collects but I am not sure on how to do the math. here is an example of the data the scout collects.

[[StaticBody2D:1523], (1432.290039, 2048.649902), (1050.720215, 1849.259277)]

the first coordinate is the location where the stationary unit is , the second coordinate is where the jeep was when it detected the stationary. the numbers I have the scout getting are just experimental trying to figure out how to do things.

I am wondering if there are formulas for taking the stationary gun location and calculating 3 or 4 destinations around it to make a kill circle or is it just better and easier to make a helper ‘compass’ out of a couple of position2d’s and use that to come up with destinations?

oh I already know where the Godot Vector math page is so please no links to that :wink:

:bust_in_silhouette: Reply From: Wakatta

Assuming you want random positions from that “kill circle”

func _ready():

func get_position():
	var kill_circle_centre = Vector2(scout_data[1])
	var radius = 4 #Distance from center to circumference of circle
	var angle = randf() * PI * 2;
	var x = kill_circle_centre.x + cos(angle) * radius;
	var y = kill_circle_centre.y + sin(angle) * radius;
	return Vector2(x, y)

And just move jeeps to those locations

If you want specific formations have a look at some more complex Trigonometry or replace randf() with the float point values you want

thank you that is very helpful for a lot of reasons especially getting vehicles to drive themselves off of walls.

I did end up making a helper tool last night and did get the tanks to attack in a decent formation with that and that got me able to see that I probably am going to need to use a little more than just the math , the unit the tanks go after first is against a wall and that poses some issues I didn’t think about till I seen it in action, if I send an enemy unit into a wall it will just keep trying to get there that and a little less than one half of the kill circle could be on the wrong side of the wall. I was thinking today that I could add a raycast to the helper to check for walls hopefully I can figure out how to use both the math and a helper tool.

the math help is appreciated there are a few parts of my scripts that can be improved by that formula and I am not very good a trigonometry so would not have figured that out…

ArthurER | 2020-10-09 21:33

Walls huh…Rather than using StaticBody2D’s try KinematicBody2D’s instead as they have the helper functions move_and_slide() and move_and_collide() which on “collision” you could use to for specific behaviors…not that RayCasts aren’t a good idea too just less code and more predictable results

Wakatta | 2020-10-09 22:14

thanks , yes my vehicles are KinematicBody2D, and I did switch from trying navigation with RayCast to move and collide a while back, RayCast worked good on one vehicle but did not work on multiple vehicles moving at once now I am using the collision normal from move and collide it is far more reliable.

the StaticBody is a stationary turret, it was just the first thing I figure tanks would attack

the Raycast is just an idea to test for walls before I make the kill circle. I have the enemy tanks able to go from target to target now but I will have to debug and straighten out my messy scripts before I try to do anything else. after I get debugging done I will be looking at using that formula on the vehicles script to help them not get stuck in corners and walls

ArthurER | 2020-10-10 05:01