Dot product math question

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By AltoWaltz

Hello,

I am trying to implement dot product from the documentation: Vector math — Godot Engine (stable) documentation in English

I have a player (2D isometric) and player has a shied and when B key is pressed blocking is active. Problem is that this works even when shield is facing away, so I would like to limit this to maybe half of the rotation.

PA = fire_direction_vector2.direction_to(weapon_direction) 	

var forward_vector = Vector2(0,1) 	
var the_object = self 	
orientation_global = (the_object.to_global( forward_vector-the_object.global_position) 	

if PA.dot(orientation_global)>0: 		
     print(PA)

fire_direction_vector2 is a player direction vector and weapon direction is an incoming impact vector. Getting seemingly random prints. Any help appreciated.

Cheers.

:bust_in_silhouette: Reply From: Asariuss

First its better to have normalized vector:

(TargetPosition - GlobalTransform.origin).Normalized().Dot(FacingDirection)

Target - GlobalTransform is vector of attack
Facing Direction is forward vector of my character

Then you need to check if that dot value is in some range. I did it as degrees angle in properties for easier setup. But Dot product is not really an angle in degrees so there goes cosinus:

ArcOfBlock = Mathf.Cos(Mathf.Deg2Rad(ArcOfBlock / 2f))

And if your new ArcOfBlock is less than DOT then it hits.
Divide by 2 because DOT is symmetric.