Movement AI question, suggestion needed - Gideros Forum

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Movement AI question, suggestion needed

Member
edited December 2018
Hi,
I am asking for help on a logical/math issue I've stumbled upon: this is part of the AI that should move the enemy horde in a tower defence game.
I know the locations of the waypoints were the enemies should go

waypoints = {{x,y}, {x2,y2}, {x3,y3}...}

and I've got 3 skill levels on each enemy type, meaning that enemy at skill level 1 uses enemy.speed[1], level 2 enemy.speed[2]...

enemy.speed = {0.5,0.7,1}

this is pseudocode for the enterframe function that moves them, and worked perfectly until I decided to use decimal numbers in my speed tables; but it won't work if I don't reach integer coordinates, equal to my waypoint coordinates.
 ```function onEnterFrame()   --This is the "check" I need to change: using decimal numbers getX() won't ever reach integer coordinates, unless I use only 0.2 and 0.5... if enemy:getX() == waypoint.x and enemy:getY() == waypoint.y then set next waypoint   elseif enemy:getX()waypoint.x then enemy:setX() = enemy:getX() - enemy.speed[enemyskill]   ...the same applies with Y position... end   end```
The issue is that I'd like to use decimal numbers to differentiate speeds (there is already too much gap between 1 and 2 pixels per frame) but I need to find a better check to perform, to understand when they need to change waypoint.
Any suggestion for a math trick or a different approach?

Thank you

• Maintainer
edited December 2018
Why not using a distance check (or better: a squared distance check): if your enemy is within 1px range of the waypoint, then assume it has reached the waypoint.
• Member
@hgy29 I think I will follow your suggestion: can you elaborate a bit on the "squared distance check" please?
My horde to keep things simple just move on one axis at a time, and the waypoints are "aligned" (same X or same Y between them)

Thank you
• Member
Did you mean this one?

If (x,y) is the centre of the rectangle, the squared distance from a point (px,py) to the rectangle's border can be computed this way:
 ```dx = max(abs(px - x) - width / 2, 0); dy = max(abs(py - y) - height / 2, 0); return dx * dx + dy * dy;```
If that squared distance is zero, it means the point touches or is inside the rectangle.

source: https://gamedev.stackexchange.com/questions/44483/how-do-i-calculate-distance-between-a-point-and-an-axis-aligned-rectangle
• Maintainer
I mean just don’t bother using sqrt to get the true distance, you can compare with the squared value on each side of the < or >

Likes: pie

+1 -1 (+1 / -0 ) Share on Facebook
• Maintainer
@pie you can use <> for max and >< for min rather than call a math lib function - they should be slightly faster.
• Maintainer
• Member
Thank you @SinisterSoft I did it: the fastest route was to adapt the example to check if the value is inside boundaries, and it is pretty fast! since it's an enterframe call everywhere I could save is really good