Page 1 of 1

Bullet Help

Posted: Thu Jan 10, 2013 12:44 pm
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

Re: Bullet Help

Posted: Thu Jan 10, 2013 12:59 pm
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.

Re: Bullet Help

Posted: Thu Jan 10, 2013 1:00 pm
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.

Re: Bullet Help

Posted: Thu Jan 10, 2013 3:30 pm
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.

Re: Bullet Help

Posted: Thu Jan 10, 2013 3:38 pm
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

Re: Bullet Help

Posted: Thu Jan 10, 2013 5:04 pm
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.

Re: Bullet Help

Posted: Fri Jan 11, 2013 4:08 am
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.

Re: Bullet Help

Posted: Fri Jan 11, 2013 1:29 pm
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.