Hello! I am trying to spawn an actor on a certain distance from a player, while determining the location by shooting a vector from a controller. How do I find the coordinates of an intersection point of the vector from controller and a radius around the player?
You can get the angle between two vectors with
and if you know the distance of the controller from the player, then it’s Pythagoras ( if you mean in front of the player ).
Hey @JohnnyBichevski
you can use 2 methods to achieve this:
- Use the law of sines and from here calculate all values for a triangle
Triangle Calculator
Start would be the triangle with g f h and then g f i
int main(){
double xa,xb,xc,ya,yb,yc,za,zb,zc,r,dot,lenSq1,lenSq2,angle,intersect;
r=6;
cout << "Enter triangle coordinates\n";
cout << "Point A: ";
cin >> xa;
cin >> ya;
cin >> za;
cout << "Enter triangle coordinates\n";
cout << "Point B: ";
cin >> xb;
cin >> yb;
cin >> zb;
cout << "Enter triangle coordinates\n";
cout << "Point C: ";
cin >> xc;
cin >> yc;
cin >> zc;
vector<double> v1 { xb-xa,yb-ya,zb-za};
vector<double> v2 { xc-xa,yc-ya,zc-za};
dot = v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
lenSq1 = v1[0]*v1[0] + v1[1]*v1[1] + v1[2]*v1[2];
lenSq2 = v2[0]*v2[0] + v2[1]*v2[1] + v2[2]*v2[2];
angle = abs(acos(dot/sqrt(lenSq1 * lenSq2)));
intersect = sin(3.14159265358979323846-angle-asin(sqrt(v2[0]*v2[0]+v2[1]*v2[1]+v2[2]*v2[2])*sin(angle)/r))*r/sin(angle);
intersect = sqrt((intersect*intersect)/(v1[0]*v1[0]+v1[1]*v1[1]+v1[2]*v1[2]));
cout << intersect;
}
- Use the equations for the circle and the vector in paramteric form to create an quadratic equation and then solve this to get 0, 1 or 2 solutions / points / intersections.
Get location of vector/circle intersection? - Mathematics Stack Exchange
Quadratic equation - Wikipedia
int main() {
double a,b,c,xa,xb,ya,yb,xc,yc,r,s,t,dx,dy;
cout << "Enter triangle coordinates\n";
cout << "Point A: ";
cin >> xa;
cin >> ya;
cout << "Enter triangle coordinates\n";
cout << "Point B: ";
cin >> xb;
cin >> yb;
cout << "Enter triangle coordinates\n";
cout << "Point C: ";
cin >> xc;
cin >> yc;
r=6;
a=pow((xb-xa),2)+pow((yb-ya),2);
b=2*(xb-xa)*(xa-xc)+2*(yb-ya)*(ya-yc);
c=pow((xa-xc),2)+pow((ya-yc),2)-pow(r,2);
dx=xb-xa;
dy=yb-ya;
if(b*b-4*a*c==0){
s=(b*(-1)+sqrt(b*b-4*a*c))/(2*a);
cout << xa+s*dx << ", " << ya+s*dy;
}
else if(b*b-4*a*c>0){
s=(b*(-1)+sqrt(b*b-4*a*c))/(2*a);
t=(b*(-1)-sqrt(b*b-4*a*c))/(2*a);
if(s>=0){
cout << xa+s*dx << ", " << ya+s*dy << "\n";
}
if(t>=0){
cout << xa+t*dx << ", " << ya+t*dy << "\n";
}
}
else{
cout << "No intersection";
}
}
427Intersection.zip (188.2 KB)
503Intersection.zip (209.6 KB)
511Intersection.zip (215.8 KB)