From e97d499568c133c8ec874d48b9f483edda3cbb26 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sat, 17 Jan 2026 12:24:11 +0200 Subject: [PATCH] raycasting units' hitboxes --- weapon.cpp | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/weapon.cpp b/weapon.cpp index 61dd3c7..6b3e75e 100644 --- a/weapon.cpp +++ b/weapon.cpp @@ -136,14 +136,26 @@ namespace battleship{ Vector3 refPos = (isWeaponPos ? components[0].node->localToGlobalPosition(Vector3::VEC_ZERO) : unit->getPos()); if(ordTp == (int)orderType){ - Vector3 targPos = (targDestruct ? targDestruct->getPos() : unit->getOrder(0).targets[0].pos); - float targDist = refPos.getDistanceFrom(targPos); - bool withinRange = (minRange <= targDist && targDist <= maxRange); - Vector3 dirVec = (isWeaponPos ? components[components.size() - 1].node->getGlobalAxis(2) : unit->getDirVec()); - bool withinAngle = (dirVec.getAngleBetween((targPos - refPos).norm()) <= maxFireAngle); + bool aimedAtTarget = false; - if((Order::TYPE)ordTp == Order::TYPE::ATTACK && withinRange && withinAngle) + if(targDestruct && targDestruct->getHitbox()){ + vector res = RayCaster::cast(refPos, dirVec, targDestruct->getHitbox()); + + if(res.empty()) return; + + float targDist = refPos.getDistanceFrom(res[0].pos); + aimedAtTarget = (minRange <= targDist && targDist <= maxRange); + } + else{ + Vector3 targPos = unit->getOrder(0).targets[0].pos; + float targDist = refPos.getDistanceFrom(targPos); + bool withinRange = (minRange <= targDist && targDist <= maxRange); + bool withinAngle = (dirVec.getAngleBetween((targPos - refPos).norm()) <= maxFireAngle); + aimedAtTarget = withinRange && withinAngle; + } + + if((Order::TYPE)ordTp == Order::TYPE::ATTACK && aimedAtTarget) fire(unit->getOrder(0)); } else{