How to check if an object is "looking at" another object
by Mehmet Emre Yorulmaz · in Torque 2D Beginner · 07/17/2017 (8:21 am) · 5 replies
Lets say we have two points on the scene and one of these points has a "view angle" of 90 degrees.
How can I check if the OTHER object is in the first object's view angle?
How can I check if the OTHER object is in the first object's view angle?
About the author
#2
However as soon as that object rotates the sin and cos calculations stop returning the desired values and start returning stuff like this;
"-nan(ind)"
Here is my code;
07/18/2017 (4:50 am)
I managed to get it working to a certain degree, I take the sin and cos of the angle of the "this.owner" object to get its forward vector.However as soon as that object rotates the sin and cos calculations stop returning the desired values and start returning stuff like this;
"-nan(ind)"
Here is my code;
//Angle calculation
//Vector from object A to object B
%checkVector = Vector2Sub(
player.getPosition(),%this.owner.getposition());
echo("checkVector");
echo(%checkVector);
%ownerangle=%this.owner.getangle();
echo("ownerangle");
echo(%ownerangle);
%coso=mAcos(%ownerangle);
echo("%coso");
echo(%coso);
%sino=mAsin(%ownerangle);
echo("%sino");
echo(%sino);
%forwardVector = %sino SPC %coso;
echo("Forward vector");
echo(%forwardVector);
%angle = Vector2AngleBetween(%forwardVector, %checkVector);
echo("your angle is");
echo(%angle);
echo("the guards angle is");
echo(%ownerangle);
//angles done
if(%solidcount==0 && %distance <=100 && %angle<=90 ){
echo("you are detected!");
}
#3
Try using:
"-nan(ind)" is likely an out of range error since taking the arcsin or arccos of a number not between -1 and 1 (-PI/2, PI/2) will generate an error.
Last thing. I'm pretty sure your angles will be in radians by default, so 90 degrees = 1.57 radians (PI/2). You may want to use. . .
07/18/2017 (7:32 am)
It looks like you may want to get the cosine and sine instead of the arccosine and arcsine here. That should give you a vector from your angle.Try using:
%coso=mCos(%ownerangle); %sino=mSin(%ownerangle); //Try flipping the vector as well //Vector.x = cos(angle) //Vector.y = sin(angle) %forwardVector = %coso SPC %sino;
"-nan(ind)" is likely an out of range error since taking the arcsin or arccos of a number not between -1 and 1 (-PI/2, PI/2) will generate an error.
Last thing. I'm pretty sure your angles will be in radians by default, so 90 degrees = 1.57 radians (PI/2). You may want to use. . .
%angle = mRadToDeg(Vector2AngleBetween(%forwardVector, %checkVector));
. . .
//OR simply use the radian value
if(%solidcount==0 && %distance <=100 && %angle<=1.57 ){
echo("you are detected!");
}
#4
I also added 90 degrees to the %ownerangle because my .PNG files for the sprites are facing upwards rather than facing right.
The default angles were in degrees. (for me at least) So I didn't need to use the conversion function.
07/18/2017 (7:39 am)
I took a look through the default modules that come with torque and I found the "Vector2direction" function which seemed to fix the problem.I also added 90 degrees to the %ownerangle because my .PNG files for the sprites are facing upwards rather than facing right.
The default angles were in degrees. (for me at least) So I didn't need to use the conversion function.
//Angle calculation
//Vector from object A to object B
%checkVector = Vector2Sub( %this.owner.getposition(),player.getPosition());
echo("checkVector");
echo(%checkVector);
%ownerangle=%this.owner.getangle();
echo("ownerangle");
echo(%ownerangle);
%ownerangle=%ownerangle+90;
echo("owneranglesubbed");
echo(%ownerangle);
/*
%coso=(mAcos(%ownerangle));
echo("%coso");
echo(%coso);
%sino=(mAsin(%ownerangle));
echo("%sino");
echo(%sino);
%forwardVector = %sino SPC %coso;*/
%forwardVector =Vector2Direction( %ownerangle, 2 );
echo("Forward vector");
echo(%forwardVector);
%angle = Vector2AngleBetween(%forwardVector, %checkVector);
echo("The angle between you is");
echo((%angle));
echo("the guards angle is");
echo(%ownerangle);
//angles done
if(%solidcount==0 && %distance <=100 && %angle>=90 ){
echo("you are detected!");
}
#5
07/18/2017 (9:17 am)
Seems to be a solid solution. Wonderful!
Torque Owner Caleb
Default Studio Name
(Disclaimer: I haven't worked with Torque script in a while)
function getAngle(%VectorA, %VectorB) { //Normalize the two vectors %vecA = Vector2Normalize(%VectorA); %vecB = Vector2Normalize(%VectorB); //Calculate the dot product %dot = Vector2Dot(%vecA, %vecB); //No need to divide by the magnitudes since we've normalized the vectors //Find the angle %angle = mAcos(%dot); //Return this value return %angle; } //Semi-pseudo code //Now you need to get the two vectors. This will assume you know the direction //object A is facing ("forwardVector") { // . . . in some function. . . //Direction object A is facing %forwardVector = %objectA.getForwardVector(); //<- or however you go about this //Vector from object A to object B %checkVector = Vector2Sub(%objectA.getPosition(), %objectB.getPosition()); //Find the angle between them %angle = getAngle(%forwardVector, %checkVector); //Is this within 90 degrees? if (%angle < 90) { //Do stuff here! //Note- I don't recall if the default angle measure will be in radians or //degrees. You can convert from one to the other using "mRadToDeg" and //"mDegToRad" though } }-EDIT-
After looking through some docs it would appear that there is already a built in method for calculating the angle between two vectors with "Vector2AngleBetween(VectorA, VectorB)". How handy! Just forget my "getAngle" function then. The rest should still apply.