Forum

> > CS2D > Scripts > Return true if line from pos to pos is clear
Forums overviewCS2D overview Scripts overviewLog in to reply

English Return true if line from pos to pos is clear

14 replies
To the start Previous 1 Next To the start

old Return true if line from pos to pos is clear

Infinite Rain
Reviewer Off Offline

Quote
Hello guys!
Im making a project and I need help,
How to make a function that will return true if the line from the position1 to position2 is obstacle free?

and

how to rotate image to position?

Thanks

old Re: Return true if line from pos to pos is clear

Bowlinghead
User Off Offline

Quote
I think user Infinite Rain means lua...
(Its in "Scripts" section!)

You can use

1
if tile(x,y,"walkable") then
or
1
if tile(x,y,"obstacle")~=true then

And this can help (Never worked with):
1
imagepos(id,x,y,rot)		Changes position and rotation (0°-360°) of an image
edited 2×, last 18.10.11 04:16:50 pm

old Re: Return true if line from pos to pos is clear

DannyDeth
User Off Offline

Quote
the coordinates are in tiles:
1
2
3
4
5
6
7
8
9
10
11
12
function checkClearPath(start_x,start_y,destination_x,destination_y)
	rot = math.atan2(destination_y - start_y,desitnation_x - start_x ) -- finds rotation
	local tmpx = start_x; local tmpy = start_y -- these are used to store currect pos
	while(tmpx < destination_x and tmpy < destination_y) do --fixed it :)
		tmpx = tmpx + math.cos(rot)
		tmpy = tmpy + math.sin(rot)
		if not tile(tmpx,tmpy,"walkable") then
			return false
		end
	end
	return true
end
edited 1×, last 18.10.11 05:05:00 pm

old Re: Return true if line from pos to pos is clear

HeroBurak
BANNED Off Offline

Quote
user DannyDeth has written
the coordinates are in tiles:
1
2
3
4
5
6
7
8
9
10
11
12
function checkClearPath(start_x,start_y,destination_x,destination_y)
	rot = math.atan2(destination_y - start_y,desitnation_x - start_x ) -- finds rotation
	local tmpx = start_x; local tmpy = start_y -- these are used to store currect pos
	while(tmpx < destination_x and tmpy < destination_y) do --fixed it :)
		tmpx = tmpx + math.cos(rot)
		tmpy = tmpy + math.sin(rot)
		if not tile(tmpx,tmpy,"walkable") then
			return false
		end
	end
	return true
end



Slight improvement:

1
2
3
4
5
6
7
8
9
10
11
12
13
function checkClearPath(start_x,start_y,destination_x,destination_y)
	rot = math.atan2(destination_y - start_y,destination_x - start_x ) -- finds rotation
	local tmpx = start_x; local tmpy = start_y -- these are used to store currect pos
	local xincrement = math.cos(rot); local yincrement = math.sin(rot)
	while(tmpx < destination_x and tmpy < destination_y) do --fixed it :)
		tmpx = tmpx + xincrement
		tmpy = tmpy + yincrement
		if not tile(tmpx,tmpy,"walkable") then
			return false
		end
	end
	return true
end

old Re: Return true if line from pos to pos is clear

MikuAuahDark
User Off Offline

Quote
it uses tile:
code:
Spoiler >

bdw it's uses rectangular area. sorry if im wrong!
edited 2×, last 20.10.11 08:47:51 am

old Re: Return true if line from pos to pos is clear

Apache uwu
User Off Offline

Quote
btw there's no point in putting a semicolon, this isn't php or java

1
local tmpx = start_x local tmpy = start_y

or

1
2
local tmpx = start_x
	local tmpy = start_y

or

1
local tmpx,tmpy = start_x,start_y

@how to rotate image to position?

Could use tween_rotate. If you want to find the angle use

1
angle=Math.atan(y1-y2,x1-x2)

old Re: Return true if line from pos to pos is clear

DannyDeth
User Off Offline

Quote
@Textual Context:
You don't know everything, putting:
1
local xincrement = (blablabla) local yincrement = (blablabla)
Will yield you nothing but an error. The semi-colon is there to separate the two statements.

Edit: Eh. Turns out I was wrong. NVMD! You do know everything.

Edit2: It's still bad form, though. The semi-colon makes it more readable.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview