unmanned turret automatic targeting
Posted: Wed Oct 19, 2011 10:11 am
I have been working on a little project and ran into a bit of a technical snag; I was wondering if someone else has done something similar and can lend a hand.
The basic concept involves setting up a turret, or a number of automated turrets, the turrets will automatically locate the closest target and open fire, then move onto the next closest turret. At this stage I haven't even thought about not shooting at something behind a wall...
These are the fragments that I have cobbled together thus far;
These two will return the distance or angle between two known points by inputting the x/y coordinates of both;
I have been able to use the above distance function, in conjunction with the below code to write to the screen the distances between the two turrets and two enemies (the full code is in the game.lua attachment, put the three functions into the function.love.draw() section of main.lua and it should be about the same as what I see).
With the addition of some sort of identifier I could maybe use the turretdist function to find the closest enemy and begin shooting it, by shooting a projectile at angle that I could find using the above angle() function...
I haven't really got any programming experience so any help is really appreciated; am I going about this the right way? Or am I completely mad...
The basic concept involves setting up a turret, or a number of automated turrets, the turrets will automatically locate the closest target and open fire, then move onto the next closest turret. At this stage I haven't even thought about not shooting at something behind a wall...
These are the fragments that I have cobbled together thus far;
These two will return the distance or angle between two known points by inputting the x/y coordinates of both;
Code: Select all
-- Returns the distance between two points.
function distance(x1,y1, x2,y2)
return ((x2-x1)^2+(y2-y1)^2)^0.5 end
-- Returns the angle between two points in radians...(?)
function angle(x1,y1, x2,y2)
return math.atan2(y2-y1, x2-x1) end
Code: Select all
function turretdist()
for i,t in ipairs (turrets) do
for j,e in ipairs (enemies) do
love.graphics.print("Distance: "..distance(t.x,t.y,e.x,e.y),20,10+(j*10)+(i*20)) --fudged y coordinates, but it works
end
end
end
I haven't really got any programming experience so any help is really appreciated; am I going about this the right way? Or am I completely mad...