Forum

> > CS2D > Scripts > How to do image collision detection?
Forums overviewCS2D overview Scripts overviewLog in to reply

English How to do image collision detection?

2 replies
To the start Previous 1 Next To the start

old How to do image collision detection?

FishyFinThing
User Off Offline

Quote
Hi there it's me again for another scripting question.

Apparently, I think Gradius is cool and I want to implement the Option thingy into CS2D. You know the glowing orb that follow player and shoot right?

So far I can make the image follow me smoothly. I can also make them shot snowball too.

But I think snowball and rocket are boring so I make a custom bullet.

But the problem is I don't know how to do collision detection for that bullet. If anyone know how, please let me know.

Updated: The collision detection is working now. However, there a bug I still don't know why. When the bullet collide somewhere near the drone, all the next bullets range get shorten. I can't shot any further until I stop holding right mouse button for a while.

I guess it have something to do with the freeimage timer on line 97.
But without it, the bullet won't disappear.


Here is the code that I use to make bullet. Sorry for posting a long code. If you want to see where the problem is, just skip to line 80 - 146:
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
bullet_speedDam = 1 --bullet speed dam -- more mean slower

--create bullet
drone_id = {}
drone_x = {}
drone_y = {}

--player last positions
drone_player_lastx = {} 
drone_player_lasty = {}

drone_player_index_use = 1 --at which pos drone is tracing
drone_player_index_write = 1 --at which index player last pos got update

drone_update = false --check when player last pos should be updated
drone_moving = false --check when drone should start chasing player

drone_chain_length = 5 --drone chain's length
drone_chain_distance = 0 --extra distance between each drone

bullet_speedDam = 3--bullet speed dam -- more mean slower
bullet_id = {}
bullet_id_max = 3200
bullet_id_index = 1
bullet_id_lifespand = {}

--key check variables and key bind
key_hold_w = false
key_hold_a = false
key_hold_s = false
key_hold_d = false
key_hold_mouse1 = false

addbind("W")
addbind("A")
addbind("S")
addbind("D")
addbind("mouse1")

--create drone
function make_drone(d_id)
  drone_id[d_id] = image("gfx/sprites/flare1.bmp",player(1,"x"),player(1,"y"),0)
  imagecolor(drone_id[d_id],255,100,0)
  imageblend(drone_id[d_id],1)
  tween_animate(drone_id[d_id],100,4)
end

function drone_follow()
	if(key_hold_w or key_hold_a or key_hold_s or key_hold_d) then
		drone_player_lastx[drone_player_index_write] = player(1,"x")
		drone_player_lasty[drone_player_index_write] = player(1,"y")
		drone_player_index_write = drone_player_index_write + 1
		if(drone_player_index_write > drone_chain_length) then
		  drone_player_index_write = 1
		  drone_moving = true
		end

		if(drone_moving) then
			drone_player_index_use = drone_player_index_use + 1
			if(drone_player_index_use > drone_chain_length) then
				drone_player_index_use = 1
			end
			
			for i = 0, 1 do
				if(drone_player_index_use - i - drone_chain_distance>= 1) then
					drone_x[i+1] = drone_player_lastx[drone_player_index_use - i - drone_chain_distance]
					drone_y[i+1] = drone_player_lasty[drone_player_index_use - i - drone_chain_distance]
					tween_move(drone_id[i+1],100,drone_x[i+1],drone_y[i+1])
				else
					drone_x[i+1] = drone_player_lastx[drone_chain_length + (drone_player_index_use - i - drone_chain_distance)]
					drone_y[i+1] = drone_player_lasty[drone_chain_length + (drone_player_index_use - i - drone_chain_distance)]
					tween_move(drone_id[i+1],100,drone_x[i+1],drone_y[i+1])
				end
				
			end
		end
	end
end

--create bullet
function spawn_bullet(id,x,y,dir,dis)
	local lifespand
	local i_id
	
	if(x ~= nil and y ~= nil) then
		i_id = image("gfx/sprites/raindrop.bmp",x,y,0)
		
		dir = (dir - 90) --direction
		imagecolor(i_id,100,100,255)
		imageblend(i_id,1)
		imagepos(i_id,x,y,dir - 90)
		
		--make bullet move toward the destination
		tween_move(i_id,bullet_speedDam * dis, x + dis * math.cos(math.rad(dir)), y + dis * math.sin(math.rad(dir)), dir - 90)
		
		--free bullet when it's reach max distance
		timer(bullet_speedDam * dis,"freeimage",i_id)
		
		--collition detection call -- when to call this using timer?
		image_collision(i_id,lifespand)
	end
end

