Bullet Help

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

Bullet Help

Post by n1ghtk1n9 »

When I run my code, and use left-click, it returns the error; core/bullet.lua:21: attempt to perform arithmetic on field 'y' (a nil value) what am I doing wrong?

main:

Code: Select all

require "core/font"
require "core/player_equip"
require "core/physics"
require "core/player"
require "core/player_turn"
require "core/bullet"
require "core/mobs"

function love.load()
	player_create()
	bullet_spawn()
	
	love.graphics.setBackgroundColor(104, 136, 248) 
end

function love.update(dt)
	bullet_keyPressed()
	bullet_shoot(dt)
	player_turn(dt)
end

function love.draw()
	player_draw()
	bullet_draw()
end

function love.mousepressed(x, y, button)

end
bullet"

Code: Select all

shots = {} -- This is where all of the shots are stored
objects.bullet = {}

function bullet_spawn()
	objects.bullet.body = love.physics.newBody(world, objects.player.body:getX(), objects.player.body:getY(), 1)
	objects.bullet.shape = love.physics.newRectangleShape(2, 5)
	objects.bullet.fixture = love.physics.newFixture(objects.bullet.body, objects.bullet.shape, 1)
	objects.bullet.body:setBullet(true)
end

function bullet_keyPressed()
	if love.mouse.isDown("l") then
		bullet_shoot()
		table.insert(shots, objects.bullet)
	end
end

function bullet_shoot(dt)
	for i, v in ipairs(shots) do
		if curDirection == playerUp then
			v.y = v.y-dt * 100
		elseif curDirection == playerDown then
			v.y = v.y+dt * 100
		elseif curDirection == playerLeft then
			v.x = v.x+dt * 100
		elseif curDirection == playerRight then
			v.x = v.x+dt * 100
		end
	end
end

function bullet_draw()
	love.graphics.setColor(0, 0, 0)
		for i,v in ipairs(shots) do
			love.graphics.rectangle("fill", v.x, v.y, 2, 5)
		end
end
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Bullet Help

Post by micha »

The error message tells you that v.y does not exist.

You are using love.physics. You should not do that, unless you need complicated physical behaviour. In your case, you only want your bullets to move up, down, left, or right, correct? In this case, write your own physics. That sound difficult, but for simple movement, it is not difficult.
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Bullet Help

Post by substitute541 »

You guys should really get a habit of understanding error messages.

You got the error because you haven't specified "y" yet. In fact, all you did was create an empty array, and assume that there are objects in there, and you ended up indexing a nil object.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
n1ghtk1n9
Prole
Posts: 10
Joined: Sat Dec 22, 2012 12:23 am

Re: Bullet Help

Post by n1ghtk1n9 »

What do you mean by "write your own physics"? And I just realized that I commented(--) me defining v.x and v.y in another .lua file. And I know what the error message says, know that I actually am awake. It's saying that on line 21 of bullet.lua, it's trying to perform arithmetic(+,*,/, -) on "y", which is nil.
Last edited by n1ghtk1n9 on Thu Jan 10, 2013 11:34 pm, edited 1 time in total.
User avatar
xXxMoNkEyMaNxXx
Party member
Posts: 206
Joined: Thu Jan 10, 2013 6:16 am
Location: Canada

Re: Bullet Help

Post by xXxMoNkEyMaNxXx »

If you want gravity, acceleration will be (0,-something (9.81 suggested))
Each object has a Position and a Velocity
The position of the part if it hasn't hit anything with respect to time(t) will be
Displacement=Acceleration/2*t^2+Velocity*t+Position
User avatar
ejmr
Party member
Posts: 302
Joined: Fri Jun 01, 2012 7:45 am
Location: South Carolina, U.S.A.
Contact:

Re: Bullet Help

Post by ejmr »

n1ghtk1n9 wrote:What do you mean by "write your own physics"?
I started out using love.physics on a shmup but that turned out to be overkill because ultimately concepts like 'gravity' did not make much sense in the context of that game. The codebase was easier to work with after replacing that with custom code that handled the movement and acceleration of bullets, which was simple to implement, as well as collision. I don't know if that's what micha was implying, but I would also suggest carefully considering whether or not you really need love.physics' functionality. For games like shooters or platformers a physics engine provides more complications than benefits in the long-run, although that is just my personal experience.
ejmr :: Programming and Game-Dev Blog, GitHub
南無妙法蓮華經
User avatar
substitute541
Party member
Posts: 484
Joined: Fri Aug 24, 2012 9:04 am
Location: Southern Leyte, Visayas, Philippines
Contact:

Re: Bullet Help

Post by substitute541 »

Love.physics is overkill unless your really trying to use functions in love.physics that are tedious to write. so I'm gonna explain some basic mechanics stuff.

The unit of measure here is "pixels per second".

You can move an object using velocity by simply adding the velocity to the objects coordinates (while, since the framerate varies, multiplying dt to velocity before adding).

Example :

Code: Select all

obj.x = obj.x + obj.vx * dt
obj.y = obj.y + obj.vy * dt
To accelerate, just add the acceleration (in pixels per second per second) to velocity (again, multplying by dt before adding).

Example :

Code: Select all

obj.vx = obj.vx + obj.ax * dt
obj.vy = obj.vy + obj.ay * dt
To convert a vector's angle and magnitude to their components (in 2D, x and y), do this :

Code: Select all

vector.x = math.cos(vector.angle) * vector.magnitude
vector.y = math.sin(vector.angle) * vector.magnitude
To do the opposite :

Code: Select all

vector.magnitude = math.sqrt(vector.x^2 + vector.y^2) -- alternatively, (vector.x^2 + vector.y^2)^0.5
vector.angle = math.atan2(vector.y, vector.x) -- the order matters, you must specify it as math.atan2(y, x) and not math.atan2(x, y). if you do the latter, you will end up with a perpendicular angle.
Oh and, it's up to you to name the variables.

Sooo, that's all. Later.
Currently designing themes for WordPress.

Sometimes lurks around the forum.
User avatar
micha
Inner party member
Posts: 1083
Joined: Wed Sep 26, 2012 5:13 pm

Re: Bullet Help

Post by micha »

Thanks guys. Thats what I meant by "write your own physics". And actually, in your bullet.lua you already have written some own physics code already. So get rid of all the love.physics stuff. This is not what you need.
Post Reply

Who is online

Users browsing this forum: No registered users and 15 guests