-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample_-_tilemap line collision.lua
More file actions
136 lines (115 loc) · 3.28 KB
/
Example_-_tilemap line collision.lua
File metadata and controls
136 lines (115 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
-- Tilemap line Collision
mapwidth = 10
mapheight = 10
tilewidth = math.floor(WIDTH/mapwidth)
tileheight = math.floor(HEIGHT/mapheight)
map = {}
for x=0,mapwidth do
map[x]={}
for y=0,mapheight do
map[x][y]=1
end
end
-- cut out the corners
for x=0,3 do
for y=0,3 do
map[x][y]=0
map[10-x][y]=0
map[x][10-y]=0
map[10-x][10-y]=0
end
end
-- erase the borders
for x=0,10 do
for y=0,10 do
if x<1 or y<2 or x>8 or y>8 then
map[x][y]=0
end
end
end
-- erase the center
map[5][5]=0
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
v1 = vec2(0,0)
v2 = vec2(0,0)
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(40, 40, 50)
drawtilemap()
fill(255)
text("touch the screen to check collision",200,HEIGHT-50)
-- our collision check
v1.x = CurrentTouch.x
v1.y = CurrentTouch.y
v2.x = v1.x+100
v2.y = v1.y+100
fill(255)
stroke(255)
strokeWidth(5)
line(v1.x,v1.y,v2.x,v2.y)
strokeWidth(1)
if linetilecollide(v1.x,v1.y,v2.x,v2.y) then
text("collision",400,HEIGHT-50)
end
-- Do your drawing here
end
function drawtilemap()
tw = WIDTH / 10
th = HEIGHT / 10
for y=0,10 do
for x=0,10 do
if map[x][y]==1 then
fill(69, 51, 213)
rect(x*tw,HEIGHT-y*th,tw,th)
end
end
end
end
--Unit collide with solid blocks true/false
-- what it does is takes the rectangular area and checks if this collides
-- with any of a section of rectangular blocks of the map. this section
-- is from around our collision check area
function linetilecollide(x,y,xt,yt)
cx = math.floor((x)/tilewidth)
cy = math.floor((y)/tileheight)
x2 = 0
y2 = 0
for y2=cy-1,cy+2,1 do -- if the collision area is large then increase the - and + area to check (+2 etc)
for x2=cx-1,cx+2,1 do
if x2>=0 and x2<mapwidth and y2>=0 and y2<mapheight then
if map[x2][y2] == 1 then
x3 = (x2)*tilewidth
y3 = (y2)*tileheight
if get_line_intersect(x,y,xt,yt,x3,y3,x3+tilewidth,y3) or
get_line_intersect(x,y,xt,yt,x3,y3+tileheight,x3+tilewidth,y3+tileheight) or
get_line_intersect(x,y,xt,yt,x3,y3,x3,y3+tileheight) or
get_line_intersect(x,y,xt,yt,x3+tilewidth,y3,x3+tilewidth,y3+tileheight) then
return true
end
end
end
end
end
return false
end
-- This function was originally by andre la moth.
function get_line_intersect(p0_x, p0_y, p1_x, p1_y, p2_x ,p2_y, p3_x, p3_y)
s1_x = 0
s1_y = 0
s2_x = 0
s2_y = 0
s1_x = p1_x - p0_x
s1_y = p1_y - p0_y
s2_x = p3_x - p2_x
s2_y = p3_y - p2_y
s = 0
t = 0
s = (-s1_y * (p0_x - p2_x) + s1_x * (p0_y - p2_y)) / (-s2_x * s1_y + s1_x * s2_y);
t = ( s2_x * (p0_y - p2_y) - s2_y * (p0_x - p2_x)) / (-s2_x * s1_y + s1_x * s2_y);
if s >= 0 and s <= 1 and t >= 0 and t <= 1 then return true end
return false --No collision
end