function image_collision(i_id,lifespand) --how should I do this?
	local x
	local y
	local hitbox
	local hitList
	local hitWall = false
	local tile_property
	
	--msg(type(lifespand))
	
	if(imageparam(i_id,"x") and imageparam(i_id,"y")) then
		x = imageparam(i_id,"x") + 16 * math.cos(math.rad(imageparam(i_id,"rot") + 90))
		y = imageparam(i_id,"y") + 16 * math.sin(math.rad(imageparam(i_id,"rot") + 90))
		--local hitbox = image("gfx/sprites/flare1.bmp",x,y,0)
		--imagecolor(hitbox,255,0,0)
		--imageblend(hitbox,1)
		--tween_animate(hitbox,100,4)
		
		hitList = closeplayers(x,y,16,1)
		tile_property = tile((x - 16)/ 32,(y - 16) / 32,"property")
		
		if(tile_property == 1 or tile_property == 3 or tile_property == 5) then
			hitWall = true
		end
		
		if(hitWall) then
			msg("hit wall")
			freeimage(i_id)
		--	freeimage(hitbox)
		else
			if (hitList[1] ~= nil) then
				msg("hit player id: "..hitList[1].." at index"..1)
				freeimage(i_id)
			--	freeimage(hitbox)
			else
				--set up next function call
			--	timer(10,"freeimage",hitbox)
				--life = life - 10
				timer(10,"image_collision",i_id,life)
			end
		end
	end
end

--make drone on spawn
addhook("spawn","hook_spawn")
function hook_spawn(id)
	--make drone for player 1
  if (id == 1) then
    make_drone(1)
	make_drone(2)
	--make_drone(3)
	--make_drone(4)
  end
end

--key check
addhook("key","hook_key")
function hook_key(id,key,state)
	if(id == 1) then
		if(key == 'W') then
			if(state == 1) then
				key_hold_w = true
			elseif(state == 0) then
				key_hold_w = false
			end
		elseif(key == 'A') then
			if(state == 1) then
				key_hold_a = true
			elseif(state == 0) then
				key_hold_a = false
			end
		elseif(key == 'S') then
			if(state == 1) then
				key_hold_s = true
			elseif(state == 0) then
				key_hold_s = false
			end
		elseif(key == 'D') then
			if(state == 1) then
				key_hold_d = true
			elseif(state == 0) then
				key_hold_d = false
			end
		elseif(key == "mouse1") then
			if(state == 1) then
				key_hold_mouse1 = true
			elseif(state == 0) then
				key_hold_mouse1 = false
			end
		end
	end
end


addhook("ms100","hook_ms100")
function hook_ms100()
	--make drone follow player 1
	drone_follow()
	
	 
	--rapid fire mode
	if(key_hold_mouse1) then
		for i = 1, 4 do
			--parse("spawnprojectile "..id.." 75 "..drone_x[i].." "..drone_y[i].." 360 "..player(1,"rot")) --shot snowball
			spawn_bullet(1,drone_x[i],drone_y[i],player(1,"rot"),360)
		end
	end
	
end


addhook("attack","hook_attack")
function hook_attack(id)
	--attack fire mode
	if(id == 1) then
		for i = 1, 2 do
			--parse("spawnprojectile "..id.." 75 "..drone_x[i].." "..drone_y[i].." 360 "..player(1,"rot"))
			--spawn_bullet(1,drone_x[i],drone_y[i],player(1,"rot"),360)
		end
	end
end


addhook("die","hook_die")
function hook_die(id)
	--free drone when die
	if(id == 1) then
		for i = 1, 2 do
			freeimage(drone_id[i])
		end
	end
end
edited 2×, last 22.10.19 08:15:08 am

old Re: How to do image collision detection?

TrialAndError
User Off Offline

Quote
Because you are using a timer in the spawn_bullet function, that timer will still get executed even after you remove the image in the image_collision function. Because you remove it and spawn another image, that image might have the same ID as the previous one and the timer that went gets executed and removes the image.

To fix it, you need to stop the execution of the timer when you remove the image.

Added freetimer to line 29 and 35 in the following code:

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
function image_collision(i_id,lifespand) --how should I do this?
     local x
     local y
     local hitbox
     local hitList
     local hitWall = false
     local tile_property
     
     --msg(type(lifespand))
     
     if(imageparam(i_id,"x") and imageparam(i_id,"y")) then
          x = imageparam(i_id,"x") + 16 * math.cos(math.rad(imageparam(i_id,"rot") + 90))
          y = imageparam(i_id,"y") + 16 * math.sin(math.rad(imageparam(i_id,"rot") + 90))
          --local hitbox = image("gfx/sprites/flare1.bmp",x,y,0)
          --imagecolor(hitbox,255,0,0)
          --imageblend(hitbox,1)
          --tween_animate(hitbox,100,4)
          
          hitList = closeplayers(x,y,16,1)
          tile_property = tile((x - 16)/ 32,(y - 16) / 32,"property")
          
          if(tile_property == 1 or tile_property == 3 or tile_property == 5) then
               hitWall = true
          end
          
          if(hitWall) then
               msg("hit wall")
               freeimage(i_id)
               freetimer("freeimage",i_id)
          --     freeimage(hitbox)
          else
               if (hitList[1] ~= nil) then
                    msg("hit player id: "..hitList[1].." at index"..1)
                    freeimage(i_id)
                    freetimer("freeimage",i_id)
               --     freeimage(hitbox)
               else
                    --set up next function call
               --     timer(10,"freeimage",hitbox)
                    --life = life - 10
                    timer(10,"image_collision",i_id,life)
               end
          end
     end
end
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview