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
Comments
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
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:
source: https://gamedev.stackexchange.com/questions/44483/how-do-i-calculate-distance-between-a-point-and-an-axis-aligned-rectangle
Likes: pie
https://deluxepixel.com
https://deluxepixel.com