Its mostly just math.. And ships can be made to bounce off too.. though it might some additional data (orientation data). One prob atm is that atleast turret shots which are bounced cant hit the parent ship but they cant hit the parent ship in normal FreeSpace anyway. However this can be changed too - at least for the standard primaries.
Mainly just usage of dot products and vector projection stuff..
#Global Hooks
$Simulation:
[
-- Reflection calculations
vectorFrontDirection = ba.createVector(0,0,1)
booleanReflection = 1
floatMissionTime = mn.getMissionTime()
if floatMissionTime > 0.5 then
objectPlayer = hv.Player
--Do not continue if player does not exist
if objectPlayer:isValid() then
--Continue only if trigger is set
if booleanReflection == 1 then
--Iterate through all the weapons
for k=1,#mn.Weapons do
objectWeapon = mn.Weapons[k]
stringWeaponTeam = objectWeapon.Team.Name
--Do this to all but friendly weapons
--Could be expanded or to set to deflect only certain weapons etc.
if stringWeaponTeam ~= "Friendly" then
--Calculate distance between weapon and ship's centerpoint
vectorWeaponPosition = objectWeapon.Position
vectorPlayerPosition = objectPlayer.Position
floatWeaponDistance = vectorPlayerPosition:getDistance(vectorWeaponPosition)
--Continue only if weapon is close enough to the ship
if floatWeaponDistance < 35 then
--Create reflection normal and calculate impact angle
vectorImpactNormal = vectorWeaponPosition - vectorPlayerPosition
vectorWeaponDirection = objectWeapon.Physics.Velocity
floatImpactAngle = vectorImpactNormal:getDotProduct(vectorWeaponDirection) / (vectorImpactNormal:getMagnitude() * vectorWeaponDirection:getMagnitude())
--Continue only if impact angle is proper
-- < causes the deflective shield (outer side deflects), angle is <90 degs
-- > causes the 'doomsphere'/containment field (inner side deflects), angle is >90 degs
if floatImpactAngle < 0 then
--Some vector math
---Calculate vector projection along the reflection normal for the 'impact vector' and then multiply it by 2 to get proper reflection 'lenght'
floatReflectionModifier = 2 * vectorWeaponDirection:getDotProduct(vectorImpactNormal) / vectorImpactNormal:getDotProduct(vectorImpactNormal)
---Create the proper reflection vector
vectorReflection = floatReflectionModifier * vectorImpactNormal
---101 vector math...
vectorWeaponNewDirection = vectorWeaponDirection - vectorReflection
---Set weapon flight direction and orientation according to the reflection vector.
orientationNewWeapon = vectorWeaponNewDirection:getOrientation()
objectWeapon.Orientation = orientationNewWeapon
floatWeaponVelocity = objectWeapon.Physics:getSpeed()
objectWeapon.Physics.Velocity = floatWeaponVelocity * vectorWeaponNewDirection / vectorWeaponNewDirection:getMagnitude()
end
end
end
end
end
end
end
]
#End