Dot Product issue

Hi,

I’m trying to make it so a Pawn can see another in front of him in a cone - I figured the best option would be to do the dot product approach, however I’m receiving irregular numbers on the X-axis and wondered if someone could help. This is a Platformer (2D) from the template.

		float ConeThirtyDegreesDotProduct = (float)FMath::Cos(FMath::DegreesToRadians(90.f / 2.f));

		FVector Dir = (ActorItr->GetActorLocation() - GetActorLocation());
		Dir.Normalize();

		FVector _FacingVector = GetActorForwardVector();
		_FacingVector.Normalize();

		float DirDotPro = FMath::Acos(FVector::DotProduct(Dir, _FacingVector));
		if (DirDotPro > ConeThirtyDegreesDotProduct)
		{
			UE_LOG(MyLog, Warning, TEXT("%s::Actor is in Range."), *this->GetName());
		}

Value 3.108492 ( Left of Enemy pawn )

Value 0.025764 ( Right of Enemy pawn )

The Y (Z Axis because of how Unreal works with 2D) works just fine, going from 0-1, but the X-axis gives off bizarre numbers and I feel I’m missing something.

Any help is appreciated!

The line

float DirDotPro = FMath::Acos(FVector::DotProduct(Dir, _FacingVector));

gives you the angle which produces said cosine (the arc cosine, or acos).
You only need to compare the result of the dot product itself, otherwise you are comparing apples and oranges, so to speak :stuck_out_tongue_winking_eye:

float DirDotPro = FVector::DotProduct(Dir, _FacingVector);

Also, as a side note, you are doing 45 degrees (90/2) not thirty :wink:

Anyway, the numbers don’t seem very right… Maybe there is something wrong with the vector itself? Where does ActorItr come from? Have you tried debugging the original Dir vector to check that it “looks” valid, given the 2 pawns?

Hey thanks for replying.

Figures I was overthinking it and yeah, the float name is old.

So I went through each Vector on each axis, they are completely valid and correct - the problem is the DotProduct itself providing irregular numbers. Actoritr is an Enemy pointer. I’m scratching my head here.

OK, let’s get some data: with debugger or prints, could you give, for a case which “fails”:

  • Value of Dir
  • Value of _FacingVector
  • Why is it “failing”

I’m not entirely seeing your point with X and Z axis… When you have the directions, and you dot them, the result is scalar… You can compare that scalar with ConeThirtyDegreesDotProduct and the result is Boolean… As far as I see, there is no reason why the xyz components of any of the vectors in your code snippet should be “going from 0-1”, as you mention, so I think I’m missing something here…

I’m a fool, I should have realized. It was working all along, the data in the output was displaying a floating point error.

Thanks for trying to help Muelas, it was muchly appreciated. :slight_smile: