Hi all,

A game I'm trying to develop would benefit from a weapons targetting system.
The system needs to check which of the models existing in a group
'pEnemiesGrp' are within a 30 degree angle of the player's ship's forward
vector and within a set distance from the ship. It then needs to return a
list of models ordered by their distances from the ship.

I've written a routine which does most of this - everything bar the ordering
of the output list of models should be working but unfortunately it's not...

I'm testing this with a single, static enemy ship and with the player's ship
fixed in position with only pitch and yaw controls. The enemy ship is
within the specified distance from the ship.

The problem is the value of eangle (which SHOULD BE the angle between the
ship's fwd vector and the vector from the ship to the enemy) only seems to
change if the ship is pitched up and down not when it's rotated left or
right. Am I missunderstanding how angleBetween works? After much playing
around I am still getting nowhere with understanding why this is happening

Can anyone help me work this out please?

In addition is there an quicker way of ordering the returned model list than
by checking each enemydist element one by one? I looked at creating a
multidim list but couldn't see a way of sorting it by distance.




-- initialise
targets = []
targetdists = []
currtarget = void
-- if there are enemies
if (pEnemiesGrp.child.count > 0) then
repeat with i = 1 to pEnemiesGrp.child.count
-- get ship transform and position
shiptran = pPlayerShip.transform
shippos = shiptran.position
-- ship fwd vector
shipfwd = shiptran.zaxis
-- get this enemy's position and distance from the player
enemy = pEnemiesGrp.child[i]
epos = enemy.transform.position
edist = shippos.distanceTo(epos)
-- vector from the ship to this enemy
ship2enemy = getnormalized(epos - shippos)
-- angle between ship fwd vector and ship to enemy vector
eangle = shipfwd.angleBetween(ship2enemy)
-- if this enemy is within targetting distance
if edist < pPlayerShip.userData.targettingdist then
-- if the vector towards this enemy is within a specified angle from
the ship's fwd vector
if eangle < 30 then
-- add this enemy to the targets list
-- add this enemy's distance to the targetdists list
end if
end if
end repeat
-- get the currently selected target from ship's properties
if not voidP(pPlayerShip.userData.targettedenemy) then
currtarget = pPlayerShip.userData.targettedenemy
end if
-- if the currently selected target is not in the targets list select a
new target
if targets.getOne(currtarget) = 0 then
if targets.count > 0 then
-- get the index position of the targetdist closest to the player
ind = targetdists.getPos(targetdists.min())
-- use 'ind' as the index position of the targets list
if not voidP(targets[ind]) then
pPlayerShip.userData.targettedenemy = targets[ind]
pPlayerShip.userData.targettedenemy = void
end if
end if
end if
pPlayerShip.userData.targettedenemy = void
end if
pPlayerShip.userData.targetlist = targets