The physical finesse with a laser beam is that it always goes absolutely straight. It can’t cut through everything though, and finally it must come to stop – against other objects. This is how to draw a line that should imitate a laser beam and stop when it hit something.
<div style="clear: both;"> draw_set_color(c_red);
//This is for horizontal laser beam - reverse for vertical
if (collision_line(x,y,room_width,y,all,false,true)>0) {
inst = (collision_line(x,y+sprite_height/2,room_width,y+sprite_height/2,
all,false,true))
draw_line(x,y+sprite_height/2,inst.x,y+sprite_height/2);
}
else {
draw_line(x,y+sprite_height/2,room_width,y+sprite_height/2);
}
draw_sprite(sprite_index,0,x,y);
}</div>
There are two situations. Case one is when collision line return a positive number. That means it hit something. The function will then return the ID of what it hit and that ID can be used when addressing different variables. You should actually not draw between your x and y and both of the walls position, since the beam should always be straight. Which direction to stick to depends upon from where the beam is coming.
Case two is when no line is hit, and the line should be fully drawn… what is fully drawn you decide in the draw line function.
Your welcome to ask anything by commenting, as usual. Finally, here is the url for the example:
http://gmtutorials.com/files/laserbeam.gm6
.
Users logged in:
Comments
Loading comments...