Forum

> > CS2D > Scripts > Detect wall between two points
Forums overviewCS2D overview Scripts overviewLog in to reply

English Detect wall between two points

5 replies
To the start Previous 1 Next To the start

old Detect wall between two points

Waldin
User Off Offline

Quote
hey guys I need help again
I have two points (lets call them A and B) and I need check if between the line of A and B is a wall (tile wall, or solid objects like wall, dispenser, etc) and know where is the wall (if there is one).
Like shoots, A is character and B the mouse, the shoot does't stop at mouse but wall. Thanks

old Re: Detect wall between two points

Baloon
GAME BANNED Off Offline

Quote
Some sample from my scripts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
local m=1
repeat
m=m+1
toX[id],toY[id]= player(id,"x")-math.sin(math.rad(player(id,"rot")+180))*m, player(id,"y")+math.cos(math.rad(player(id,"rot")+180))*m
until (tile(math.floor(toX[id]/32),math.floor(toY[id]/32),"wall")) or m>=itemtype(player(id,"weapontype"),"range")*3
toX[id], toY[id] = player(id,"x")-math.sin(math.rad(player(id,"rot")+180))*(m-1), player(id,"y")+math.cos(math.rad(player(id,"rot")+180))*(m-1)
xtoX[id], ytoY[id] = toX[id]+math.sin(math.rad(player(id,"rot")+180))*(m-1)/2, toY[id]-math.cos(math.rad(player(id,"rot")+180))*(m-1)/2
Dist= math.sqrt((player(id,"x")-toX[id])^2 + (player(id,"y")-toY[id])^2)
if zb.beam[id]~="" then
freeimage(zb.beam[id])
else
zb.beam[id]=0
end
zb.beam[id]= image("gfx/Zombie Brutality/beam.bmp",xtoX[id],ytoY[id],1)
imagepos(zb.beam[id],xtoX[id],ytoY[id],player(id,"rot")+math.random(-1,1))
imagescale(zb.beam[id],1,Dist)
alP=1
function LocalalP()
alP=alP-0.1
imagealpha(zb.beam[id],alP)
end
timer(5,"LocalalP","",10)

old Re: Detect wall between two points

Talented Doge
User Off Offline

Quote
Consider the following image:


The coordinate system is using Cartesian coordinate system, which you have already learnt early high school. You can take this approach, find the equation of the straight line, then see if there are walls that collide with the straight line.

Well, I am too lazy to figure out how to do this though.

old Re: Detect wall between two points

TrialAndError
User Off Offline

Quote
I think this is what you need, it's commonly used in tibia.

1
2
3
4
5
6
7
8
9
10
11
12
function distance(x1, y1, x2, y2)
    return math.floor(math.sqrt(math.pow(x1 - x2, 2) + math.pow(y1 - y2, 2)))
end

function freeline(x1, y1, x2, y2)
    for i=1, distance(x1, y1, x2, y2) do
        if tile(math.floor((x1 + i * math.cos(math.atan2(y2 - y1, x2 - x1))) / 32),math.floor((y1 + i * math.sin(math.atan2(y2 - y1, x2 - x1))) / 32), "walkable") == false then
            return false
        end
    end
    return true
end

There you can call freeline and put in your 2 coordinates and it will do the job (return true or false).
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview