# Lasers

You can draw lasers / lines easily in Defold. You will need to place the laser/line segments in an atlas with edges extruded at 2.

![](https://59336108-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHnmfJK3HDz-s0f74qO%2F-LJMLL7X9gyCR8jr-Tl5%2F-LJMLtGYPUveG-ACyFHE%2F2018-08-07%2019_26_31-Untitled-1%20%40%201070%25%20\(Group%201%2C%20RGB_8\)%20_.png?alt=media\&token=2db4d151-0d4f-434a-b124-d8ae66def769)

Your segments need to have a width of 1. Then place their sprites on a .go so that their left side is at x of 0. This may require you to place them at x of 0.5 like in this example.

The important functions used in this example are dist2d to get the distances between two 2d points - this is used to determine how far to stretch the laser which is done through setting the x scale of the sprite's go, angle\_of\_vector\_between\_two\_points which returns the radian angle between two 2d points, and vmath.quat\_rotation\_z which returns a quaternion of the rotation around the z axis based on the previously generated radian value.&#x20;

![](https://59336108-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHnmfJK3HDz-s0f74qO%2F-LJMLL7X9gyCR8jr-Tl5%2F-LJMLbjWfU4I-msmP_EW%2F2018-08-07%2019_25_17-LaserExample.png?alt=media\&token=d9c6fd6f-d9eb-4ac0-a1ce-52e0934fd42e)

{% code title="laser.script" %}

```lua
local function dist2d(x1, y1, x2, y2)
	return ((x2-x1)^2+(y2-y1)^2)^0.5
end

local function angle_of_vector_between_two_points(x1,y1, x2,y2) 
	return math.atan2(y2-y1, x2-x1) 
end

function init(self)
	msg.post(".", "acquire_input_focus")
	self.position = go.get_position()
	self.target_position = go.get_position()
	self.scale = go.get_scale()
	go.animate(".", "scale.y", go.PLAYBACK_LOOP_PINGPONG, 1.5, go.EASING_INOUTSINE, 1.2 + math.random(30) * 0.01)
end

function update(self, dt)
	local distance = dist2d(self.position.x, self.position.y, self.target_position.x, self.target_position.y)
	self.scale.x = distance
	go.set_scale(self.scale)
	local direction = angle_of_vector_between_two_points(self.position.x, self.position.y, self.target_position.x, self.target_position.y)
	local rotation = vmath.quat_rotation_z(direction)
	go.set_rotation(rotation)
end

function on_input(self, action_id, action)
	if action.x ~= nil then
		self.target_position.x = action.x
		self.target_position.y = action.y
	end
end


```

{% endcode %}

{% file src="<https://59336108-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHnmfJK3HDz-s0f74qO%2F-LJMLL7X9gyCR8jr-Tl5%2F-LJMLWCteZl_adSkjvl2%2FLaserExample.zip?alt=media&token=f7c68c41-e76d-4651-9c55-d0e44aae07ea>" %}
