Author Topic: function CastRay(X1,Y1,X2,Y2,Accuracy: single; Maxdist: integer): single;  (Read 1449 times)

0 Members and 1 Guest are viewing this topic.

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
CastRay does the same thing as RayCast, it sees if there is something blocking LoS from point S to point B. But what CastRay does and RayCast does not do is it actually returns the distance from point A to the collision. Meaning that if point A is at 0,0 and point B is at 10,0 and there is a poly in the way at 5,0 then CastRay would return a distance of 5 rather than simply false as its predecessor does.

x1,y1: point A
x2,y2: point B
accuracy: how accurately do you want CastRay() to check (1-5 recommended)
maxdist: maximum distance to check, if there is no collision the result will be this, otherwise it is the distance to the collision from point A

Code: [Select]
function CastRay(X1,Y1,X2,Y2,Accuracy: single; Maxdist: integer): single;
var dst,dx,dy,h,d: single;
begin
if raycast(x1,y1,x2,y2,d,maxdist) then begin
result := maxdist;
exit;
end;
dst := accuracy;
h := distance(x1,y1,x2,y2);
dx := (x2 - x1);
dy := (y2 - y1);
while(raycast(x1+((dst-accuracy)/h)*dx,y1+((dst-accuracy)/h)*dy,x1+dst/h*dx,y1+dst/h*dy,d,maxdist)) do dst := dst + accuracy;
result := dst;
end;

Enjoy.
« Last Edit: May 14, 2010, 06:30:26 pm by Hacktank »


Offline tk

  • Soldier
  • **
  • Posts: 235
You always know how to write a script in the most inefficient way as possible.
Since you have a vector, there is no need to change it into an alngle and then to vector again, spamming with sin and cos (calculating values for the same angle every time) in the loop.

btw, a function returning a point of collision of vector with poly.
Code: [Select]
function RayCast2(vx, vy: single; range: word; var x, y: single): boolean;
var i: word; rd,d,x2,y2: single;
begin
d:=Sqrt(vx*vx + vy*vy);
x2:=x; y2:=y;
for i:=1 to 1+Round(range/d) do begin
x2:=x2+vx; y2:=y2+vy;
if not RayCast(x, y, x2, y2, rd, Trunc(d+1)) then
exit;
x:=x+vx; y:=y+vy;
end;
Result:=true;
end;
« Last Edit: May 14, 2010, 08:24:51 am by tk »

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Yeah, was kinda stupid there, updated.