unmanned turret automatic targeting

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

unmanned turret automatic targeting

Post by Kazagha »

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;

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
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).

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
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...
Attachments
game.lua
This file contains the tables with enemies and turrets, distance function not included.
(1.23 KiB) Downloaded 74 times
User avatar
ivan
Party member
Posts: 1918
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: unmanned turret automatic targeting

Post by ivan »

Sounds like you're on the right path.
Depending on the complexity of your gameplay you may want to look into state machines or other AI techniques. There's a book called "Programming Game AI by Example" by Buckland - and although the examples are in C++, I strongly recommend looking into it.
A while ago I wrote a short a tutorial on vector math that you might possibly find useful (see "Rotating to target").
At this stage I haven't even thought about not shooting at something behind a wall..
This is usually checked using a technique called "raycasting"
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: unmanned turret automatic targeting

Post by vrld »

This may sound trivial, but you are actually talking about two different problems here: The first is to find the nearest enemy and the other one is to shoot a bullet in that direction.

Your solution for the nearest neighbor search is a correct one: For every turret, seek through every enemy to determine the closest one. Depending on how many turrets and enemies you have, this may become very slow, but don't bother about that just yet.

For the second problem, there are (at least) two solutions: The trigonometric and the linear algebra one. You chose the the trigonometric one and that will work just fine, but I find it often more easy to think in order of directions and magnitues (i.e. vectors) to solve those problems.

You can get the direction and distance you have to move a turret at position (t.x, t.y) to an enemy at (e.x, e.y) by substracting the turret position from the enemy position:
\[ \mathbf{d} = \left(\begin{array}{c}d.x\\ d.y\end{array}\right) = \left(\begin{array}{c}e.x\\ e.y\end{array}\right) - \left(\begin{array}{c}t.x\\ t.y\end{array}\right) = \left(\begin{array}{c}e.x - t.x\\ e.y - t.y\end{array}\right)\]
To get just the direction in which you have to move the turret (or rather: shoot the bullet), you have to divide (normalize) the vector d by it's magnitude ||d||:
\[\mathbf{n} = \frac{\mathbf{d}}{\|\mathbf{d}\|} = \frac{1}{\sqrt{(d.x^2 + d.y^2)}} \left(\begin{array}{c}d.x\\ d.y\end{array}\right) = \left(\begin{array}{c}\frac{d.x}{\sqrt{(d.x^2 + d.y^2)}}\\ \frac{d.y}{\sqrt{(d.x^2 + d.y^2)}}\end{array}\right)\]

Now if the bullet will move 100px per second, you can use the vector n to calculate it's new position in each update step:

Code: Select all

function love.update(dt)
    (...)
    bullet.x = bullet.x + bullet.n.x * 100 * dt
    bullet.y = bullet.y + bullet.n.y * 100 * dt
end
I hope that wasn't too confusing :roll:
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
GijsB
Party member
Posts: 380
Joined: Wed Jul 20, 2011 10:19 pm
Location: Netherlands

Re: unmanned turret automatic targeting

Post by GijsB »

Or just move the turret like :
(ANGLE is the angle from turret to closest target)

turret.x = turret.x+math.cos(ANGLE)*speed*dt
turret.y = turret.y+math.sin(ANGLE)*speed*dt
User avatar
T-Bone
Inner party member
Posts: 1492
Joined: Thu Jun 09, 2011 9:03 am

Re: unmanned turret automatic targeting

Post by T-Bone »

vrld wrote:This may sound trivial, but you are actually talking about two different problems here: The first is to find the nearest enemy and the other one is to shoot a bullet in that direction.

Your solution for the nearest neighbor search is a correct one: For every turret, seek through every enemy to determine the closest one. Depending on how many turrets and enemies you have, this may become very slow, but don't bother about that just yet.

For the second problem, there are (at least) two solutions: The trigonometric and the linear algebra one. You chose the the trigonometric one and that will work just fine, but I find it often more easy to think in order of directions and magnitues (i.e. vectors) to solve those problems.

You can get the direction and distance you have to move a turret at position (t.x, t.y) to an enemy at (e.x, e.y) by substracting the turret position from the enemy position:
\[ \mathbf{d} = \left(\begin{array}{c}d.x\\ d.y\end{array}\right) = \left(\begin{array}{c}e.x\\ e.y\end{array}\right) - \left(\begin{array}{c}t.x\\ t.y\end{array}\right) = \left(\begin{array}{c}e.x - t.x\\ e.y - t.y\end{array}\right)\]
To get just the direction in which you have to move the turret (or rather: shoot the bullet), you have to divide (normalize) the vector d by it's magnitude ||d||:
\[\mathbf{n} = \frac{\mathbf{d}}{\|\mathbf{d}\|} = \frac{1}{\sqrt{(d.x^2 + d.y^2)}} \left(\begin{array}{c}d.x\\ d.y\end{array}\right) = \left(\begin{array}{c}\frac{d.x}{\sqrt{(d.x^2 + d.y^2)}}\\ \frac{d.y}{\sqrt{(d.x^2 + d.y^2)}}\end{array}\right)\]

Now if the bullet will move 100px per second, you can use the vector n to calculate it's new position in each update step:

Code: Select all

function love.update(dt)
    (...)
    bullet.x = bullet.x + bullet.n.x * 100 * dt
    bullet.y = bullet.y + bullet.n.y * 100 * dt
end
I hope that wasn't too confusing :roll:
Holy crap, math formatting <3333
User avatar
TechnoCat
Inner party member
Posts: 1612
Joined: Thu Jul 30, 2009 12:31 am
Location: Milwaukee, WI
Contact:

Re: unmanned turret automatic targeting

Post by TechnoCat »

T-Bone wrote:Holy crap, math formatting <3333
LaTeX.
User avatar
Kazagha
Prole
Posts: 40
Joined: Tue Oct 04, 2011 7:05 am

Re: unmanned turret automatic targeting

Post by Kazagha »

vrld wrote: I hope that wasn't too confusing :roll:
It has been a mighty long time since I have had to think about vectors, but it makes sense. The more I think about it the more sense it makes.

Would I be correct in assuming that bullet.n.x would be a positive value when moving... to the right, and negative when moving to the left?

There is a lot to think though, thank you everyone for your replies.
Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests