I would like to do different damage amount depending off the target impact area (front, rear, left, right of the unit). This can add very cool game mechanisms (strenghening parts of the armor, bonus when flank or rear attacks, projectile dispersion at some angles...)
I tried several solutions but one is partialy fonctional.
The following code detects the projectile orientation and the target orientation at the impact. It calculates the difference between the two.
If the difference is near to 0, the unit and the projectile are sharing the same direction. It means that the projectile hit the target from rear.
When the difference is near 180°, it means that the projectile hit the target from front.
But, when the target is hit from flank, i got near switching 90° or 270° random results. So i don't know if the projectile hit from left or right flank.
My skills in mathematics are not good enough to correct my code. Is somebody is enough skilled in math to help me ?
- Code: Select all
local oldprojectile = Projectile
Projectile = Class(oldprojectile) {
oldOnImpact = Projectile.OnImpact,
OnImpact = function(self, targetType, targetEntity)
if targetEntity and self then
local qx, qy, qz, qw = unpack(targetEntity:GetOrientation())
local a = math.atan2(2.0*(qx*qz + qw*qy), qw*qw + qx*qx - qz*qz - qy*qy)
local current_yaw = math.floor(a * (180 / math.pi) + 0.5) + 180
-- LOG('Target orientation : '..current_yaw)
qx, qy, qz, qw = unpack(self:GetOrientation())
a = math.atan2(2.0*(qx*qz + qw*qy), qw*qw + qx*qx - qz*qz - qy*qy)
current_yaw2 = math.floor(a * (180 / math.pi) + 0.5) + 180
-- LOG('Projectile orientation : '..current_yaw2)
LOG('Difference : '..math.abs(current_yaw2 - current_yaw))
end
Projectile.oldOnImpact(self, targetType, targetEntity)
end,
}