0 votes

Hello,

I am trying to implement dot product from the documentation: https://docs.godotengine.org/en/stable/tutorials/math/vector_math.html#dot-product

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)

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

Cheers.

Godot version 3.3.2
in Engine by (44 points)

1 Answer

0 votes

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.

by (14 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.