나는했습니다이 작동하는 방법을 정확하게 설명 부탁하지 마십시오 방정식 쌍에서 발견 u
의 대체 계산 될 수있다 그냥 내가 놓아 둔 고대 코드에서 외삽/재 작성했습니다. (액션 스크립트 1)
이 예를 들어 개체를 구축하는 일부 기능 :
function point(x, y){
return {x, y}
}
function line(x0, y0, x1, y1){
return {
start: point(x0, y0),
end: point(x1, y1)
}
}
function ray(x, y, vx, vy){
return {
start: point(x, y),
vector: point(vx, vy)
}
}
function ray2(x, y, angle){
var rad = angle * Math.PI/180;
return ray(x, y, Math.cos(rad), Math.sin(rad));
}
교차로 코드 :
//returns the difference vector between two points (pointB - pointA)
function delta(a, b){ return point(b.x - a.x, b.y - a.y) }
//kind of a 2D-version of the cross-product
function cp(a, b){ return a.y * b.x - a.x * b.y }
function intersection(a, b){
var d21 = a.vector || delta(a.start, a.end),
d43 = b.vector || delta(b.start, b.end),
d13 = delta(b.start, a.start),
d = cp(d43, d21);
//rays are paralell, no intersection possible
if(!d) return null;
//if(!d) return { a, b, position: null, hitsA: false, hitsB: false };
var u = cp(d13, d21)/d,
v = cp(d13, d43)/d;
return {
a, b,
//position of the intersection
position: point(
a.start.x + d21.x * v,
a.start.y + d21.y * v
),
//is position on lineA?
hitsA: v >= 0 && v <= 1,
//is position on lineB?
hitsB: u >= 0 && u <= 1,
timeTillIntersection: v,
};
}
및 예 :
var a = line(0, 0, 50, 50);
var b = line(0, 50, 50, 0); //lines are crossing
console.log(intersection(a, b));
var c = line(100, 50, 150, 0); //lines are not crossing
console.log(intersection(a, c));
var d = line(100, -1000, 100, 1000); //intersection is only on d, not on a
console.log(intersection(a, d));
var e = ray(100, 50, -1, -1); //paralell to a
console.log(intersection(a, e));
반환 정보를 교점을 중심으로 지나가고, 지나가는 선/광선 위에 있습니다. 당신이 라인이나 광선을 통과하는 것은 중요하지 않습니다.
에 대한 timeTillIntersection
: 첫 번째 인수/레이는 현재 위치와 모션 벡터 및 두 번째 인수 그렇다면, 벽이나 v
을 나타내는 어떤 공/총알/일명 timeTillIntersection
그것까지 걸리는 시간이 얼마나 결정을 나타내는 경우 이 볼은 공의 속도에 사용 된 것과 같은 단위로 벽을 가로 지르거나 (현재 조건에서) 벽에 닿습니다. 따라서 기본적으로 무료로 정보를 얻을 수 있습니다.
그래서 선은 수직 일 수 없습니까? – Beta