Tutorial:Fire Toward Mouse

This tutorial describes how to make a bullet fire toward the mouse when the user clicks. It is assumed that you know the basics of LOVE and Lua. All code takes place within main.lua.


function love.load()
	love.graphics.setBackgroundColor(0.21, 0.67, 0.97)
	bulletSpeed = 250
	bullets = {}
	player = {x=250, y=250, width=15, height=15}

The first line just sets the background color as a nice blue. bulletSpeed is our variable that defines how fast a bullet will travel in pixels per second. The table bullets will hold a list of all our bullets and the table player holds all the info about our player.

Each bullet will be its own table inside the bullets table. It will contain the properties x, y, dx, and dy. The dx and dy variables define how much the bullet should move in pixels per second on the x and y axis.

Drawing Everything

function love.draw()
	love.graphics.setColor(1, 1, 1)
	love.graphics.rectangle("fill",  player.x, player.y, player.width, player.height)
	love.graphics.setColor(0.5, 0.5, 0.5)
	for i,v in ipairs(bullets) do
		love.graphics.circle("fill", v.x, v.y, 3)

The first thing we do in the draw function is to set the color as white. After that, we draw a rectangle (which will represent the player).

The next segment sets the color as gray for the bullet. The for statement will go through each value in our table of bullets. i is the index of the current bullet and v is its value. For each bullet we draw it as a circle on the screen

Determining the Bullet's New Position

function love.update(dt)
	for i,v in ipairs(bullets) do
		v.x = v.x + (v.dx * dt)
		v.y = v.y + (v.dy * dt)

Once again we go through the list of bullets. For each one we update its x and y position. We do this by taking the current position and adding the change multiplied by the delta time.

Firing the Bullet

function love.mousepressed(x, y, button)
	if button == 1 then
		local startX = player.x + player.width / 2
		local startY = player.y + player.height / 2
		local mouseX = x
		local mouseY = y
		local angle = math.atan2((mouseY - startY), (mouseX - startX))
		local bulletDx = bulletSpeed * math.cos(angle)
		local bulletDy = bulletSpeed * math.sin(angle)
		table.insert(bullets, {x = startX, y = startY, dx = bulletDx, dy = bulletDy})

The if statement at the beginning of this makes it so that we only fire a bullet if the user left clicks. The startX and startY variables define the origin of the bullet. We want the bullet to start at the center of the player so we use player.x + player.width / 2 to get that.

To get angle we use math.atan2 and pass it the difference in y and the difference in x. This function is very useful for finding the angle between any two objects.

We find bulletDx and bulletDy by using some more trig. These varibales will define how fast the bullet will move in the x and y plane.

The final line here inserts our new bullet into the list of bullets


When all put together you get the start of a top-down shooter game. Try out making the player movable and making the bullets disappear when they go off screen.

--Somethingmoreunique 03:22, 25 December 2011 (GMT)

Other Languages